Skip to content

Commit bb7ffaf

Browse files
committed
Make handle fields private
This allows structs to define additional safety invariants (which would otherwise be trivial to override by modifying the field). This allows done one of: - Adding reference-counting to platform handles. - Combining `WindowHandle<'_>` and `RawWindowHandle`. Depending on what we want to do in the future.
1 parent 3f360ef commit bb7ffaf

13 files changed

Lines changed: 333 additions & 144 deletions

File tree

src/android.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use core::ptr::NonNull;
44
use super::DisplayHandle;
55

66
/// Raw display handle for Android.
7-
#[non_exhaustive]
87
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9-
pub struct AndroidDisplayHandle {}
8+
pub struct AndroidDisplayHandle(());
109

1110
impl AndroidDisplayHandle {
1211
/// Create a new empty display handle.
@@ -19,7 +18,7 @@ impl AndroidDisplayHandle {
1918
/// let handle = AndroidDisplayHandle::new();
2019
/// ```
2120
pub fn new() -> Self {
22-
Self {}
21+
Self(())
2322
}
2423
}
2524

@@ -44,17 +43,14 @@ impl DisplayHandle<'static> {
4443
}
4544

4645
/// Raw window handle for Android NDK.
47-
#[non_exhaustive]
4846
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4947
pub struct AndroidNdkWindowHandle {
50-
/// A pointer to an `ANativeWindow`.
51-
pub a_native_window: NonNull<c_void>,
48+
a_native_window: NonNull<c_void>,
5249
}
5350

