From d517e6dabaf1ba11b314a9f8330febf418f49916 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 4 Sep 2024 08:40:53 +0200 Subject: [PATCH 1/2] Implement common traits for Layer Debug, Clone, PartialEq, Eq, Hash, Send, Sync, UnwindSafe and RefUnwindSafe. --- src/lib.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 559c2d6..33ec2aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,11 @@ #![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide), doc(cfg_hide(doc)))] #![deny(unsafe_op_in_unsafe_fn)] +use core::ffi::c_void; +use core::hash; +use core::panic::{RefUnwindSafe, UnwindSafe}; use objc2::rc::Retained; use objc2_quartz_core::CAMetalLayer; -use std::ffi::c_void; #[cfg(any(target_os = "macos", doc))] pub mod appkit; @@ -14,11 +16,43 @@ pub mod appkit; pub mod uikit; /// A wrapper around [`CAMetalLayer`]. +#[doc(alias = "CAMetalLayer")] +#[derive(Debug, Clone)] pub struct Layer { layer: Retained, pre_existing: bool, } +impl PartialEq for Layer { + #[inline] + fn eq(&self, other: &Self) -> bool { + self.layer == other.layer + } +} + +impl Eq for Layer {} + +impl hash::Hash for Layer { + #[inline] + fn hash(&self, state: &mut H) { + self.layer.hash(state); + } +} + +// SAFETY: `CAMetalLayer` is thread safe, like most things in Core Animation, see: +// https://developer.apple.com/documentation/quartzcore/catransaction/1448267-lock?language=objc +// https://stackoverflow.com/questions/76250226/how-to-render-content-of-calayer-on-a-background-thread +// +// TODO(madsmtm): Move this to `objc2-quartz-core`. +unsafe impl Send for Layer {} +unsafe impl Sync for Layer {} + +// Layer methods may panic, but that won't leave the layer in an invalid state. +// +// TODO(madsmtm): Move this to `objc2-quartz-core`. +impl UnwindSafe for Layer {} +impl RefUnwindSafe for Layer {} + impl Layer { /// Get a pointer to the underlying [`CAMetalLayer`]. The pointer is valid /// for at least as long as the [`Layer`] is valid, but can be extended by From f43ecabb423a047bed4bafde10832ac7eb3110e7 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 4 Sep 2024 12:08:15 +0200 Subject: [PATCH 2/2] Use .eq --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 33ec2aa..0150a77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ pub struct Layer { impl PartialEq for Layer { #[inline] fn eq(&self, other: &Self) -> bool { - self.layer == other.layer + self.layer.eq(&other.layer) } }