Skip to content

Commit

Permalink
Add Layer::into_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 9, 2024
1 parent 10f5284 commit 42aa106
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Unreleased
- Bump Rust Edition from 2018 to 2021.
- Make `Layer`'s implementation details private; it is now a struct with `as_ptr` and `is_existing` accessor methods.
- Make `Layer`'s implementation details private; it is now a struct with `as_ptr`, `into_raw` and `is_existing` accessor methods.
- Add support for tvOS, watchOS and visionOS.
- Use `objc2` internally.
- Move `Layer` constructors to the type itself.
Expand Down
38 changes: 34 additions & 4 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.as_ptr().cast();
//! let layer = unsafe { Retained::retain(layer).unwrap() };
//! let layer: *mut CAMetalLayer = layer.into_raw().cast();
//! // SAFETY: The pointer is a valid `CAMetalLayer` with +1 retain count.
//! let layer = unsafe { Retained::from_raw(layer).unwrap() };
//!
//! // Use `CAMetalLayer` here.
//! ```
Expand Down Expand Up @@ -198,7 +199,6 @@ impl Layer {
/// # Example
///
/// ```no_run
/// use objc2::rc::Retained;
/// use objc2_quartz_core::CAMetalLayer;
/// use raw_window_metal::Layer;
///
Expand All @@ -207,7 +207,7 @@ impl Layer {
///
/// let layer: *mut CAMetalLayer = layer.as_ptr().cast();
/// // SAFETY: The pointer is a valid `CAMetalLayer`.
/// let layer = unsafe { Retained::retain(layer).unwrap() };
/// let layer: &CAMetalLayer = unsafe { &*layer };
///
/// // Use the `CAMetalLayer` here.
/// ```
Expand All @@ -217,6 +217,36 @@ impl Layer {
ptr as *mut _
}

/// Consume the layer, and return a pointer with +1 retain count to the underlying
/// [`CAMetalLayer`].
///
/// After calling this function, the caller is responsible for releasing the pointer, otherwise
/// the layer will be leaked.
///
///
/// # Example
///
/// Convert a layer to a [`Retained`] `CAMetalLayer`.
///
/// ```no_run
/// use objc2::rc::Retained;
/// use objc2_quartz_core::CAMetalLayer;
/// use raw_window_metal::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 = 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()
}

/// If `raw-window-metal` created a new [`CAMetalLayer`] for you, this returns `false`.
///
/// This may be useful if you want to override some part of `raw-window-metal`'s behaviour, and
Expand Down

0 comments on commit 42aa106

Please sign in to comment.