chore: use our own AsAny to provide &dyn Any

This removes the direct requirement to implement `as_any` and it could
be just derived.

Also implement HasDisplayHandle for dyn ActiveEventLoop + '_.

Suggested-by: daxpedda <daxpedda@gmail.com>
This commit is contained in:
Kirill Chibisov
2024-08-11 21:24:34 +03:00
committed by GitHub
parent 038ef5c3ad
commit 3392e9c1de
10 changed files with 27 additions and 46 deletions

View File

@@ -8,7 +8,6 @@
//! //!
//! See the root-level documentation for information on how to create and use an event loop to //! See the root-level documentation for information on how to create and use an event loop to
//! handle events. //! handle events.
use std::any::Any;
use std::fmt; use std::fmt;
use std::marker::PhantomData; use std::marker::PhantomData;
#[cfg(any(x11_platform, wayland_platform))] #[cfg(any(x11_platform, wayland_platform))]
@@ -24,6 +23,7 @@ use crate::application::ApplicationHandler;
use crate::error::{EventLoopError, ExternalError, OsError}; use crate::error::{EventLoopError, ExternalError, OsError};
use crate::monitor::MonitorHandle; use crate::monitor::MonitorHandle;
use crate::platform_impl; use crate::platform_impl;
use crate::utils::AsAny;
use crate::window::{CustomCursor, CustomCursorSource, Theme, Window, WindowAttributes}; use crate::window::{CustomCursor, CustomCursorSource, Theme, Window, WindowAttributes};
/// Provides a way to retrieve events from the system and from the windows that were registered to /// Provides a way to retrieve events from the system and from the windows that were registered to
@@ -308,7 +308,7 @@ impl AsRawFd for EventLoop {
} }
} }
pub trait ActiveEventLoop { pub trait ActiveEventLoop: AsAny {
/// Creates an [`EventLoopProxy`] that can be used to dispatch user events /// Creates an [`EventLoopProxy`] that can be used to dispatch user events
/// to the main event loop, possibly from another thread. /// to the main event loop, possibly from another thread.
fn create_proxy(&self) -> EventLoopProxy; fn create_proxy(&self) -> EventLoopProxy;
@@ -403,16 +403,18 @@ pub trait ActiveEventLoop {
/// See the [`OwnedDisplayHandle`] type for more information. /// See the [`OwnedDisplayHandle`] type for more information.
fn owned_display_handle(&self) -> OwnedDisplayHandle; fn owned_display_handle(&self) -> OwnedDisplayHandle;
/// Get the [`ActiveEventLoop`] as [`Any`].
///
/// This is useful for downcasting to a concrete event loop type.
fn as_any(&self) -> &dyn Any;
/// Get the raw-window-handle handle. /// Get the raw-window-handle handle.
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle; fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle;
} }
#[cfg(feature = "rwh_06")]
impl rwh_06::HasDisplayHandle for dyn ActiveEventLoop + '_ {
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
self.rwh_06_handle().display_handle()
}
}
/// A proxy for the underlying display handle. /// A proxy for the underlying display handle.
/// ///
/// The purpose of this type is to provide a cheaply clonable handle to the underlying /// The purpose of this type is to provide a cheaply clonable handle to the underlying

View File

@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::Cell; use std::cell::Cell;
use std::hash::Hash; use std::hash::Hash;
use std::num::{NonZeroU16, NonZeroU32}; use std::num::{NonZeroU16, NonZeroU32};
@@ -640,10 +639,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle } RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
} }
fn as_any(&self) -> &dyn Any {
self
}
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self self

View File

@@ -159,10 +159,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle } RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
} }
fn as_any(&self) -> &dyn Any {
self
}
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self self

View File

@@ -1,4 +1,3 @@
use std::any::Any;
use std::ffi::{c_char, c_int, c_void}; use std::ffi::{c_char, c_int, c_void};
use std::ptr::{self, NonNull}; use std::ptr::{self, NonNull};
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering}; use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
@@ -94,10 +93,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle } RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
} }
fn as_any(&self) -> &dyn Any {
self
}
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self self

View File

@@ -1,6 +1,5 @@
//! The event-loop routines. //! The event-loop routines.
use std::any::Any;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::io::Result as IOResult; use std::io::Result as IOResult;
use std::mem; use std::mem;
@@ -658,11 +657,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
} }
} }
#[inline(always)]
fn as_any(&self) -> &dyn Any {
self
}
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self self

View File

@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
use std::ffi::CStr; use std::ffi::CStr;
@@ -751,10 +750,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: handle } RootOwnedDisplayHandle { platform: handle }
} }
fn as_any(&self) -> &dyn Any {
self
}
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self self

View File

@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::Cell; use std::cell::Cell;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::sync::{mpsc, Arc, Mutex}; use std::sync::{mpsc, Arc, Mutex};
@@ -778,10 +777,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
event_loop::OwnedDisplayHandle { platform: OwnedDisplayHandle } event_loop::OwnedDisplayHandle { platform: OwnedDisplayHandle }
} }
fn as_any(&self) -> &dyn Any {
self
}
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self self

View File

@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::Cell; use std::cell::Cell;
use std::clone::Clone; use std::clone::Clone;
use std::iter; use std::iter;
@@ -693,10 +692,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle } RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
} }
fn as_any(&self) -> &dyn Any {
self
}
#[cfg(feature = "rwh_06")] #[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self self

View File

@@ -570,10 +570,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle } RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
} }
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self self
} }

View File

@@ -4,6 +4,7 @@
// This isn't used on every platform, which can come up as dead code warnings. // This isn't used on every platform, which can come up as dead code warnings.
#![allow(dead_code)] #![allow(dead_code)]
use std::any::Any;
use std::ops::Deref; use std::ops::Deref;
use std::sync::OnceLock; use std::sync::OnceLock;
@@ -26,3 +27,20 @@ impl<T> Deref for Lazy<T> {
self.cell.get_or_init(self.init) self.cell.get_or_init(self.init)
} }
} }
pub trait AsAny {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
impl<T: Any> AsAny for T {
#[inline(always)]
fn as_any(&self) -> &dyn Any {
self
}
#[inline(always)]
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}