Make DeviceId/WindowId::dummy() safe (#3784)

This commit is contained in:
daxpedda
2024-07-14 13:14:32 +02:00
committed by GitHub
parent bf97def398
commit 5b8f5cb54a
15 changed files with 28 additions and 33 deletions

View File

@@ -68,6 +68,7 @@ changelog entry.
- Change signature of `EventLoop::run_app`, `EventLoopExtPumpEvents::pump_app_events` and - Change signature of `EventLoop::run_app`, `EventLoopExtPumpEvents::pump_app_events` and
`EventLoopExtRunOnDemand::run_app_on_demand` to accept a `impl ApplicationHandler` directly, `EventLoopExtRunOnDemand::run_app_on_demand` to accept a `impl ApplicationHandler` directly,
instead of requiring a `&mut` reference to it. instead of requiring a `&mut` reference to it.
- `DeviceId::dummy()` and `WindowId::dummy()` are no longer marked `unsafe`.
### Removed ### Removed

View File

@@ -440,16 +440,13 @@ pub struct DeviceId(pub(crate) platform_impl::DeviceId);
impl DeviceId { impl DeviceId {
/// Returns a dummy id, useful for unit testing. /// Returns a dummy id, useful for unit testing.
/// ///
/// # Safety /// # Notes
/// ///
/// The only guarantee made about the return value of this function is that /// 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. /// 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`. /// No other guarantees are made. This may be equal to a real `DeviceId`.
/// pub const fn dummy() -> Self {
/// **Passing this into a winit function will result in undefined behavior.** DeviceId(platform_impl::DeviceId::dummy())
pub const unsafe fn dummy() -> Self {
#[allow(unused_unsafe)]
DeviceId(unsafe { platform_impl::DeviceId::dummy() })
} }
} }
@@ -1013,7 +1010,7 @@ mod tests {
($closure:expr) => {{ ($closure:expr) => {{
#[allow(unused_mut)] #[allow(unused_mut)]
let mut x = $closure; let mut x = $closure;
let did = unsafe { event::DeviceId::dummy() }; let did = event::DeviceId::dummy();
#[allow(deprecated)] #[allow(deprecated)]
{ {
@@ -1023,7 +1020,7 @@ mod tests {
use crate::window::WindowId; use crate::window::WindowId;
// Mainline events. // Mainline events.
let wid = unsafe { WindowId::dummy() }; let wid = WindowId::dummy();
x(NewEvents(event::StartCause::Init)); x(NewEvents(event::StartCause::Init));
x(AboutToWait); x(AboutToWait);
x(LoopExiting); x(LoopExiting);
@@ -1138,7 +1135,7 @@ mod tests {
}); });
let _ = event::StartCause::Init.clone(); let _ = event::StartCause::Init.clone();
let did = unsafe { crate::event::DeviceId::dummy() }.clone(); let did = crate::event::DeviceId::dummy().clone();
HashSet::new().insert(did); HashSet::new().insert(did);
let mut set = [did, did, did]; let mut set = [did, did, did];
set.sort_unstable(); set.sort_unstable();

View File

@@ -35,7 +35,7 @@ pub(crate) use crate::platform_impl::Fullscreen;
pub struct DeviceId; pub struct DeviceId;
impl DeviceId { impl DeviceId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
DeviceId DeviceId
} }
} }

View File

@@ -74,7 +74,7 @@ impl Window {
pub struct WindowId(pub usize); pub struct WindowId(pub usize);
impl WindowId { impl WindowId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
Self(0) Self(0)
} }
} }

View File

@@ -31,7 +31,7 @@ pub(crate) use crate::platform_impl::Fullscreen;
pub struct DeviceId; pub struct DeviceId;
impl DeviceId { impl DeviceId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
DeviceId DeviceId
} }
} }

View File

@@ -699,7 +699,7 @@ pub struct WindowId {
} }
impl WindowId { impl WindowId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
WindowId { window: std::ptr::null_mut() } WindowId { window: std::ptr::null_mut() }
} }
} }

View File

