Skip to content

Commit

Permalink
Add #![warn(clippy::undocumented_unsafe_blocks)]
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 8, 2024
1 parent 5618656 commit b1a2641
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
#![cfg(target_vendor = "apple")]
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide), doc(cfg_hide(doc)))]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(clippy::undocumented_unsafe_blocks)]

mod observer;

Expand Down Expand Up @@ -143,6 +144,7 @@ impl hash::Hash for Layer {
//
// TODO(madsmtm): Move this to `objc2-quartz-core`.
unsafe impl Send for Layer {}
// SAFETY: Same as above.
unsafe impl Sync for Layer {}

// Layer methods may panic, but that won't leave the layer in an invalid state.
Expand Down Expand Up @@ -325,6 +327,7 @@ impl Layer {
let ns_view: &NSObject = unsafe { ns_view_ptr.cast().as_ref() };

// Force the view to become layer backed
// SAFETY: The signature of `NSView::setWantsLayer` is correctly specified, and
let _: () = unsafe { msg_send![ns_view, setWantsLayer: true] };

// SAFETY: `-[NSView layer]` returns an optional `CALayer`
Expand Down
10 changes: 9 additions & 1 deletion src/observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Drop for ObserverLayer {
// We use a weak variable here to avoid issues if the layer was removed from the super
// layer, and then later de-allocated, without de-registering these observers.
if let Some(root_layer) = self.ivars().load() {
// SAFETY: The observer is registered for these key paths in `new`.
unsafe {
root_layer.removeObserver_forKeyPath(self, ns_string!("contentsScale"));
root_layer.removeObserver_forKeyPath(self, ns_string!("bounds"));
Expand All @@ -76,6 +77,7 @@ impl ObserverLayer {
/// Create a new custom layer that tracks parameters from the given super layer.
pub fn new(root_layer: &CALayer) -> Retained<Self> {
let this = Self::alloc().set_ivars(Weak::new(root_layer));
// SAFETY: Initializing `CAMetalLayer` is safe.
let this: Retained<Self> = unsafe { msg_send_id![super(this), init] };

// Add the layer as a sublayer of the root layer.
Expand Down Expand Up @@ -143,6 +145,8 @@ impl ObserverLayer {
) {
// An unrecognized context must belong to the super class.
if context != ObserverLayer::context() {
// SAFETY: The signature is correct, and it's safe to forward to the superclass' method
// when we're overriding the method.
return unsafe {
msg_send![
super(self),
Expand All @@ -156,8 +160,10 @@ impl ObserverLayer {

let change =
change.expect("requested a change dictionary in `addObserver`, but none was provided");
// SAFETY: The static is declared with the correct type in `objc2`.
let key = unsafe { NSKeyValueChangeNewKey };
let new = change
.get(unsafe { NSKeyValueChangeNewKey })
.get(key)
.expect("requested change dictionary did not contain `NSKeyValueChangeNewKey`");

// NOTE: Setting these values usually causes a quarter second animation to occur, which is
Expand All @@ -167,13 +173,15 @@ impl ObserverLayer {
// ongoing, and as such we don't need to wrap this in a `CATransaction` ourselves.

if key_path == Some(ns_string!("contentsScale")) {
// SAFETY: `contentsScale` is a CGFloat, and so the observed value is always a NSNumber.
let new = unsafe { &*(new as *const AnyObject as *const NSNumber) };
let scale_factor = new.as_cgfloat();

// Set the scale factor of the layer to match the root layer when it changes (e.g. if
// moved to a different monitor, or monitor settings changed).
self.setContentsScale(scale_factor);
} else if key_path == Some(ns_string!("bounds")) {
// SAFETY: `bounds` is a CGRect, and so the observed value is always a NSValue.
let new = unsafe { &*(new as *const AnyObject as *const NSValue) };
let bounds = new.get_rect().expect("new bounds value was not CGRect");

Expand Down

0 comments on commit b1a2641

Please sign in to comment.