Skip to content

Commit

Permalink
Safety docs
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 28, 2024
1 parent 883d5c5 commit 4bfd0c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/appkit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ use std::ptr::NonNull;

use crate::Layer;

/// Get or create a new [`Layer`] associated with the given
/// [`AppKitWindowHandle`].
///
/// # Safety
///
/// The handle must be valid.
pub unsafe fn metal_layer_from_handle(handle: AppKitWindowHandle) -> Layer {
unsafe { metal_layer_from_ns_view(handle.ns_view) }
}

/// Get or create a new [`Layer`] associated with the given `NSView`.
///
/// # Safety
///
/// The view must be a valid instance of `NSView`.
pub unsafe fn metal_layer_from_ns_view(view: NonNull<c_void>) -> Layer {
// SAFETY: Caller ensures that the view is valid.
let obj = unsafe { view.cast::<NSObject>().as_ref() };
Expand Down
9 changes: 9 additions & 0 deletions src/uikit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ use objc2_quartz_core::CAMetalLayer;
use raw_window_handle::UiKitWindowHandle;
use std::{ffi::c_void, ptr::NonNull};

/// Get or create a new [`Layer`] associated with the given
/// [`UiKitWindowHandle`].
///
/// # Safety
///
/// The handle must be valid.
pub unsafe fn metal_layer_from_handle(handle: UiKitWindowHandle) -> Layer {
if let Some(_ui_view_controller) = handle.ui_view_controller {
// TODO: ui_view_controller support
}
unsafe { metal_layer_from_ui_view(handle.ui_view) }
}

/// Get or create a new [`Layer`] associated with the given `UIView`.
///
/// # Safety
///
/// The view must be a valid instance of `UIView`.
pub unsafe fn metal_layer_from_ui_view(view: NonNull<c_void>) -> Layer {
// SAFETY: Caller ensures that the view is a `UIView`.
let view = unsafe { view.cast::<objc2_ui_kit::UIView>().as_ref() };
Expand Down

0 comments on commit 4bfd0c2

Please sign in to comment.