@@ -155,7 +155,7 @@ impl From<u64> for WindowId {
} }
impl WindowId { impl WindowId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
Self(0) Self(0)
} }
} }
@@ -169,11 +169,11 @@ pub enum DeviceId {
} }
impl DeviceId { impl DeviceId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
#[cfg(wayland_platform)] #[cfg(wayland_platform)]
return DeviceId::Wayland(unsafe { wayland::DeviceId::dummy() }); return DeviceId::Wayland(wayland::DeviceId::dummy());
#[cfg(all(not(wayland_platform), x11_platform))] #[cfg(all(not(wayland_platform), x11_platform))]
return DeviceId::X(unsafe { x11::DeviceId::dummy() }); return DeviceId::X(x11::DeviceId::dummy());
} }
} }

View File

@@ -66,7 +66,7 @@ impl From<WaylandError> for OsError {
pub struct DeviceId; pub struct DeviceId;
impl DeviceId { impl DeviceId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
DeviceId DeviceId
} }
} }

View File

@@ -768,7 +768,7 @@ pub struct DeviceId(xinput::DeviceId);
impl DeviceId { impl DeviceId {
#[allow(unused)] #[allow(unused)]
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
DeviceId(0) DeviceId(0)
} }
} }

View File

@@ -2,7 +2,7 @@
pub struct DeviceId(pub i32); pub struct DeviceId(pub i32);
impl DeviceId { impl DeviceId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
Self(0) Self(0)
} }
} }

View File

@@ -369,7 +369,7 @@ impl Shared {
} }
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(unsafe { DeviceId::dummy() }), device_id: RootDeviceId(DeviceId::dummy()),
event: DeviceEvent::Key(RawKeyEvent { event: DeviceEvent::Key(RawKeyEvent {
physical_key: backend::event::key_code(&event), physical_key: backend::event::key_code(&event),
state: ElementState::Pressed, state: ElementState::Pressed,
@@ -387,7 +387,7 @@ impl Shared {
} }
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(unsafe { DeviceId::dummy() }), device_id: RootDeviceId(DeviceId::dummy()),
event: DeviceEvent::Key(RawKeyEvent { event: DeviceEvent::Key(RawKeyEvent {
physical_key: backend::event::key_code(&event), physical_key: backend::event::key_code(&event),
state: ElementState::Released, state: ElementState::Released,

View File

@@ -150,7 +150,7 @@ impl ActiveEventLoop {
} }
}); });
let device_id = RootDeviceId(unsafe { DeviceId::dummy() }); let device_id = RootDeviceId(DeviceId::dummy());
runner.send_events( runner.send_events(
iter::once(Event::WindowEvent { iter::once(Event::WindowEvent {
@@ -186,7 +186,7 @@ impl ActiveEventLoop {
} }
}); });
let device_id = RootDeviceId(unsafe { DeviceId::dummy() }); let device_id = RootDeviceId(DeviceId::dummy());
runner.send_events( runner.send_events(
iter::once(Event::WindowEvent { iter::once(Event::WindowEvent {

View File

@@ -431,7 +431,7 @@ impl Drop for Inner {
pub struct WindowId(pub(crate) u32); pub struct WindowId(pub(crate) u32);
impl WindowId { impl WindowId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
Self(0) Self(0)
} }
} }

View File

@@ -64,7 +64,7 @@ unsafe impl Sync for PlatformSpecificWindowAttributes {}
pub struct DeviceId(u32); pub struct DeviceId(u32);
impl DeviceId { impl DeviceId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
DeviceId(0) DeviceId(0)
} }
} }
@@ -100,7 +100,7 @@ unsafe impl Send for WindowId {}
unsafe impl Sync for WindowId {} unsafe impl Sync for WindowId {}
impl WindowId { impl WindowId {
pub const unsafe fn dummy() -> Self { pub const fn dummy() -> Self {
WindowId(0) WindowId(0)
} }
} }

View File

@@ -70,16 +70,13 @@ pub struct WindowId(pub(crate) platform_impl::WindowId);
impl WindowId { impl WindowId {
/// Returns a dummy id, useful for unit testing. /// Returns a dummy id, useful for unit testing.
/// ///
/// # Safety /// # Notes
/// ///
/// The only guarantee made about the return value of this function is that /// 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. /// 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`]. /// No other guarantees are made. This may be equal to a real [`WindowId`].
/// pub const fn dummy() -> Self {
/// **Passing this into a winit function will result in undefined behavior.** WindowId(platform_impl::WindowId::dummy())
pub const unsafe fn dummy() -> Self {
#[allow(unused_unsafe)]
WindowId(unsafe { platform_impl::WindowId::dummy() })
} }
} }