Skip to content
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

Safe constructors for some display handles #159

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/android.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::ffi::c_void;
use core::ptr::NonNull;

use super::DisplayHandle;

/// Raw display handle for Android.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -21,6 +23,26 @@ impl AndroidDisplayHandle {
}
}

impl DisplayHandle<'static> {
/// Create an Android-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::android();
/// do_something(handle);
/// ```
pub fn android() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(AndroidDisplayHandle::new().into()) }
}
}

/// Raw window handle for Android NDK.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
22 changes: 22 additions & 0 deletions src/appkit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::ffi::c_void;
use core::ptr::NonNull;

use super::DisplayHandle;

/// Raw display handle for AppKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -21,6 +23,26 @@ impl AppKitDisplayHandle {
}
}

impl DisplayHandle<'static> {
/// Create an AppKit-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::appkit();
/// do_something(handle);
/// ```
pub fn appkit() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(AppKitDisplayHandle::new().into()) }
}
}

/// Raw window handle for AppKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
22 changes: 22 additions & 0 deletions src/haiku.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::ffi::c_void;
use core::ptr::NonNull;

use super::DisplayHandle;

/// Raw display handle for Haiku.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -21,6 +23,26 @@ impl HaikuDisplayHandle {
}
}

impl DisplayHandle<'static> {
/// Create an Haiku-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::haiku();
/// do_something(handle);
/// ```
pub fn haiku() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(HaikuDisplayHandle::new().into()) }
}
}

/// Raw window handle for Haiku.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
22 changes: 22 additions & 0 deletions src/redox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::ffi::c_void;
use core::ptr::NonNull;

use super::DisplayHandle;

/// Raw display handle for the Redox operating system.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -21,6 +23,26 @@ impl OrbitalDisplayHandle {
}
}

impl DisplayHandle<'static> {
/// Create an Orbital-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::orbital();
/// do_something(handle);
/// ```
pub fn orbital() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(OrbitalDisplayHandle::new().into()) }
}
}

/// Raw window handle for the Redox operating system.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
22 changes: 22 additions & 0 deletions src/uikit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::ffi::c_void;
use core::ptr::NonNull;

use super::DisplayHandle;

/// Raw display handle for UIKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -21,6 +23,26 @@ impl UiKitDisplayHandle {
}
}

impl DisplayHandle<'static> {
/// Create a UiKit-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::uikit();
/// do_something(handle);
/// ```
pub fn uikit() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(UiKitDisplayHandle::new().into()) }
}
}

/// Raw window handle for UIKit.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
22 changes: 22 additions & 0 deletions src/web.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::ffi::c_void;
use core::ptr::NonNull;

use super::DisplayHandle;

/// Raw display handle for the Web.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -21,6 +23,26 @@ impl WebDisplayHandle {
}
}

impl DisplayHandle<'static> {
/// Create a Web-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::web();
/// do_something(handle);
/// ```
pub fn web() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(WebDisplayHandle::new().into()) }
}
}

/// Raw window handle for the Web.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
22 changes: 22 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use core::ffi::c_void;
use core::num::NonZeroIsize;
use core::ptr::NonNull;

use super::DisplayHandle;

/// Raw display handle for Windows.
///
/// It can be used regardless of Windows window backend.
Expand All @@ -24,6 +26,26 @@ impl WindowsDisplayHandle {
}
}

impl DisplayHandle<'static> {
/// Create a Windows-based display handle.
///
/// As no data is borrowed by this handle, it is completely safe to create. This function
/// may be useful to windowing framework implementations that want to avoid unsafe code.
///
/// # Example
///
/// ```
/// # use raw_window_handle::{DisplayHandle, HasDisplayHandle};
/// # fn do_something(rwh: impl HasDisplayHandle) { let _ = rwh; }
/// let handle = DisplayHandle::windows();
/// do_something(handle);
/// ```
pub fn windows() -> Self {
// SAFETY: No data is borrowed.
unsafe { Self::borrow_raw(WindowsDisplayHandle::new().into()) }
}
}

/// Raw window handle for Win32.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
Loading