You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make implementing ClassType in declare_class! safe #528
Implementing ClassType in declare_class! is unsafe because we need the user to uphold a few safety guarantees, and that was just the most convenient place to put the unsafe keyword.
After #521 though, the only safety guarantees that the user needs to uphold are:
Any invariants that the superclass ClassType::Super may have must be upheld.
ClassType::Mutability must be correct.
Drop must be implemented correctly.
We should work on ways to make fulfilling these unsafe requirements more granular.
One possibility for requirement 2 would be to migrate from an associated type Mutability to a constant, that you must initialize with unsafe if you need certain features:
pubstructMutability{}implMutability{pubconstfninterior_mutable() -> Self{todo!()}/// # Safety/// ... clearly detailed docs for when this is safe ...pubconstunsafefnmain_thread_only() -> Self{todo!()}// ...}// Usagedeclare_class!(structMyClass;unsafeimplClassTypeforMyClass{typeSuper = NSObject;constMUTABILITY:Mutability = Mutability::interior_mutable();// Or// SAFETY: ...// const MUTABILITY: Mutability = unsafe { Mutability::main_thread_only() };constNAME:&'staticstr = "MyClass";}// ...);
Hmm, just realized that the proposed idea won't work, since you could do const MUTABILITY: Mutability = NSView::MUTABILITY;, and thereby bypass the unsafe.
Since #563, the associated Mutability type is changed to ThreadKind, and I've implemented a check to ensure that you can't set an incorrect thread kind.
I'm considering moving Drop to a fn dealloc(&self), to avoid the mutability issue if you call self.retain() inside dealloc. This would also be a slight perf hit, since you could no longer do things like self.ivars_mut().my_ivar.get_mut(), you'd still have to do interior mutability inside your deallocation method, but that's probably fine. Besides, instance variables themselves can still use &mut safely, it's just the class itself which can't.
Activity
madsmtm commentedon Nov 28, 2023
Hmm, just realized that the proposed idea won't work, since you could do
const MUTABILITY: Mutability = NSView::MUTABILITY;, and thereby bypass theunsafe.madsmtm commentedon Sep 17, 2024
Since #563, the associated
Mutabilitytype is changed toThreadKind, and I've implemented a check to ensure that you can't set an incorrect thread kind.I'm considering moving
Dropto afn dealloc(&self), to avoid the mutability issue if you callself.retain()insidedealloc. This would also be a slight perf hit, since you could no longer do things likeself.ivars_mut().my_ivar.get_mut(), you'd still have to do interior mutability inside your deallocation method, but that's probably fine. Besides, instance variables themselves can still use&mutsafely, it's just the class itself which can't.