Skip to content

Commit 352e70b

Browse files
authored
m: Remove once_cell dependency
Removes the once_cell dependency, instead using std::sync::OnceLock and a minimal polyfill for std::sync::LazyLock, which may be stabilized soon (see rust-lang/rust#121377). This should not require a bump in MSRV, as OnceLock was stabilized in 1.70, which this crate is using.
1 parent 990bbf1 commit 352e70b

File tree

14 files changed

+57
-27
lines changed

14 files changed

+57
-27
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ bitflags = "2"
6767
cursor-icon = "1.1.0"
6868
log = "0.4"
6969
mint = { version = "0.5.6", optional = true }
70-
once_cell = "1.12"
7170
rwh_04 = { package = "raw-window-handle", version = "0.4", optional = true }
7271
rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = ["std"], optional = true }
7372
rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"], optional = true }

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ mod icon;
187187
pub mod keyboard;
188188
pub mod monitor;
189189
mod platform_impl;
190+
mod utils;
190191
pub mod window;
191192

192193
pub mod platform;

src/platform_impl/android/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use std::{
1212
time::{Duration, Instant},
1313
};
1414

15+
use crate::utils::Lazy;
1516
use android_activity::input::{InputEvent, KeyAction, Keycode, MotionAction};
1617
use android_activity::{
1718
AndroidApp, AndroidAppWaker, ConfigurationRef, InputStatus, MainEvent, Rect,
1819
};
1920
use log::{debug, trace, warn};
20-
use once_cell::sync::Lazy;
2121

2222
use crate::{
2323
cursor::Cursor,

src/platform_impl/ios/app_state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
time::Instant,
1111
};
1212

