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

Use NonNull in as_ptr and into_raw #23

Merged
merged 1 commit into from
Sep 9, 2024
Merged
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
23 changes: 13 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
//! RawWindowHandle::UiKit(handle) => unsafe { Layer::from_ui_view(handle.ui_view) },
//! _ => panic!("unsupported handle"),
//! };
//! let layer: *mut CAMetalLayer = layer.into_raw().cast();
//! // SAFETY: The pointer is a valid `CAMetalLayer` with +1 retain count.
//! let layer: *mut CAMetalLayer = layer.into_raw().as_ptr().cast();
//! // SAFETY: The pointer is a valid `CAMetalLayer`, and because we consumed `Layer` with
//! // `into_raw`, the pointer has +1 retain count.
//! let layer = unsafe { Retained::from_raw(layer).unwrap() };
//!
//! // Use `CAMetalLayer` here.
Expand Down Expand Up @@ -212,16 +213,16 @@ impl Layer {
/// let layer: Layer;
/// # layer = unimplemented!();
///
/// let layer: *mut CAMetalLayer = layer.as_ptr().cast();
/// // SAFETY: The pointer is a valid `CAMetalLayer`.
/// let layer: &CAMetalLayer = unsafe { &*layer };
/// let layer: &CAMetalLayer = unsafe { layer.as_ptr().cast().as_ref() };
///
/// // Use the `CAMetalLayer` here.
/// ```
#[inline]
pub fn as_ptr(&self) -> *mut c_void {
pub fn as_ptr(&self) -> NonNull<c_void> {
let ptr: *const CAMetalLayer = Retained::as_ptr(&self.layer);
ptr as *mut _
// Unwrap is fine, `Retained` stores a non-null pointer
NonNull::new(ptr as *mut _).unwrap()
}

/// Consume the layer, and return a pointer with +1 retain count to the underlying
Expand All @@ -243,15 +244,17 @@ impl Layer {
/// let layer: Layer;
/// # layer = unimplemented!();
///
/// let layer: *mut CAMetalLayer = layer.into_raw().cast();
/// // SAFETY: The pointer is a valid `CAMetalLayer` with +1 retain count.
/// let layer: *mut CAMetalLayer = layer.into_raw().as_ptr().cast();
/// // SAFETY: The pointer is a valid `CAMetalLayer`, and because we consumed `Layer` with
/// // `into_raw`, the pointer has +1 retain count.
/// let layer = unsafe { Retained::from_raw(layer).unwrap() };
///
/// // Use the `CAMetalLayer` here.
/// ```
#[inline]
pub fn into_raw(self) -> *mut c_void {
Retained::into_raw(self.layer).cast()
pub fn into_raw(self) -> NonNull<c_void> {
// Unwrap is fine, `Retained` stores a non-null pointer
NonNull::new(Retained::into_raw(self.layer).cast()).unwrap()
Comment on lines +256 to +257
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing (given your PR description here about Box) that you might plan on changing the return type of Retained too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm considering it

}

/// If `raw-window-metal` created a new [`CAMetalLayer`] for you, this returns `false`.
Expand Down
Loading