From 361baf9c142585c2623d807ff409cd6d4e5d6f64 Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 00:42:24 -0800 Subject: [PATCH 01/10] Remove once_cell dependency --- Cargo.toml | 1 - src/lib.rs | 1 + src/platform_impl/android/mod.rs | 2 +- src/platform_impl/ios/app_state.rs | 2 +- src/platform_impl/linux/common/xkb/mod.rs | 2 +- src/platform_impl/linux/mod.rs | 2 +- .../linux/x11/ime/input_method.rs | 2 +- src/platform_impl/linux/x11/util/wm.rs | 2 +- src/platform_impl/macos/cursor.rs | 2 +- src/platform_impl/windows/dark_mode.rs | 2 +- src/platform_impl/windows/event_loop.rs | 2 +- src/platform_impl/windows/keyboard_layout.rs | 2 +- src/platform_impl/windows/util.rs | 2 +- src/utils.rs | 37 +++++++++++++++++++ 14 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index d6170f233a..f59ec09d12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,6 @@ bitflags = "2" cursor-icon = "1.1.0" log = "0.4" mint = { version = "0.5.6", optional = true } -once_cell = "1.12" rwh_04 = { package = "raw-window-handle", version = "0.4", optional = true } rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = ["std"], optional = true } rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"], optional = true } diff --git a/src/lib.rs b/src/lib.rs index dda10d2ca9..8238146f32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -187,6 +187,7 @@ mod icon; pub mod keyboard; pub mod monitor; mod platform_impl; +mod utils; pub mod window; pub mod platform; diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index f974c1d251..8992c63b1d 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -12,12 +12,12 @@ use std::{ time::{Duration, Instant}, }; +use crate::utils::Lazy; use android_activity::input::{InputEvent, KeyAction, Keycode, MotionAction}; use android_activity::{ AndroidApp, AndroidAppWaker, ConfigurationRef, InputStatus, MainEvent, Rect, }; use log::{debug, trace, warn}; -use once_cell::sync::Lazy; use crate::{ cursor::Cursor, diff --git a/src/platform_impl/ios/app_state.rs b/src/platform_impl/ios/app_state.rs index d0105dd8a8..beef5a1df1 100644 --- a/src/platform_impl/ios/app_state.rs +++ b/src/platform_impl/ios/app_state.rs @@ -10,6 +10,7 @@ use std::{ time::Instant, }; +use crate::utils::Lazy; use core_foundation::base::CFRelease; use core_foundation::date::CFAbsoluteTimeGetCurrent; use core_foundation::runloop::{ @@ -22,7 +23,6 @@ use icrate::Foundation::{ use objc2::rc::Id; use objc2::runtime::AnyObject; use objc2::{msg_send, sel}; -use once_cell::sync::Lazy; use super::uikit::UIView; use super::window::WinitUIWindow; diff --git a/src/platform_impl/linux/common/xkb/mod.rs b/src/platform_impl/linux/common/xkb/mod.rs index a861b5d6c8..d7fe55ea9d 100644 --- a/src/platform_impl/linux/common/xkb/mod.rs +++ b/src/platform_impl/linux/common/xkb/mod.rs @@ -4,10 +4,10 @@ use std::ptr::{self, NonNull}; use std::sync::atomic::{AtomicBool, Ordering}; use log::warn; -use once_cell::sync::Lazy; use smol_str::SmolStr; #[cfg(wayland_platform)] use std::os::unix::io::OwnedFd; +use utils::Lazy; use xkbcommon_dl::{ self as xkb, xkb_compose_status, xkb_context, xkb_context_flags, xkbcommon_compose_handle, xkbcommon_handle, XkbCommon, XkbCommonCompose, diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index becaadd2c7..84ff9ef5f0 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -11,7 +11,7 @@ use std::{collections::VecDeque, env, fmt}; use std::{ffi::CStr, mem::MaybeUninit, os::raw::*, sync::Mutex}; #[cfg(x11_platform)] -use once_cell::sync::Lazy; +use crate::utils::Lazy; use smol_str::SmolStr; #[cfg(x11_platform)] diff --git a/src/platform_impl/linux/x11/ime/input_method.rs b/src/platform_impl/linux/x11/ime/input_method.rs index 87bf708f49..9221efcd68 100644 --- a/src/platform_impl/linux/x11/ime/input_method.rs +++ b/src/platform_impl/linux/x11/ime/input_method.rs @@ -8,7 +8,7 @@ use std::{ }; use super::{super::atoms::*, ffi, util, XConnection, XError}; -use once_cell::sync::Lazy; +use crate::utils::Lazy; use x11rb::protocol::xproto; static GLOBAL_LOCK: Lazy> = Lazy::new(Default::default); diff --git a/src/platform_impl/linux/x11/util/wm.rs b/src/platform_impl/linux/x11/util/wm.rs index 43a800dd73..57efb6bf7a 100644 --- a/src/platform_impl/linux/x11/util/wm.rs +++ b/src/platform_impl/linux/x11/util/wm.rs @@ -1,6 +1,6 @@ use std::sync::Mutex; -use once_cell::sync::Lazy; +use crate::utils::Lazy; use super::*; diff --git a/src/platform_impl/macos/cursor.rs b/src/platform_impl/macos/cursor.rs index a7fb54e5ca..fc105eba9c 100644 --- a/src/platform_impl/macos/cursor.rs +++ b/src/platform_impl/macos/cursor.rs @@ -1,3 +1,4 @@ +use crate::utils::Lazy; use icrate::AppKit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSImage}; use icrate::Foundation::{ ns_string, NSData, NSDictionary, NSNumber, NSObject, NSObjectProtocol, NSPoint, NSSize, @@ -6,7 +7,6 @@ use icrate::Foundation::{ use objc2::rc::Id; use objc2::runtime::Sel; use objc2::{msg_send_id, sel, ClassType}; -use once_cell::sync::Lazy; use std::ffi::c_uchar; use std::slice; diff --git a/src/platform_impl/windows/dark_mode.rs b/src/platform_impl/windows/dark_mode.rs index 6487617f2c..11b6977657 100644 --- a/src/platform_impl/windows/dark_mode.rs +++ b/src/platform_impl/windows/dark_mode.rs @@ -2,7 +2,7 @@ /// which is inspired by the solution in https://github.com/ysc3839/win32-darkmode use std::{ffi::c_void, ptr}; -use once_cell::sync::Lazy; +use crate::utils::Lazy; use windows_sys::{ core::PCSTR, Win32::{ diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index b496e920f9..764ed2690d 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -17,7 +17,7 @@ use std::{ time::{Duration, Instant}, }; -use once_cell::sync::Lazy; +use crate::utils::Lazy; use windows_sys::Win32::{ Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE, diff --git a/src/platform_impl/windows/keyboard_layout.rs b/src/platform_impl/windows/keyboard_layout.rs index ac2a3444b6..31a15e49c5 100644 --- a/src/platform_impl/windows/keyboard_layout.rs +++ b/src/platform_impl/windows/keyboard_layout.rs @@ -5,7 +5,7 @@ use std::{ sync::Mutex, }; -use once_cell::sync::Lazy; +use crate::utils::Lazy; use smol_str::SmolStr; use windows_sys::Win32::{ System::SystemServices::{LANG_JAPANESE, LANG_KOREAN}, diff --git a/src/platform_impl/windows/util.rs b/src/platform_impl/windows/util.rs index 66e6be0980..f88aa5405f 100644 --- a/src/platform_impl/windows/util.rs +++ b/src/platform_impl/windows/util.rs @@ -9,7 +9,7 @@ use std::{ sync::atomic::{AtomicBool, Ordering}, }; -use once_cell::sync::Lazy; +use crate::utils::Lazy; use windows_sys::{ core::{HRESULT, PCWSTR}, Win32::{ diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000000..4725a5f506 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,37 @@ +// A poly-fill for `lazy_cell` +// Replace with std::sync::LazyLock when https://github.com/rust-lang/rust/issues/109736 is stablized. + +use std::cell::Cell; +use std::ops::Deref; +use std::sync::OnceLock; + +pub(crate) struct Lazy T> { + cell: OnceLock, + init: Cell>, +} + +unsafe impl Sync for Lazy {} + +impl T> Lazy { + pub const fn new(f: F) -> Self { + Self { + cell: OnceLock::new(), + init: Cell::new(Some(f)), + } + } + + pub fn get(&self) -> &'_ T { + self.cell.get_or_init(|| match self.init.take() { + Some(f) => f(), + None => unreachable!(), + }) + } +} + +impl T> Deref for Lazy { + type Target = T; + #[inline] + fn deref(&self) -> &'_ T { + self.get() + } +} From 702bd12b41ddc25e7025bc3962266883826aaacb Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 00:51:09 -0800 Subject: [PATCH 02/10] Fix typo --- src/platform_impl/linux/common/xkb/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform_impl/linux/common/xkb/mod.rs b/src/platform_impl/linux/common/xkb/mod.rs index d7fe55ea9d..a4506f8316 100644 --- a/src/platform_impl/linux/common/xkb/mod.rs +++ b/src/platform_impl/linux/common/xkb/mod.rs @@ -7,7 +7,7 @@ use log::warn; use smol_str::SmolStr; #[cfg(wayland_platform)] use std::os::unix::io::OwnedFd; -use utils::Lazy; +use crate::utils::Lazy; use xkbcommon_dl::{ self as xkb, xkb_compose_status, xkb_context, xkb_context_flags, xkbcommon_compose_handle, xkbcommon_handle, XkbCommon, XkbCommonCompose, From 4154614c8aa29710cb0dca7e303f015a7c65fb58 Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 00:52:01 -0800 Subject: [PATCH 03/10] Formatting --- src/platform_impl/linux/common/xkb/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform_impl/linux/common/xkb/mod.rs b/src/platform_impl/linux/common/xkb/mod.rs index a4506f8316..bb9ba0f832 100644 --- a/src/platform_impl/linux/common/xkb/mod.rs +++ b/src/platform_impl/linux/common/xkb/mod.rs @@ -3,11 +3,11 @@ use std::os::raw::c_char; use std::ptr::{self, NonNull}; use std::sync::atomic::{AtomicBool, Ordering}; +use crate::utils::Lazy; use log::warn; use smol_str::SmolStr; #[cfg(wayland_platform)] use std::os::unix::io::OwnedFd; -use crate::utils::Lazy; use xkbcommon_dl::{ self as xkb, xkb_compose_status, xkb_context, xkb_context_flags, xkbcommon_compose_handle, xkbcommon_handle, XkbCommon, XkbCommonCompose, From fadef452396941c8aeb988cd774c522cf02847b3 Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 00:55:03 -0800 Subject: [PATCH 04/10] Remove Lazy::get, only Deref is used. --- src/utils.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 4725a5f506..b280e0b45c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -19,19 +19,15 @@ impl T> Lazy { init: Cell::new(Some(f)), } } - - pub fn get(&self) -> &'_ T { - self.cell.get_or_init(|| match self.init.take() { - Some(f) => f(), - None => unreachable!(), - }) - } } impl T> Deref for Lazy { type Target = T; #[inline] fn deref(&self) -> &'_ T { - self.get() + self.cell.get_or_init(|| match self.init.take() { + Some(f) => f(), + None => unreachable!(), + }) } } From 9a1085cf6170fd4d2ebb8269bd4d3bd479f52811 Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 15:44:40 -0800 Subject: [PATCH 05/10] Constrict the unsafe impl to match once_cell's --- src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index b280e0b45c..95f825569c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -10,7 +10,7 @@ pub(crate) struct Lazy T> { init: Cell>, } -unsafe impl Sync for Lazy {} +unsafe impl Sync for Lazy {} impl T> Lazy { pub const fn new(f: F) -> Self { From 19ae9f703fb017fc72139c74f13647d73be228df Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 15:45:42 -0800 Subject: [PATCH 06/10] Make Lazy statics pub(crate). --- src/platform_impl/windows/util.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/platform_impl/windows/util.rs b/src/platform_impl/windows/util.rs index f88aa5405f..4f162d0f35 100644 --- a/src/platform_impl/windows/util.rs +++ b/src/platform_impl/windows/util.rs @@ -262,27 +262,27 @@ pub type GetPointerTouchInfo = pub type GetPointerPenInfo = unsafe extern "system" fn(pointId: u32, penInfo: *mut POINTER_PEN_INFO) -> BOOL; -pub static GET_DPI_FOR_WINDOW: Lazy> = +pub(crate)static GET_DPI_FOR_WINDOW: Lazy> = Lazy::new(|| get_function!("user32.dll", GetDpiForWindow)); -pub static ADJUST_WINDOW_RECT_EX_FOR_DPI: Lazy> = +pub(crate)static ADJUST_WINDOW_RECT_EX_FOR_DPI: Lazy> = Lazy::new(|| get_function!("user32.dll", AdjustWindowRectExForDpi)); -pub static GET_DPI_FOR_MONITOR: Lazy> = +pub(crate)static GET_DPI_FOR_MONITOR: Lazy> = Lazy::new(|| get_function!("shcore.dll", GetDpiForMonitor)); -pub static ENABLE_NON_CLIENT_DPI_SCALING: Lazy> = +pub(crate)static ENABLE_NON_CLIENT_DPI_SCALING: Lazy> = Lazy::new(|| get_function!("user32.dll", EnableNonClientDpiScaling)); -pub static SET_PROCESS_DPI_AWARENESS_CONTEXT: Lazy> = +pub(crate)static SET_PROCESS_DPI_AWARENESS_CONTEXT: Lazy> = Lazy::new(|| get_function!("user32.dll", SetProcessDpiAwarenessContext)); -pub static SET_PROCESS_DPI_AWARENESS: Lazy> = +pub(crate)static SET_PROCESS_DPI_AWARENESS: Lazy> = Lazy::new(|| get_function!("shcore.dll", SetProcessDpiAwareness)); -pub static SET_PROCESS_DPI_AWARE: Lazy> = +pub(crate)static SET_PROCESS_DPI_AWARE: Lazy> = Lazy::new(|| get_function!("user32.dll", SetProcessDPIAware)); -pub static GET_POINTER_FRAME_INFO_HISTORY: Lazy> = +pub(crate)static GET_POINTER_FRAME_INFO_HISTORY: Lazy> = Lazy::new(|| get_function!("user32.dll", GetPointerFrameInfoHistory)); -pub static SKIP_POINTER_FRAME_MESSAGES: Lazy> = +pub(crate)static SKIP_POINTER_FRAME_MESSAGES: Lazy> = Lazy::new(|| get_function!("user32.dll", SkipPointerFrameMessages)); -pub static GET_POINTER_DEVICE_RECTS: Lazy> = +pub(crate)static GET_POINTER_DEVICE_RECTS: Lazy> = Lazy::new(|| get_function!("user32.dll", GetPointerDeviceRects)); -pub static GET_POINTER_TOUCH_INFO: Lazy> = +pub(crate)static GET_POINTER_TOUCH_INFO: Lazy> = Lazy::new(|| get_function!("user32.dll", GetPointerTouchInfo)); -pub static GET_POINTER_PEN_INFO: Lazy> = +pub(crate)static GET_POINTER_PEN_INFO: Lazy> = Lazy::new(|| get_function!("user32.dll", GetPointerPenInfo)); From aa3a0d6cac2fea299437d9a7e52116c8c6b53ccb Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 15:47:42 -0800 Subject: [PATCH 07/10] Make more of the pub Lazy statics pub(crate) --- src/platform_impl/windows/event_loop.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 764ed2690d..8078b8dd7c 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -848,16 +848,16 @@ static USER_EVENT_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::WakeupMsg\0 static EXEC_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::ExecMsg\0"); // Message sent by a `Window` when it wants to be destroyed by the main thread. // WPARAM and LPARAM are unused. -pub static DESTROY_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::DestroyMsg\0"); +pub(crate) static DESTROY_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::DestroyMsg\0"); // WPARAM is a bool specifying the `WindowFlags::MARKER_RETAIN_STATE_ON_SIZE` flag. See the // documentation in the `window_state` module for more information. -pub static SET_RETAIN_STATE_ON_SIZE_MSG_ID: LazyMessageId = +pub (crate) static SET_RETAIN_STATE_ON_SIZE_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::SetRetainMaximized\0"); static THREAD_EVENT_TARGET_WINDOW_CLASS: Lazy> = Lazy::new(|| util::encode_wide("Winit Thread Event Target")); /// When the taskbar is created, it registers a message with the "TaskbarCreated" string and then broadcasts this message to all top-level windows /// -pub static TASKBAR_CREATED: LazyMessageId = LazyMessageId::new("TaskbarCreated\0"); +pub (crate) static TASKBAR_CREATED: LazyMessageId = LazyMessageId::new("TaskbarCreated\0"); fn create_event_target_window() -> HWND { use windows_sys::Win32::UI::WindowsAndMessaging::CS_HREDRAW; From a95f1dd8c6733ba479706b7d446aef20536724e0 Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 15:51:46 -0800 Subject: [PATCH 08/10] Mutex instead of Cell --- src/platform_impl/windows/event_loop.rs | 4 ++-- src/platform_impl/windows/util.rs | 24 ++++++++++++------------ src/utils.rs | 17 ++++++++--------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 8078b8dd7c..ebf1f33d1e 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -851,13 +851,13 @@ static EXEC_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::ExecMsg\0"); pub(crate) static DESTROY_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::DestroyMsg\0"); // WPARAM is a bool specifying the `WindowFlags::MARKER_RETAIN_STATE_ON_SIZE` flag. See the // documentation in the `window_state` module for more information. -pub (crate) static SET_RETAIN_STATE_ON_SIZE_MSG_ID: LazyMessageId = +pub(crate) static SET_RETAIN_STATE_ON_SIZE_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::SetRetainMaximized\0"); static THREAD_EVENT_TARGET_WINDOW_CLASS: Lazy> = Lazy::new(|| util::encode_wide("Winit Thread Event Target")); /// When the taskbar is created, it registers a message with the "TaskbarCreated" string and then broadcasts this message to all top-level windows /// -pub (crate) static TASKBAR_CREATED: LazyMessageId = LazyMessageId::new("TaskbarCreated\0"); +pub(crate) static TASKBAR_CREATED: LazyMessageId = LazyMessageId::new("TaskbarCreated\0"); fn create_event_target_window() -> HWND { use windows_sys::Win32::UI::WindowsAndMessaging::CS_HREDRAW; diff --git a/src/platform_impl/windows/util.rs b/src/platform_impl/windows/util.rs index 4f162d0f35..ae36bdaec6 100644 --- a/src/platform_impl/windows/util.rs +++ b/src/platform_impl/windows/util.rs @@ -262,27 +262,27 @@ pub type GetPointerTouchInfo = pub type GetPointerPenInfo = unsafe extern "system" fn(pointId: u32, penInfo: *mut POINTER_PEN_INFO) -> BOOL; -pub(crate)static GET_DPI_FOR_WINDOW: Lazy> = +pub(crate) static GET_DPI_FOR_WINDOW: Lazy> = Lazy::new(|| get_function!("user32.dll", GetDpiForWindow)); -pub(crate)static ADJUST_WINDOW_RECT_EX_FOR_DPI: Lazy> = +pub(crate) static ADJUST_WINDOW_RECT_EX_FOR_DPI: Lazy> = Lazy::new(|| get_function!("user32.dll", AdjustWindowRectExForDpi)); -pub(crate)static GET_DPI_FOR_MONITOR: Lazy> = +pub(crate) static GET_DPI_FOR_MONITOR: Lazy> = Lazy::new(|| get_function!("shcore.dll", GetDpiForMonitor)); -pub(crate)static ENABLE_NON_CLIENT_DPI_SCALING: Lazy> = +pub(crate) static ENABLE_NON_CLIENT_DPI_SCALING: Lazy> = Lazy::new(|| get_function!("user32.dll", EnableNonClientDpiScaling)); -pub(crate)static SET_PROCESS_DPI_AWARENESS_CONTEXT: Lazy> = +pub(crate) static SET_PROCESS_DPI_AWARENESS_CONTEXT: Lazy> = Lazy::new(|| get_function!("user32.dll", SetProcessDpiAwarenessContext)); -pub(crate)static SET_PROCESS_DPI_AWARENESS: Lazy> = +pub(crate) static SET_PROCESS_DPI_AWARENESS: Lazy> = Lazy::new(|| get_function!("shcore.dll", SetProcessDpiAwareness)); -pub(crate)static SET_PROCESS_DPI_AWARE: Lazy> = +pub(crate) static SET_PROCESS_DPI_AWARE: Lazy> = Lazy::new(|| get_function!("user32.dll", SetProcessDPIAware)); -pub(crate)static GET_POINTER_FRAME_INFO_HISTORY: Lazy> = +pub(crate) static GET_POINTER_FRAME_INFO_HISTORY: Lazy> = Lazy::new(|| get_function!("user32.dll", GetPointerFrameInfoHistory)); -pub(crate)static SKIP_POINTER_FRAME_MESSAGES: Lazy> = +pub(crate) static SKIP_POINTER_FRAME_MESSAGES: Lazy> = Lazy::new(|| get_function!("user32.dll", SkipPointerFrameMessages)); -pub(crate)static GET_POINTER_DEVICE_RECTS: Lazy> = +pub(crate) static GET_POINTER_DEVICE_RECTS: Lazy> = Lazy::new(|| get_function!("user32.dll", GetPointerDeviceRects)); -pub(crate)static GET_POINTER_TOUCH_INFO: Lazy> = +pub(crate) static GET_POINTER_TOUCH_INFO: Lazy> = Lazy::new(|| get_function!("user32.dll", GetPointerTouchInfo)); -pub(crate)static GET_POINTER_PEN_INFO: Lazy> = +pub(crate) static GET_POINTER_PEN_INFO: Lazy> = Lazy::new(|| get_function!("user32.dll", GetPointerPenInfo)); diff --git a/src/utils.rs b/src/utils.rs index 95f825569c..724c61e56b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,22 +1,20 @@ // A poly-fill for `lazy_cell` // Replace with std::sync::LazyLock when https://github.com/rust-lang/rust/issues/109736 is stablized. -use std::cell::Cell; use std::ops::Deref; +use std::sync::Mutex; use std::sync::OnceLock; pub(crate) struct Lazy T> { cell: OnceLock, - init: Cell>, + init: Mutex>, } -unsafe impl Sync for Lazy {} - impl T> Lazy { pub const fn new(f: F) -> Self { Self { cell: OnceLock::new(), - init: Cell::new(Some(f)), + init: Mutex::new(Some(f)), } } } @@ -25,9 +23,10 @@ impl T> Deref for Lazy { type Target = T; #[inline] fn deref(&self) -> &'_ T { - self.cell.get_or_init(|| match self.init.take() { - Some(f) => f(), - None => unreachable!(), - }) + self.cell + .get_or_init(|| match self.init.lock().unwrap().take() { + Some(f) => f(), + None => unreachable!(), + }) } } From bed0033cd692d285904260e38ce4b1fea0be461b Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 15:54:10 -0800 Subject: [PATCH 09/10] Don't allow closures --- src/utils.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 724c61e56b..b573210a70 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,31 +2,26 @@ // Replace with std::sync::LazyLock when https://github.com/rust-lang/rust/issues/109736 is stablized. use std::ops::Deref; -use std::sync::Mutex; use std::sync::OnceLock; -pub(crate) struct Lazy T> { +pub(crate) struct Lazy { cell: OnceLock, - init: Mutex>, + init: fn() -> T, } -impl T> Lazy { - pub const fn new(f: F) -> Self { +impl Lazy { + pub const fn new(f: fn() -> T) -> Self { Self { cell: OnceLock::new(), - init: Mutex::new(Some(f)), + init: f, } } } -impl T> Deref for Lazy { +impl Deref for Lazy { type Target = T; #[inline] fn deref(&self) -> &'_ T { - self.cell - .get_or_init(|| match self.init.lock().unwrap().take() { - Some(f) => f(), - None => unreachable!(), - }) + self.cell.get_or_init(self.init) } } From a4ebaeac985849df3cb615c6a770f25e05d065bf Mon Sep 17 00:00:00 2001 From: james7132 Date: Sat, 24 Feb 2024 16:30:30 -0800 Subject: [PATCH 10/10] Allow dead code for Lazy --- src/utils.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utils.rs b/src/utils.rs index b573210a70..5bb52a4af2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,9 @@ // A poly-fill for `lazy_cell` // Replace with std::sync::LazyLock when https://github.com/rust-lang/rust/issues/109736 is stablized. +// This isn't used on every platform, which can come up as dead code warnings. +#![allow(dead_code)] + use std::ops::Deref; use std::sync::OnceLock;