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