diff --git a/src/borrowed.rs b/src/borrowed.rs index b05c78e..98c1469 100644 --- a/src/borrowed.rs +++ b/src/borrowed.rs @@ -91,6 +91,33 @@ pub trait HasDisplayHandle { fn display_handle(&self) -> DisplayHandle<'_>; } +impl HasDisplayHandle for &H { + fn display_handle(&self) -> DisplayHandle<'_> { + (**self).display_handle() + } +} + +#[cfg(feature = "alloc")] +impl HasDisplayHandle for alloc::boxed::Box { + fn display_handle(&self) -> DisplayHandle<'_> { + (**self).display_handle() + } +} + +#[cfg(feature = "alloc")] +impl HasDisplayHandle for alloc::rc::Rc { + fn display_handle(&self) -> DisplayHandle<'_> { + (**self).display_handle() + } +} + +#[cfg(feature = "alloc")] +impl HasDisplayHandle for alloc::sync::Arc { + fn display_handle(&self) -> DisplayHandle<'_> { + (**self).display_handle() + } +} + /// The handle to the display controller of the windowing system. /// /// This is the primary return type of the [`HasDisplayHandle`] trait. It is guaranteed to contain @@ -169,14 +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 &H { + fn window_handle(&self) -> Result, WindowHandleError> { + (**self).window_handle() + } +} + +#[cfg(feature = "alloc")] +impl HasWindowHandle for alloc::boxed::Box { + fn window_handle(&self) -> Result, WindowHandleError> { + (**self).window_handle() + } +} + +#[cfg(feature = "alloc")] +impl HasWindowHandle for alloc::rc::Rc { + fn window_handle(&self) -> Result, WindowHandleError> { + (**self).window_handle() + } +} + +#[cfg(feature = "alloc")] +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 @@ -234,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()) } }