Skip to content

Commit

Permalink
chore: use our own AsAny to provide &dyn Any
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kchibisov authored Aug 11, 2024
1 parent 038ef5c commit 3392e9c
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 46 deletions.
16 changes: 9 additions & 7 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//!
//! See the root-level documentation for information on how to create and use an event loop to
//! handle events.
use std::any::Any;
use std::fmt;
use std::marker::PhantomData;
#[cfg(any(x11_platform, wayland_platform))]
Expand All @@ -24,6 +23,7 @@ use crate::application::ApplicationHandler;
use crate::error::{EventLoopError, ExternalError, OsError};
use crate::monitor::MonitorHandle;
use crate::platform_impl;
use crate::utils::AsAny;
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
Expand Down Expand Up @@ -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
/// to the main event loop, possibly from another thread.
fn create_proxy(&self) -> EventLoopProxy;
Expand Down Expand Up @@ -403,16 +403,18 @@ pub trait ActiveEventLoop {
/// See the [`OwnedDisplayHandle`] type for more information.
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.
#[cfg(feature = "rwh_06")]
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.
///
/// The purpose of this type is to provide a cheaply clonable handle to the underlying
Expand Down
5 changes: 0 additions & 5 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::Cell;
use std::hash::Hash;
use std::num::{NonZeroU16, NonZeroU32};
Expand Down Expand Up @@ -640,10 +639,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
}

fn as_any(&self) -> &dyn Any {
self
}

#[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
Expand Down
4 changes: 0 additions & 4 deletions src/platform_impl/apple/appkit/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
}

fn as_any(&self) -> &dyn Any {
self
}

#[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
Expand Down
5 changes: 0 additions & 5 deletions src/platform_impl/apple/uikit/event_loop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use std::ffi::{c_char, c_int, c_void};
use std::ptr::{self, NonNull};
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
Expand Down Expand Up @@ -94,10 +93,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
}

fn as_any(&self) -> &dyn Any {
self
}

#[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
Expand Down
6 changes: 0 additions & 6 deletions src/platform_impl/linux/wayland/event_loop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! The event-loop routines.

use std::any::Any;
use std::cell::{Cell, RefCell};
use std::io::Result as IOResult;
use std::mem;
Expand Down Expand Up @@ -658,11 +657,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
}
}

#[inline(always)]
fn as_any(&self) -> &dyn Any {
self
}

#[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
Expand Down
5 changes: 0 additions & 5 deletions src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet, VecDeque};
use std::ffi::CStr;
Expand Down Expand Up @@ -751,10 +750,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: handle }
}

fn as_any(&self) -> &dyn Any {
self
}

#[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
Expand Down
5 changes: 0 additions & 5 deletions src/platform_impl/orbital/event_loop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::Cell;
use std::collections::VecDeque;
use std::sync::{mpsc, Arc, Mutex};
Expand Down Expand Up @@ -778,10 +777,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
event_loop::OwnedDisplayHandle { platform: OwnedDisplayHandle }
}

fn as_any(&self) -> &dyn Any {
self
}

#[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
Expand Down
5 changes: 0 additions & 5 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::any::Any;
use std::cell::Cell;
use std::clone::Clone;
use std::iter;
Expand Down Expand Up @@ -693,10 +692,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
}

fn as_any(&self) -> &dyn Any {
self
}

#[cfg(feature = "rwh_06")]
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
Expand Down
4 changes: 0 additions & 4 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
RootOwnedDisplayHandle { platform: OwnedDisplayHandle }
}

fn as_any(&self) -> &dyn std::any::Any {
self
}

fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
self
}
Expand Down
18 changes: 18 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// This isn't used on every platform, which can come up as dead code warnings.
#![allow(dead_code)]

use std::any::Any;
use std::ops::Deref;
use std::sync::OnceLock;

Expand All @@ -26,3 +27,20 @@ impl<T> Deref for Lazy<T> {
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
}
}

0 comments on commit 3392e9c

Please sign in to comment.