36
36
//! RawWindowHandle::UiKit(handle) => unsafe { Layer::from_ui_view(handle.ui_view) },
37
37
//! _ => panic!("unsupported handle"),
38
38
//! };
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() };
41
42
//!
42
43
//! // Use `CAMetalLayer` here.
43
44
//! ```
@@ -205,7 +206,6 @@ impl Layer {
205
206
/// # Example
206
207
///
207
208
/// ```no_run
208
- /// use objc2::rc::Retained;
209
209
/// use objc2_quartz_core::CAMetalLayer;
210
210
/// use raw_window_metal::Layer;
211
211
///
@@ -214,7 +214,7 @@ impl Layer {
214
214
///
215
215
/// let layer: *mut CAMetalLayer = layer.as_ptr().cast();
216
216
/// // SAFETY: The pointer is a valid `CAMetalLayer`.
217
- /// let layer = unsafe { Retained::retain( layer).unwrap() };
217
+ /// let layer: &CAMetalLayer = unsafe { &* layer };
218
218
///
219
219
/// // Use the `CAMetalLayer` here.
220
220
/// ```
@@ -224,6 +224,36 @@ impl Layer {
224
224
ptr as * mut _
225
225
}
226
226
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
+
227
257
/// If `raw-window-metal` created a new [`CAMetalLayer`] for you, this returns `false`.
228
258
///
229
259
/// This may be useful if you want to override some part of `raw-window-metal`'s behaviour, and
0 commit comments