From 1435fe47f4f4a9063151d3cbdfc3c2a5a53f6fa1 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Sun, 14 Jul 2024 13:14:32 +0200 Subject: [PATCH] Make `DeviceId/WindowId::dummy()` safe (#3784) --- src/changelog/unreleased.md | 4 ++++ src/event.rs | 15 ++++++--------- src/platform_impl/ios/mod.rs | 2 +- src/platform_impl/ios/window.rs | 2 +- src/platform_impl/linux/mod.rs | 8 ++++---- src/platform_impl/linux/wayland/mod.rs | 2 +- src/platform_impl/linux/x11/mod.rs | 2 +- src/platform_impl/macos/mod.rs | 2 +- src/platform_impl/macos/window.rs | 2 +- src/platform_impl/web/device.rs | 2 +- src/platform_impl/web/event_loop/runner.rs | 4 ++-- src/platform_impl/web/event_loop/window_target.rs | 4 ++-- src/platform_impl/web/window.rs | 2 +- src/platform_impl/windows/mod.rs | 4 ++-- src/window.rs | 9 +++------ 15 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index b7bb6e6143..b3df644625 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -40,6 +40,10 @@ changelog entry. ## Unreleased +### Changed + +- `DeviceId::dummy()` and `WindowId::dummy()` are no longer marked `unsafe`. + ### Fixed - On Wayland, avoid crashing when compositor is misbehaving. diff --git a/src/event.rs b/src/event.rs index 30a71d6514..d05d35c853 100644 --- a/src/event.rs +++ b/src/event.rs @@ -449,16 +449,13 @@ pub struct DeviceId(pub(crate) platform_impl::DeviceId); impl DeviceId { /// Returns a dummy id, useful for unit testing. /// - /// # Safety + /// # Notes /// /// The only guarantee made about the return value of this function is that /// it will always be equal to itself and to future values returned by this function. /// No other guarantees are made. This may be equal to a real `DeviceId`. - /// - /// **Passing this into a winit function will result in undefined behavior.** - pub const unsafe fn dummy() -> Self { - #[allow(unused_unsafe)] - DeviceId(unsafe { platform_impl::DeviceId::dummy() }) + pub const fn dummy() -> Self { + DeviceId(platform_impl::DeviceId::dummy()) } } @@ -1021,7 +1018,7 @@ mod tests { ($closure:expr) => {{ #[allow(unused_mut)] let mut x = $closure; - let did = unsafe { event::DeviceId::dummy() }; + let did = event::DeviceId::dummy(); #[allow(deprecated)] { @@ -1031,7 +1028,7 @@ mod tests { use crate::window::WindowId; // Mainline events. - let wid = unsafe { WindowId::dummy() }; + let wid = WindowId::dummy(); x(UserEvent(())); x(NewEvents(event::StartCause::Init)); x(AboutToWait); @@ -1160,7 +1157,7 @@ mod tests { }); let _ = event::StartCause::Init.clone(); - let did = unsafe { crate::event::DeviceId::dummy() }.clone(); + let did = crate::event::DeviceId::dummy().clone(); HashSet::new().insert(did); let mut set = [did, did, did]; set.sort_unstable(); diff --git a/src/platform_impl/ios/mod.rs b/src/platform_impl/ios/mod.rs index f69ac21b79..9c2362e11d 100644 --- a/src/platform_impl/ios/mod.rs +++ b/src/platform_impl/ios/mod.rs @@ -32,7 +32,7 @@ pub(crate) use crate::platform_impl::Fullscreen; pub struct DeviceId; impl DeviceId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { DeviceId } } diff --git a/src/platform_impl/ios/window.rs b/src/platform_impl/ios/window.rs index 8ea7aeae91..675fdfeef8 100644 --- a/src/platform_impl/ios/window.rs +++ b/src/platform_impl/ios/window.rs @@ -701,7 +701,7 @@ pub struct WindowId { } impl WindowId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { WindowId { window: std::ptr::null_mut() } } } diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 776689fe20..6cb99fee31 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -157,7 +157,7 @@ impl From for WindowId { } impl WindowId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { Self(0) } } @@ -171,11 +171,11 @@ pub enum DeviceId { } impl DeviceId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { #[cfg(wayland_platform)] - return DeviceId::Wayland(unsafe { wayland::DeviceId::dummy() }); + return DeviceId::Wayland(wayland::DeviceId::dummy()); #[cfg(all(not(wayland_platform), x11_platform))] - return DeviceId::X(unsafe { x11::DeviceId::dummy() }); + return DeviceId::X(x11::DeviceId::dummy()); } } diff --git a/src/platform_impl/linux/wayland/mod.rs b/src/platform_impl/linux/wayland/mod.rs index 674671ff8c..63052b78af 100644 --- a/src/platform_impl/linux/wayland/mod.rs +++ b/src/platform_impl/linux/wayland/mod.rs @@ -66,7 +66,7 @@ impl From for OsError { pub struct DeviceId; impl DeviceId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { DeviceId } } diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 88071b72ba..3aafd7b316 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -771,7 +771,7 @@ pub struct DeviceId(xinput::DeviceId); impl DeviceId { #[allow(unused)] - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { DeviceId(0) } } diff --git a/src/platform_impl/macos/mod.rs b/src/platform_impl/macos/mod.rs index 2c3d3e3032..1b427a87d3 100644 --- a/src/platform_impl/macos/mod.rs +++ b/src/platform_impl/macos/mod.rs @@ -37,7 +37,7 @@ pub(crate) use crate::platform_impl::Fullscreen; pub struct DeviceId; impl DeviceId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { DeviceId } } diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index eb1f75deea..04da8b29ee 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -74,7 +74,7 @@ impl Window { pub struct WindowId(pub usize); impl WindowId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { Self(0) } } diff --git a/src/platform_impl/web/device.rs b/src/platform_impl/web/device.rs index 383bf3a1b8..91d08586c8 100644 --- a/src/platform_impl/web/device.rs +++ b/src/platform_impl/web/device.rs @@ -2,7 +2,7 @@ pub struct DeviceId(pub i32); impl DeviceId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { Self(0) } } diff --git a/src/platform_impl/web/event_loop/runner.rs b/src/platform_impl/web/event_loop/runner.rs index 51457a8fbc..3e7507106d 100644 --- a/src/platform_impl/web/event_loop/runner.rs +++ b/src/platform_impl/web/event_loop/runner.rs @@ -370,7 +370,7 @@ impl Shared { } runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(unsafe { DeviceId::dummy() }), + device_id: RootDeviceId(DeviceId::dummy()), event: DeviceEvent::Key(RawKeyEvent { physical_key: backend::event::key_code(&event), state: ElementState::Pressed, @@ -388,7 +388,7 @@ impl Shared { } runner.send_event(Event::DeviceEvent { - device_id: RootDeviceId(unsafe { DeviceId::dummy() }), + device_id: RootDeviceId(DeviceId::dummy()), event: DeviceEvent::Key(RawKeyEvent { physical_key: backend::event::key_code(&event), state: ElementState::Released, diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index e27d024051..eb0051d36d 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -142,7 +142,7 @@ impl ActiveEventLoop { } }); - let device_id = RootDeviceId(unsafe { DeviceId::dummy() }); + let device_id = RootDeviceId(DeviceId::dummy()); runner.send_events( iter::once(Event::WindowEvent { @@ -178,7 +178,7 @@ impl ActiveEventLoop { } }); - let device_id = RootDeviceId(unsafe { DeviceId::dummy() }); + let device_id = RootDeviceId(DeviceId::dummy()); runner.send_events( iter::once(Event::WindowEvent { diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index acfefe3063..29ca730299 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -431,7 +431,7 @@ impl Drop for Inner { pub struct WindowId(pub(crate) u32); impl WindowId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { Self(0) } } diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index 15051f0ca3..9bb02fd4ed 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -67,7 +67,7 @@ unsafe impl Sync for PlatformSpecificWindowAttributes {} pub struct DeviceId(u32); impl DeviceId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { DeviceId(0) } } @@ -103,7 +103,7 @@ unsafe impl Send for WindowId {} unsafe impl Sync for WindowId {} impl WindowId { - pub const unsafe fn dummy() -> Self { + pub const fn dummy() -> Self { WindowId(0) } } diff --git a/src/window.rs b/src/window.rs index 775b3f5352..7c4812c560 100644 --- a/src/window.rs +++ b/src/window.rs @@ -71,16 +71,13 @@ pub struct WindowId(pub(crate) platform_impl::WindowId); impl WindowId { /// Returns a dummy id, useful for unit testing. /// - /// # Safety + /// # Notes /// /// The only guarantee made about the return value of this function is that /// it will always be equal to itself and to future values returned by this function. /// No other guarantees are made. This may be equal to a real [`WindowId`]. - /// - /// **Passing this into a winit function will result in undefined behavior.** - pub const unsafe fn dummy() -> Self { - #[allow(unused_unsafe)] - WindowId(unsafe { platform_impl::WindowId::dummy() }) + pub const fn dummy() -> Self { + WindowId(platform_impl::WindowId::dummy()) } }