pub unsafe trait MainThreadOnly: SealedMainThreadOnly {
// Provided methods
fn mtm(&self) -> MainThreadMarker { ... }
fn alloc(mtm: MainThreadMarker) -> Allocated<Self>
where Self: Sized + ClassType { ... }
}Expand description
Marker trait for classes and protocols that are only safe to use on the main thread.
This is commonly used in GUI code like AppKit and UIKit, e.g.
UIWindow is only usable from the application’s main thread because it
accesses global statics like the UIApplication.
See MainThreadMarker for a few more details on this.
§Safety
It is unsound to implement Send or Sync together with this.
This is a sealed trait, and should not need to be implemented; it is
implemented automatically when you implement ClassType.
Provided Methods§
Sourcefn mtm(&self) -> MainThreadMarker
fn mtm(&self) -> MainThreadMarker
Get a MainThreadMarker from the main-thread-only object.
This function exists purely in the type-system, and will succeed at runtime (with a safety check when debug assertions are enabled).
Sourcefn alloc(mtm: MainThreadMarker) -> Allocated<Self>
fn alloc(mtm: MainThreadMarker) -> Allocated<Self>
Allocate a new instance of the class on the main thread.
§Example
Create a view on the main thread.
use objc2::{MainThreadOnly, MainThreadMarker};
use objc2_app_kit::NSView;
use objc2_core_foundation::CGRect;
let mtm = MainThreadMarker::new().expect("must be on the main thread");
let frame = CGRect::default();
let view = NSView::initWithFrame(NSView::alloc(mtm), frame);