13+
use crate::utils::Lazy;
1314
use core_foundation::base::CFRelease;
1415
use core_foundation::date::CFAbsoluteTimeGetCurrent;
1516
use core_foundation::runloop::{
@@ -22,7 +23,6 @@ use icrate::Foundation::{
2223
use objc2::rc::Id;
2324
use objc2::runtime::AnyObject;
2425
use objc2::{msg_send, sel};
25-
use once_cell::sync::Lazy;
2626

2727
use super::uikit::UIView;
2828
use super::window::WinitUIWindow;

src/platform_impl/linux/common/xkb/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::os::raw::c_char;
33
use std::ptr::{self, NonNull};
44
use std::sync::atomic::{AtomicBool, Ordering};
55

6+
use crate::utils::Lazy;
67
use log::warn;
7-
use once_cell::sync::Lazy;
88
use smol_str::SmolStr;
99
#[cfg(wayland_platform)]
1010
use std::os::unix::io::OwnedFd;

src/platform_impl/linux/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{collections::VecDeque, env, fmt};
1111
use std::{ffi::CStr, mem::MaybeUninit, os::raw::*, sync::Mutex};
1212

1313
#[cfg(x11_platform)]
14-
use once_cell::sync::Lazy;
14+
use crate::utils::Lazy;
1515
use smol_str::SmolStr;
1616

1717
#[cfg(x11_platform)]

src/platform_impl/linux/x11/ime/input_method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
};
99

1010
use super::{super::atoms::*, ffi, util, XConnection, XError};
11-
use once_cell::sync::Lazy;
11+
use crate::utils::Lazy;
1212
use x11rb::protocol::xproto;
1313

1414
static GLOBAL_LOCK: Lazy<Mutex<()>> = Lazy::new(Default::default);

src/platform_impl/linux/x11/util/wm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Mutex;
22

3-
use once_cell::sync::Lazy;
3+
use crate::utils::Lazy;
44

55
use super::*;
66

src/platform_impl/macos/cursor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::Lazy;
12
use icrate::AppKit::{NSBitmapImageRep, NSCursor, NSDeviceRGBColorSpace, NSImage};
23
use icrate::Foundation::{
34
ns_string, NSData, NSDictionary, NSNumber, NSObject, NSObjectProtocol, NSPoint, NSSize,
@@ -6,7 +7,6 @@ use icrate::Foundation::{
67
use objc2::rc::Id;
78
use objc2::runtime::Sel;
89
use objc2::{msg_send_id, sel, ClassType};
9-
use once_cell::sync::Lazy;
1010
use std::ffi::c_uchar;
1111
use std::slice;
1212

src/platform_impl/windows/dark_mode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/// which is inspired by the solution in https://github.com/ysc3839/win32-darkmode
33
use std::{ffi::c_void, ptr};
44

5-
use once_cell::sync::Lazy;
5+
use crate::utils::Lazy;
66
use windows_sys::{
77
core::PCSTR,
88
Win32::{

src/platform_impl/windows/event_loop.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::{
1717
time::{Duration, Instant},
1818
};
1919

20-
use once_cell::sync::Lazy;
20+
use crate::utils::Lazy;
2121

2222
use windows_sys::Win32::{
2323
Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE,
@@ -848,16 +848,16 @@ static USER_EVENT_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::WakeupMsg\0
848848
static EXEC_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::ExecMsg\0");
849849
// Message sent by a `Window` when it wants to be destroyed by the main thread.
850850
// WPARAM and LPARAM are unused.
851-
pub static DESTROY_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::DestroyMsg\0");
851+
pub(crate) static DESTROY_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::DestroyMsg\0");
852852
// WPARAM is a bool specifying the `WindowFlags::MARKER_RETAIN_STATE_ON_SIZE` flag. See the
853853
// documentation in the `window_state` module for more information.
854-
pub static SET_RETAIN_STATE_ON_SIZE_MSG_ID: LazyMessageId =
854+
pub(crate) static SET_RETAIN_STATE_ON_SIZE_MSG_ID: LazyMessageId =
855855
LazyMessageId::new("Winit::SetRetainMaximized\0");
856856
static THREAD_EVENT_TARGET_WINDOW_CLASS: Lazy<Vec<u16>> =
857857
Lazy::new(|| util::encode_wide("Winit Thread Event Target"));
858858
/// When the taskbar is created, it registers a message with the "TaskbarCreated" string and then broadcasts this message to all top-level windows
859859
/// <https://docs.microsoft.com/en-us/windows/win32/shell/taskbar#taskbar-creation-notification>
860-
pub static TASKBAR_CREATED: LazyMessageId = LazyMessageId::new("TaskbarCreated\0");
860+
pub(crate) static TASKBAR_CREATED: LazyMessageId = LazyMessageId::new("TaskbarCreated\0");
861861

862862
fn create_event_target_window() -> HWND {
863863
use windows_sys::Win32::UI::WindowsAndMessaging::CS_HREDRAW;

src/platform_impl/windows/keyboard_layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
sync::Mutex,
66
};
77

8-
use once_cell::sync::Lazy;
8+
use crate::utils::Lazy;
99
use smol_str::SmolStr;
1010
use windows_sys::Win32::{
1111
System::SystemServices::{LANG_JAPANESE, LANG_KOREAN},

src/platform_impl/windows/util.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
sync::atomic::{AtomicBool, Ordering},
1010
};
1111

12-
use once_cell::sync::Lazy;
12+
use crate::utils::Lazy;
1313
use windows_sys::{
1414
core::{HRESULT, PCWSTR},
1515
Win32::{
@@ -262,27 +262,27 @@ pub type GetPointerTouchInfo =
262262
pub type GetPointerPenInfo =
263263
unsafe extern "system" fn(pointId: u32, penInfo: *mut POINTER_PEN_INFO) -> BOOL;
264264

265-
pub static GET_DPI_FOR_WINDOW: Lazy<Option<GetDpiForWindow>> =
265+
pub(crate) static GET_DPI_FOR_WINDOW: Lazy<Option<GetDpiForWindow>> =
266266
Lazy::new(|| get_function!("user32.dll", GetDpiForWindow));
267-
pub static ADJUST_WINDOW_RECT_EX_FOR_DPI: Lazy<Option<AdjustWindowRectExForDpi>> =
267+
pub(crate) static ADJUST_WINDOW_RECT_EX_FOR_DPI: Lazy<Option<AdjustWindowRectExForDpi>> =
268268
Lazy::new(|| get_function!("user32.dll", AdjustWindowRectExForDpi));
269-
pub static GET_DPI_FOR_MONITOR: Lazy<Option<GetDpiForMonitor>> =
269+
pub(crate) static GET_DPI_FOR_MONITOR: Lazy<Option<GetDpiForMonitor>> =
270270
Lazy::new(|| get_function!("shcore.dll", GetDpiForMonitor));
271-
pub static ENABLE_NON_CLIENT_DPI_SCALING: Lazy<Option<EnableNonClientDpiScaling>> =
271+
pub(crate) static ENABLE_NON_CLIENT_DPI_SCALING: Lazy<Option<EnableNonClientDpiScaling>> =
272272
Lazy::new(|| get_function!("user32.dll", EnableNonClientDpiScaling));
273-
pub static SET_PROCESS_DPI_AWARENESS_CONTEXT: Lazy<Option<SetProcessDpiAwarenessContext>> =
273+
pub(crate) static SET_PROCESS_DPI_AWARENESS_CONTEXT: Lazy<Option<SetProcessDpiAwarenessContext>> =
274274
Lazy::new(|| get_function!("user32.dll", SetProcessDpiAwarenessContext));
275-
pub static SET_PROCESS_DPI_AWARENESS: Lazy<Option<SetProcessDpiAwareness>> =
275+
pub(crate) static SET_PROCESS_DPI_AWARENESS: Lazy<Option<SetProcessDpiAwareness>> =
276276
Lazy::new(|| get_function!("shcore.dll", SetProcessDpiAwareness));
277-
pub static SET_PROCESS_DPI_AWARE: Lazy<Option<SetProcessDPIAware>> =
277+
pub(crate) static SET_PROCESS_DPI_AWARE: Lazy<Option<SetProcessDPIAware>> =
278278
Lazy::new(|| get_function!("user32.dll", SetProcessDPIAware));
279-
pub static GET_POINTER_FRAME_INFO_HISTORY: Lazy<Option<GetPointerFrameInfoHistory>> =
279+
pub(crate) static GET_POINTER_FRAME_INFO_HISTORY: Lazy<Option<GetPointerFrameInfoHistory>> =
280280
Lazy::new(|| get_function!("user32.dll", GetPointerFrameInfoHistory));
281-
pub static SKIP_POINTER_FRAME_MESSAGES: Lazy<Option<SkipPointerFrameMessages>> =
281+
pub(crate) static SKIP_POINTER_FRAME_MESSAGES: Lazy<Option<SkipPointerFrameMessages>> =
282282
Lazy::new(|| get_function!("user32.dll", SkipPointerFrameMessages));
283-
pub static GET_POINTER_DEVICE_RECTS: Lazy<Option<GetPointerDeviceRects>> =
283+
pub(crate) static GET_POINTER_DEVICE_RECTS: Lazy<Option<GetPointerDeviceRects>> =
284284
Lazy::new(|| get_function!("user32.dll", GetPointerDeviceRects));
285-
pub static GET_POINTER_TOUCH_INFO: Lazy<Option<GetPointerTouchInfo>> =
285+
pub(crate) static GET_POINTER_TOUCH_INFO: Lazy<Option<GetPointerTouchInfo>> =
286286
Lazy::new(|| get_function!("user32.dll", GetPointerTouchInfo));
287-
pub static GET_POINTER_PEN_INFO: Lazy<Option<GetPointerPenInfo>> =
287+
pub(crate) static GET_POINTER_PEN_INFO: Lazy<Option<GetPointerPenInfo>> =
288288
Lazy::new(|| get_function!("user32.dll", GetPointerPenInfo));

src/utils.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// A poly-fill for `lazy_cell`
2+
// Replace with std::sync::LazyLock when https://github.com/rust-lang/rust/issues/109736 is stablized.
3+
4+
// This isn't used on every platform, which can come up as dead code warnings.
5+
#![allow(dead_code)]
6+
7+
use std::ops::Deref;
8+
use std::sync::OnceLock;
9+
10+
pub(crate) struct Lazy<T> {
11+
cell: OnceLock<T>,
12+
init: fn() -> T,
13+
}
14+
15+
impl<T> Lazy<T> {
16+
pub const fn new(f: fn() -> T) -> Self {
17+
Self {
18+
cell: OnceLock::new(),
19+
init: f,
20+
}
21+
}
22+
}
23+
24+
impl<T> Deref for Lazy<T> {
25+
type Target = T;
26+
#[inline]
27+
fn deref(&self) -> &'_ T {
28+
self.cell.get_or_init(self.init)
29+
}
30+
}

0 commit comments

Comments
 (0)