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