5451
impl AndroidNdkWindowHandle {
5552
/// Create a new handle to an `ANativeWindow`.
5653
///
57-
///
5854
/// # Example
5955
///
6056
/// ```
@@ -69,4 +65,21 @@ impl AndroidNdkWindowHandle {
6965
pub fn new(a_native_window: NonNull<c_void>) -> Self {
7066
Self { a_native_window }
7167
}
68+
69+
/// A pointer to an `ANativeWindow`.
70+
///
71+
/// # Example
72+
///
73+
/// ```
74+
/// # use core::ptr::NonNull;
75+
/// # use raw_window_handle::AndroidNdkWindowHandle;
76+
/// # type ANativeWindow = ();
77+
/// #
78+
/// # let handle = AndroidNdkWindowHandle::new(NonNull::dangling());
79+
/// let ptr = handle.a_native_window();
80+
/// let ptr = ptr.cast::<ANativeWindow>();
81+
/// ```
82+
pub fn a_native_window(&self) -> NonNull<c_void> {
83+
self.a_native_window
84+
}
7285
}

src/appkit.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use core::ptr::NonNull;
44
use super::DisplayHandle;
55

66
/// Raw display handle for AppKit.
7-
#[non_exhaustive]
87
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9-
pub struct AppKitDisplayHandle {}
8+
pub struct AppKitDisplayHandle(());
109

1110
impl AppKitDisplayHandle {
1211
/// Create a new empty display handle.
@@ -19,7 +18,7 @@ impl AppKitDisplayHandle {
1918
/// let handle = AppKitDisplayHandle::new();
2019
/// ```
2120
pub fn new() -> Self {
22-
Self {}
21+
Self(())
2322
}
2423
}
2524

@@ -70,7 +69,7 @@ impl DisplayHandle<'static> {
7069
/// # #[cfg(requires_objc2)]
7170
/// RawWindowHandle::AppKit(handle) => {
7271
/// assert!(MainThreadMarker::new().is_some(), "can only access AppKit handles on the main thread");
73-
/// let ns_view = handle.ns_view.as_ptr();
72+
/// let ns_view = handle.ns_view().as_ptr();
7473
/// // SAFETY: The pointer came from `WindowHandle`, which ensures
7574
/// // that the `AppKitWindowHandle` contains a valid pointer to an
7675
/// // `NSView`.
@@ -83,11 +82,9 @@ impl DisplayHandle<'static> {
8382
/// }
8483
/// # }
8584
/// ```
86-
#[non_exhaustive]
8785
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8886
pub struct AppKitWindowHandle {
89-
/// A pointer to an `NSView` object.
90-
pub ns_view: NonNull<c_void>,
87+
ns_view: NonNull<c_void>,
9188
}
9289

9390
impl AppKitWindowHandle {
@@ -112,4 +109,9 @@ impl AppKitWindowHandle {
112109
pub fn new(ns_view: NonNull<c_void>) -> Self {
113110
Self { ns_view }
114111
}
112+
113+
/// A pointer to an `NSView` object.
114+
pub fn ns_view(&self) -> NonNull<c_void> {
115+
self.ns_view
116+
}
115117
}

src/drm.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/// Raw display handle for the Linux Kernel Mode Set/Direct Rendering Manager.
2-
#[non_exhaustive]
32
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
43
pub struct DrmDisplayHandle {
5-
/// The drm file descriptor.
64
// TODO: Use `std::os::fd::RawFd`?
7-
pub fd: i32,
5+
fd: i32,
86
}
97

108
impl DrmDisplayHandle {
@@ -23,14 +21,17 @@ impl DrmDisplayHandle {
2321
pub fn new(fd: i32) -> Self {
2422
Self { fd }
2523
}
24+
25+
/// The drm file descriptor.
26+
pub fn fd(&self) -> i32 {
27+
self.fd
28+
}
2629
}
2730

2831
/// Raw window handle for the Linux Kernel Mode Set/Direct Rendering Manager.
29-
#[non_exhaustive]
3032
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3133
pub struct DrmWindowHandle {
32-
/// The primary drm plane handle.
33-
pub plane: u32,
34+
plane: u32,
3435
}
3536

3637
impl DrmWindowHandle {
@@ -49,4 +50,9 @@ impl DrmWindowHandle {
4950
pub fn new(plane: u32) -> Self {
5051
Self { plane }
5152
}
53+
54+
/// The primary drm plane handle.
55+
pub fn plane(&self) -> u32 {
56+
self.plane
57+
}
5258
}

src/gbm.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use core::ffi::c_void;
22
use core::ptr::NonNull;
33

44
/// Raw display handle for the Linux Generic Buffer Manager.
5-
#[non_exhaustive]
65
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
76
pub struct GbmDisplayHandle {
8-
/// The gbm device.
9-
pub gbm_device: NonNull<c_void>,
7+
gbm_device: NonNull<c_void>,
108
}
119

1210
impl GbmDisplayHandle {
@@ -27,14 +25,17 @@ impl GbmDisplayHandle {
2725
pub fn new(gbm_device: NonNull<c_void>) -> Self {
2826
Self { gbm_device }
2927
}
28+
29+
/// The gbm device.
30+
pub fn gbm_device(&self) -> NonNull<c_void> {
31+
self.gbm_device
32+
}
3033
}
3134

3235
/// Raw window handle for the Linux Generic Buffer Manager.
33-
#[non_exhaustive]
3436
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3537
pub struct GbmWindowHandle {
36-
/// The gbm surface.
37-
pub gbm_surface: NonNull<c_void>,
38+
gbm_surface: NonNull<c_void>,
3839
}
3940

4041
impl GbmWindowHandle {
@@ -55,4 +56,9 @@ impl GbmWindowHandle {
5556
pub fn new(gbm_surface: NonNull<c_void>) -> Self {
5657
Self { gbm_surface }
5758
}
59+
60+
/// The gbm surface.
61+
pub fn gbm_surface(&self) -> NonNull<c_void> {
62+
self.gbm_surface
63+
}
5864
}

src/haiku.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use core::ptr::NonNull;
44
use super::DisplayHandle;
55

66
/// Raw display handle for Haiku.
7-
#[non_exhaustive]
87
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9-
pub struct HaikuDisplayHandle {}
8+
pub struct HaikuDisplayHandle(());
109

1110
impl HaikuDisplayHandle {
1211
/// Create a new empty display handle.
@@ -19,7 +18,7 @@ impl HaikuDisplayHandle {
1918
/// let handle = HaikuDisplayHandle::new();
2019
/// ```
2120
pub fn new() -> Self {
22-
Self {}
21+
Self(())
2322
}
2423
}
2524

@@ -44,19 +43,15 @@ impl DisplayHandle<'static> {
4443
}
4544

4645
/// Raw window handle for Haiku.
47-
#[non_exhaustive]
4846
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4947
pub struct HaikuWindowHandle {
50-
/// A pointer to a BWindow object
51-
pub b_window: NonNull<c_void>,
52-
/// A pointer to a BDirectWindow object that might be null
53-
pub b_direct_window: Option<NonNull<c_void>>,
48+
b_window: NonNull<c_void>,
49+
b_direct_window: Option<NonNull<c_void>>,
5450
}
5551

5652
impl HaikuWindowHandle {
5753
/// Create a new handle to a window.
5854
///
59-
///
6055
/// # Example
6156
///
6257
/// ```
@@ -66,14 +61,45 @@ impl HaikuWindowHandle {
6661
/// #
6762
/// let b_window: NonNull<BWindow>;
6863
/// # b_window = NonNull::from(&());
69-
/// let mut handle = HaikuWindowHandle::new(b_window.cast());
70-
/// // Optionally set `b_direct_window`.
71-
/// handle.b_direct_window = None;
64+
/// let handle = HaikuWindowHandle::new(b_window.cast());
7265
/// ```
7366
pub fn new(b_window: NonNull<c_void>) -> Self {
7467
Self {
7568
b_window,
7669
b_direct_window: None,
7770
}
7871
}
72+
73+
/// Create a new window handle together with a `BDirectWindow`.
74+
///
75+
/// # Example
76+
///
77+
/// ```
78+
/// # use core::ptr::NonNull;
79+
/// # use raw_window_handle::HaikuWindowHandle;
80+
/// # type BWindow = ();
81+
/// # type BDirectWindow = ();
82+
/// #
83+
/// let b_window: NonNull<BWindow>;
84+
/// let b_direct_window: NonNull<BDirectWindow>;
85+
/// # b_window = NonNull::dangling();
86+
/// # b_direct_window = NonNull::dangling();
87+
/// let handle = HaikuWindowHandle::with_window(b_window.cast(), b_direct_window.cast());
88+
/// ```
89+
pub fn with_window(b_window: NonNull<c_void>, b_direct_window: NonNull<c_void>) -> Self {
90+
Self {
91+
b_window,
92+
b_direct_window: Some(b_direct_window),
93+
}
94+
}
95+
96+
/// A pointer to a BWindow object.
97+
pub fn b_window(&self) -> NonNull<c_void> {
98+
self.b_window
99+
}
100+
101+
/// A pointer to a BDirectWindow object that might be null.
102+
pub fn b_direct_window(&self) -> Option<NonNull<c_void>> {
103+
self.b_direct_window
104+
}
79105
}

src/ohos.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ use core::ptr::NonNull;
2020
use super::DisplayHandle;
2121

2222
/// Raw display handle for OpenHarmony.
23-
#[non_exhaustive]
2423
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25-
pub struct OhosDisplayHandle {}
24+
pub struct OhosDisplayHandle(());
2625

2726
impl OhosDisplayHandle {
2827
/// Create a new empty display handle.
@@ -35,7 +34,7 @@ impl OhosDisplayHandle {
3534
/// let handle = OhosDisplayHandle::new();
3635
/// ```
3736
pub fn new() -> Self {
38-
Self {}
37+
Self(())
3938
}
4039
}
4140

@@ -60,10 +59,9 @@ impl DisplayHandle<'static> {
6059
}
6160

6261
/// Raw window handle for Ohos NDK.
63-
#[non_exhaustive]
6462
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6563
pub struct OhosNdkWindowHandle {
66-
pub native_window: NonNull<c_void>,
64+
native_window: NonNull<c_void>,
6765
}
6866

6967
impl OhosNdkWindowHandle {
@@ -95,4 +93,9 @@ impl OhosNdkWindowHandle {
9593
pub fn new(native_window: NonNull<c_void>) -> Self {
9694
Self { native_window }
9795
}
96+
97+
/// Get the handle to `OHNativeWindow`.
98+
pub fn native_window(&self) -> NonNull<c_void> {
99+
self.native_window
100+
}
98101
}

src/redox.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use core::ptr::NonNull;
44
use super::DisplayHandle;
55

66
/// Raw display handle for the Redox operating system.
7-
#[non_exhaustive]
87
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9-
pub struct OrbitalDisplayHandle {}
8+
pub struct OrbitalDisplayHandle(());
109

1110
impl OrbitalDisplayHandle {
1211
/// Create a new empty display handle.
@@ -19,7 +18,7 @@ impl OrbitalDisplayHandle {
1918
/// let handle = OrbitalDisplayHandle::new();
2019
/// ```
2120
pub fn new() -> Self {
22-
Self {}
21+
Self(())
2322
}
2423
}
2524

@@ -44,13 +43,11 @@ impl DisplayHandle<'static> {
4443
}
4544

4645
/// Raw window handle for the Redox operating system.
47-
#[non_exhaustive]
4846
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4947
pub struct OrbitalWindowHandle {
50-
/// A pointer to an orbclient window.
5148
// TODO(madsmtm): I think this is a file descriptor, so perhaps it should
5249
// actually use `std::os::fd::RawFd`, or some sort of integer instead?
53-
pub window: NonNull<c_void>,
50+
window: NonNull<c_void>,
5451
}
5552

5653
impl OrbitalWindowHandle {
@@ -71,4 +68,9 @@ impl OrbitalWindowHandle {
7168
pub fn new(window: NonNull<c_void>) -> Self {
7269
Self { window }
7370
}
71+
72+
/// A pointer to an orbclient window.
73+
pub fn window(&self) -> NonNull<c_void> {
74+
self.window
75+
}
7476
}

0 commit comments

Comments
 (0)