Skip to content

Commit 70e3f4d

Browse files
authored
Add Layer::into_raw (#22)
1 parent e2a1748 commit 70e3f4d

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
//! ```
@@ -205,7 +206,6 @@ impl Layer {
205206
/// # Example
206207
///
207208
/// ```no_run
208-
/// use objc2::rc::Retained;
209209
/// use objc2_quartz_core::CAMetalLayer;
210210
/// use raw_window_metal::Layer;
211211
///
@@ -214,7 +214,7 @@ impl Layer {
214214
///
215215
/// let layer: *mut CAMetalLayer = layer.as_ptr().cast();
216216
/// // SAFETY: The pointer is a valid `CAMetalLayer`.
217-
/// let layer = unsafe { Retained::retain(layer).unwrap() };
217+
/// let layer: &CAMetalLayer = unsafe { &*layer };
218218
///
219219
/// // Use the `CAMetalLayer` here.
220220
/// ```
@@ -224,6 +224,36 @@ impl Layer {
224224
ptr as *mut _
225225
}
226226

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

0 commit comments

Comments
 (0)