Skip to content

Commit

Permalink
Second look at HasWindowHandle trait
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Mar 24, 2023
1 parent 41f8480 commit 65a9d76
Showing 1 changed file with 77 additions and 19 deletions.
96 changes: 77 additions & 19 deletions src/borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ pub trait HasDisplayHandle {
fn display_handle(&self) -> DisplayHandle<'_>;
}

impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for &H {
fn display_handle(&self) -> DisplayHandle<'_> {
(**self).display_handle()
}
}

#[cfg(feature = "alloc")]
impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for alloc::boxed::Box<H> {
fn display_handle(&self) -> DisplayHandle<'_> {
(**self).display_handle()
}
}

#[cfg(feature = "alloc")]
impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for alloc::rc::Rc<H> {
fn display_handle(&self) -> DisplayHandle<'_> {
(**self).display_handle()
}
}

#[cfg(feature = "alloc")]
impl<H: HasDisplayHandle + ?Sized> HasDisplayHandle for alloc::sync::Arc<H> {
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
Expand Down Expand Up @@ -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<WindowHandle<'_>, WindowHandleError>;
}

impl<H: HasWindowHandle + ?Sized> HasWindowHandle for &H {
fn window_handle(&self) -> Result<WindowHandle<'_>, WindowHandleError> {
(**self).window_handle()
}
}

#[cfg(feature = "alloc")]
impl<H: HasWindowHandle + ?Sized> HasWindowHandle for alloc::boxed::Box<H> {
fn window_handle(&self) -> Result<WindowHandle<'_>, WindowHandleError> {
(**self).window_handle()
}
}

#[cfg(feature = "alloc")]
impl<H: HasWindowHandle + ?Sized> HasWindowHandle for alloc::rc::Rc<H> {
fn window_handle(&self) -> Result<WindowHandle<'_>, WindowHandleError> {
(**self).window_handle()
}
}

#[cfg(feature = "alloc")]
impl<H: HasWindowHandle + ?Sized> HasWindowHandle for alloc::sync::Arc<H> {
fn window_handle(&self) -> Result<WindowHandle<'_>, 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
Expand Down Expand Up @@ -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<Self, WindowHandleError> {
Ok(self.clone())
}
}

Expand Down

0 comments on commit 65a9d76

Please sign in to comment.