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
//! ```
@@ -198,7 +199,6 @@ impl Layer {
198
199
/// # Example
199
200
///
200
201
/// ```no_run
201
- /// use objc2::rc::Retained;
202
202
/// use objc2_quartz_core::CAMetalLayer;
203
203
/// use raw_window_metal::Layer;
204
204
///
@@ -207,7 +207,7 @@ impl Layer {
207
207
///
208
208
/// let layer: *mut CAMetalLayer = layer.as_ptr().cast();
209
209
/// // SAFETY: The pointer is a valid `CAMetalLayer`.
210
- /// let layer = unsafe { Retained::retain( layer).unwrap() };
210
+ /// let layer: &CAMetalLayer = unsafe { &* layer };
211
211
///
212
212
/// // Use the `CAMetalLayer` here.
213
213
/// ```
@@ -217,6 +217,36 @@ impl Layer {
217
217
ptr as * mut _
218
218
}
219
219
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
+
220
250
/// If `raw-window-metal` created a new [`CAMetalLayer`] for you, this returns `false`.
221
251
///
222
252
/// This may be useful if you want to override some part of `raw-window-metal`'s behaviour, and
0 commit comments