Skip to content

Commit 42aa106

Browse files
committed
Add Layer::into_raw
1 parent 10f5284 commit 42aa106

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Unreleased
22
- Bump Rust Edition from 2018 to 2021.
3-
- Make `Layer`'s implementation details private; it is now a struct with `as_ptr` and `is_existing` accessor methods.
3+
- Make `Layer`'s implementation details private; it is now a struct with `as_ptr`, `into_raw` and `is_existing` accessor methods.
44
- Add support for tvOS, watchOS and visionOS.
55
- Use `objc2` internally.
66
- Move `Layer` constructors to the type itself.

src/lib.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
//! RawWindowHandle::UiKit(handle) => unsafe { Layer::from_ui_view(handle.ui_view) },
3737
//! _ => panic!("unsupported handle"),
3838
//! };
39-
//! let layer: *mut CAMetalLayer = layer.as_ptr().cast();
40-
//! let layer = unsafe { Retained::retain(layer).unwrap() };
39+
//! let layer: *mut CAMetalLayer = layer.into_raw().cast();
40+
//! // SAFETY: The pointer is a valid `CAMetalLayer` with +1 retain count.
41+
//! let layer = unsafe { Retained::from_raw(layer).unwrap() };
4142
//!
4243
//! // Use `CAMetalLayer` here.
4344
//! ```
@@ -198,7 +199,6 @@ impl Layer {
198199
/// # Example
199200
///
200201
/// ```no_run
201-
/// use objc2::rc::Retained;
202202
/// use objc2_quartz_core::CAMetalLayer;
203203
/// use raw_window_metal::Layer;
204204
///
@@ -207,7 +207,7 @@ impl Layer {
207207
///
208208
/// let layer: *mut CAMetalLayer = layer.as_ptr().cast();
209209
/// // SAFETY: The pointer is a valid `CAMetalLayer`.
210-
/// let layer = unsafe { Retained::retain(layer).unwrap() };
210+
/// let layer: &CAMetalLayer = unsafe { &*layer };
211211
///
212212
/// // Use the `CAMetalLayer` here.
213213
/// ```
@@ -217,6 +217,36 @@ impl Layer {
217217
ptr as *mut _
218218
}
219219

220+
/// Consume the layer, and return a pointer with +1 retain count to the underlying
221+
/// [`CAMetalLayer`].
222+
///
223+
/// After calling this function, the caller is responsible for releasing the pointer, otherwise
224+
/// the layer will be leaked.
225+
///
226+
///
227+
/// # Example
228+
///
229+
/// Convert a layer to a [`Retained`] `CAMetalLayer`.
230+
///
231+
/// ```no_run
232+
/// use objc2::rc::Retained;
233+
/// use objc2_quartz_core::CAMetalLayer;
234+
/// use raw_window_metal::Layer;
235+
///
236+
/// let layer: Layer;
237+
/// # layer = unimplemented!();
238+
///
239+
/// let layer: *mut CAMetalLayer = layer.into_raw().cast();
240+
/// // SAFETY: The pointer is a valid `CAMetalLayer` with +1 retain count.
241+
/// let layer = unsafe { Retained::from_raw(layer).unwrap() };
242+
///
243+
/// // Use the `CAMetalLayer` here.
244+
/// ```
245+
#[inline]
246+
pub fn into_raw(self) -> *mut c_void {
247+
Retained::into_raw(self.layer).cast()
248+
}
249+
220250
/// If `raw-window-metal` created a new [`CAMetalLayer`] for you, this returns `false`.
221251
///
222252
/// This may be useful if you want to override some part of `raw-window-metal`'s behaviour, and

0 commit comments

Comments
 (0)