-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make raw_*_handle return a result #122
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,8 +48,7 @@ pub use appkit::{AppKitDisplayHandle, AppKitWindowHandle}; | |
#[cfg(any(feature = "std", not(target_os = "android")))] | ||
#[allow(deprecated)] | ||
pub use borrowed::{ | ||
Active, ActiveHandle, DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, | ||
WindowHandle, | ||
Active, ActiveHandle, DisplayHandle, HasDisplayHandle, HasWindowHandle, WindowHandle, | ||
}; | ||
pub use haiku::{HaikuDisplayHandle, HaikuWindowHandle}; | ||
pub use redox::{OrbitalDisplayHandle, OrbitalWindowHandle}; | ||
|
@@ -61,6 +60,8 @@ pub use unix::{ | |
pub use web::{WebDisplayHandle, WebWindowHandle}; | ||
pub use windows::{Win32WindowHandle, WinRtWindowHandle, WindowsDisplayHandle}; | ||
|
||
use core::fmt; | ||
|
||
/// Window that wraps around a raw window handle. | ||
/// | ||
/// # Safety | ||
|
@@ -76,25 +77,25 @@ pub use windows::{Win32WindowHandle, WinRtWindowHandle, WindowsDisplayHandle}; | |
/// The exact handles returned by `raw_window_handle` must remain consistent between multiple calls | ||
/// to `raw_window_handle` as long as not indicated otherwise by platform specific events. | ||
pub unsafe trait HasRawWindowHandle { | ||
fn raw_window_handle(&self) -> RawWindowHandle; | ||
fn raw_window_handle(&self) -> Result<RawWindowHandle, HandleError>; | ||
} | ||
|
||
unsafe impl<'a, T: HasRawWindowHandle + ?Sized> HasRawWindowHandle for &'a T { | ||
fn raw_window_handle(&self) -> RawWindowHandle { | ||
fn raw_window_handle(&self) -> Result<RawWindowHandle, HandleError> { | ||
(*self).raw_window_handle() | ||
} | ||
} | ||
#[cfg(feature = "alloc")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] | ||
unsafe impl<T: HasRawWindowHandle + ?Sized> HasRawWindowHandle for alloc::rc::Rc<T> { | ||
fn raw_window_handle(&self) -> RawWindowHandle { | ||
fn raw_window_handle(&self) -> Result<RawWindowHandle, HandleError> { | ||
(**self).raw_window_handle() | ||
} | ||
} | ||
#[cfg(feature = "alloc")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] | ||
unsafe impl<T: HasRawWindowHandle + ?Sized> HasRawWindowHandle for alloc::sync::Arc<T> { | ||
fn raw_window_handle(&self) -> RawWindowHandle { | ||
fn raw_window_handle(&self) -> Result<RawWindowHandle, HandleError> { | ||
(**self).raw_window_handle() | ||
} | ||
} | ||
|
@@ -216,27 +217,27 @@ pub enum RawWindowHandle { | |
/// The exact handles returned by `raw_display_handle` must remain consistent between multiple calls | ||
/// to `raw_display_handle` as long as not indicated otherwise by platform specific events. | ||
pub unsafe trait HasRawDisplayHandle { | ||
fn raw_display_handle(&self) -> RawDisplayHandle; | ||
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError>; | ||
} | ||
|
||
unsafe impl<'a, T: HasRawDisplayHandle + ?Sized> HasRawDisplayHandle for &'a T { | ||
fn raw_display_handle(&self) -> RawDisplayHandle { | ||
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError> { | ||
(*self).raw_display_handle() | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] | ||
unsafe impl<T: HasRawDisplayHandle + ?Sized> HasRawDisplayHandle for alloc::rc::Rc<T> { | ||
fn raw_display_handle(&self) -> RawDisplayHandle { | ||
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError> { | ||
(**self).raw_display_handle() | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] | ||
unsafe impl<T: HasRawDisplayHandle + ?Sized> HasRawDisplayHandle for alloc::sync::Arc<T> { | ||
fn raw_display_handle(&self) -> RawDisplayHandle { | ||
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError> { | ||
(**self).raw_display_handle() | ||
} | ||
} | ||
|
@@ -344,6 +345,32 @@ pub enum RawDisplayHandle { | |
Haiku(HaikuDisplayHandle), | ||
} | ||
|
||
/// An error that can occur while fetching a display or window handle. | ||
#[derive(Debug, Clone)] | ||
#[non_exhaustive] | ||
pub enum HandleError { | ||
/// The underlying handle cannot be represented using the types in this crate. | ||
NotSupported, | ||
|
||
/// The underlying handle is not available. | ||
Unavailable, | ||
} | ||
|
||
impl fmt::Display for HandleError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::NotSupported => write!( | ||
f, | ||
"The underlying handle cannot be represented using the types in this crate" | ||
), | ||
Self::Unavailable => write!(f, "The underlying handle is not available"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a precedent for this? I've always started my error messages with a capital letter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well the |
||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl std::error::Error for HandleError {} | ||
|
||
macro_rules! from_impl { | ||
($($to:ident, $enum:ident, $from:ty)*) => ($( | ||
impl From<$from> for $to { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the use-case for this?
The use-case discussed in #104 would use
Unavailable
, since that's what most accurately describes their situation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the library can't provide a any handle for the current platform, but still somehow built for it. Or when multiple could be provided at the same time, like XLIB and XCB, but it was compiled without xlib, so it'll be not-available in generic cfg less code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that:
NotSupported
is when the underlying windowing system does not support returning any of the C window handles. For instance, if you're using a pure Rust implementation of X11 or Wayland, or if you're using something thatraw-window-handle
doesn't support (like a game console).Unavailable
is when the window is temporarily not available. The use case here is Android after a suspend event. There are probably others.Handling these two error conditions may require separate code for certain cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be cool to have some of those example uses in the docs