From 2dbf7d07007674d9733aad349472a4a9fa5e45be Mon Sep 17 00:00:00 2001 From: jtnunley Date: Fri, 24 Mar 2023 04:49:26 -0700 Subject: [PATCH] Second look at HasWindowHandle trait --- src/borrowed.rs | 160 ++++++++++++++---------------------------------- 1 file changed, 47 insertions(+), 113 deletions(-) diff --git a/src/borrowed.rs b/src/borrowed.rs index 8563df0..98c1469 100644 --- a/src/borrowed.rs +++ b/src/borrowed.rs @@ -91,73 +91,30 @@ pub trait HasDisplayHandle { fn display_handle(&self) -> DisplayHandle<'_>; } -impl HasDisplayHandle for &T { - fn active(&self) -> Option> { - (**self).active() - } - - fn display_handle<'this, 'active>( - &'this self, - active: &'active Active<'_>, - ) -> DisplayHandle<'this> - where - 'active: 'this, - { - (**self).display_handle(active) +impl HasDisplayHandle for &H { + fn display_handle(&self) -> DisplayHandle<'_> { + (**self).display_handle() } } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] -impl HasDisplayHandle for alloc::boxed::Box { - fn active(&self) -> Option> { - (**self).active() - } - - fn display_handle<'this, 'active>( - &'this self, - active: &'active Active<'_>, - ) -> DisplayHandle<'this> - where - 'active: 'this, - { - (**self).display_handle(active) +impl HasDisplayHandle for alloc::boxed::Box { + fn display_handle(&self) -> DisplayHandle<'_> { + (**self).display_handle() } } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] -impl HasDisplayHandle for alloc::rc::Rc { - fn active(&self) -> Option> { - (**self).active() - } - - fn display_handle<'this, 'active>( - &'this self, - active: &'active Active<'_>, - ) -> DisplayHandle<'this> - where - 'active: 'this, - { - (**self).display_handle(active) +impl HasDisplayHandle for alloc::rc::Rc { + fn display_handle(&self) -> DisplayHandle<'_> { + (**self).display_handle() } } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] -impl HasDisplayHandle for alloc::sync::Arc { - fn active(&self) -> Option> { - (**self).active() - } - - fn display_handle<'this, 'active>( - &'this self, - active: &'active Active<'_>, - ) -> DisplayHandle<'this> - where - 'active: 'this, - { - (**self).display_handle(active) +impl HasDisplayHandle for alloc::sync::Arc { + fn display_handle(&self) -> DisplayHandle<'_> { + (**self).display_handle() } } @@ -239,68 +196,55 @@ impl<'a> HasDisplayHandle for DisplayHandle<'a> { /// constructors of [`WindowHandle`]. This is because the `HasWindowHandle` trait is safe to implement. pub trait HasWindowHandle { /// Get a handle to the window. - fn window_handle<'this, 'active>( - &'this self, - active: ActiveHandle<'active>, - ) -> WindowHandle<'this> - where - 'active: 'this; + fn window_handle(&self) -> Result, WindowHandleError>; } -impl HasWindowHandle for &T { - fn window_handle<'this, 'active>( - &'this self, - active: &'active Active<'_>, - ) -> WindowHandle<'this> - where - 'active: 'this, - { - (**self).window_handle(active) +impl HasWindowHandle for &H { + fn window_handle(&self) -> Result, WindowHandleError> { + (**self).window_handle() } } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] -impl HasWindowHandle for alloc::boxed::Box { - fn window_handle<'this, 'active>( - &'this self, - active: &'active Active<'_>, - ) -> WindowHandle<'this> - where - 'active: 'this, - { - (**self).window_handle(active) +impl HasWindowHandle for alloc::boxed::Box { + fn window_handle(&self) -> Result, WindowHandleError> { + (**self).window_handle() } } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] -impl HasWindowHandle for alloc::rc::Rc { - fn window_handle<'this, 'active>( - &'this self, - active: &'active Active<'_>, - ) -> WindowHandle<'this> - where - 'active: 'this, - { - (**self).window_handle(active) +impl HasWindowHandle for alloc::rc::Rc { + fn window_handle(&self) -> Result, WindowHandleError> { + (**self).window_handle() } } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] -impl HasWindowHandle for alloc::sync::Arc { - fn window_handle<'this, 'active>( - &'this self, - active: &'active Active<'_>, - ) -> WindowHandle<'this> - where - 'active: 'this, - { - (**self).window_handle(active) +impl HasWindowHandle for alloc::sync::Arc { + fn window_handle(&self) -> Result, WindowHandleError> { + (**self).window_handle() } } +/// The error type returned when a window handle cannot be obtained. +#[derive(Debug)] +#[non_exhaustive] +pub enum WindowHandleError { + /// The window is not currently active. + Inactive, +} + +impl fmt::Display for WindowHandleError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Inactive => write!(f, "the window is not currently active"), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for WindowHandleError {} + /// The handle to a window. /// /// This is the primary return type of the [`HasWindowHandle`] trait. All *pointers* within this type @@ -358,19 +302,9 @@ unsafe impl HasRawWindowHandle for WindowHandle<'_> { } } -impl<'a> HasWindowHandle for WindowHandle<'a> { - fn window_handle<'this, 'active>( - &'this self, - active: ActiveHandle<'active>, - ) -> WindowHandle<'this> - where - 'active: 'this, - { - WindowHandle { - raw: self.raw, - _active: active, - _marker: PhantomData, - } +impl HasWindowHandle for WindowHandle<'_> { + fn window_handle(&self) -> Result { + Ok(self.clone()) } }