Struct objc2_foundation::MainThreadMarker
source · [−]pub struct MainThreadMarker { /* private fields */ }
Expand description
A marker type taken by functions that can only be executed on the main thread.
By design, this is neither Send
nor Sync
, and can only be created
on the main thread, meaning that if you’re holding this, you know you’re
running on the main thread.
See the following links for more information on main-thread-only APIs:
- Main Thread Only APIs on OS X
- Thread Safety Summary
- Are the Cocoa Frameworks Thread Safe?
- Technical Note TN2028 - Threading Architectures
- Thread Management
- Mike Ash’ article on thread safety
Examples
Use when designing APIs that are only safe to use on the main thread:
use objc2::msg_send;
use objc2::runtime::Object;
use objc2_foundation::MainThreadMarker;
// This action requires the main thread, so we take a marker as parameter.
// It signals clearly to users "this requires the main thread".
unsafe fn do_thing(obj: *const Object, _mtm: MainThreadMarker) {
msg_send![obj, someActionThatRequiresTheMainThread]
}
// Usage
let mtm = MainThreadMarker::new().unwrap();
unsafe { do_thing(obj, mtm) }
Implementations
sourceimpl MainThreadMarker
impl MainThreadMarker
sourcepub fn new() -> Option<Self>
pub fn new() -> Option<Self>
Construct a new MainThreadMarker
.
Returns None
if the current thread was not the main thread.
sourcepub unsafe fn new_unchecked() -> Self
pub unsafe fn new_unchecked() -> Self
Construct a new MainThreadMarker
without first checking whether
the current thread is the main one.
Safety
The current thread must be the main thread.
Trait Implementations
sourceimpl Clone for MainThreadMarker
impl Clone for MainThreadMarker
sourcefn clone(&self) -> MainThreadMarker
fn clone(&self) -> MainThreadMarker
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for MainThreadMarker
impl Debug for MainThreadMarker
sourceimpl Hash for MainThreadMarker
impl Hash for MainThreadMarker
sourceimpl Ord for MainThreadMarker
impl Ord for MainThreadMarker
sourceimpl PartialEq<MainThreadMarker> for MainThreadMarker
impl PartialEq<MainThreadMarker> for MainThreadMarker
sourcefn eq(&self, other: &MainThreadMarker) -> bool
fn eq(&self, other: &MainThreadMarker) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &MainThreadMarker) -> bool
fn ne(&self, other: &MainThreadMarker) -> bool
This method tests for !=
.
sourceimpl PartialOrd<MainThreadMarker> for MainThreadMarker
impl PartialOrd<MainThreadMarker> for MainThreadMarker
sourcefn partial_cmp(&self, other: &MainThreadMarker) -> Option<Ordering>
fn partial_cmp(&self, other: &MainThreadMarker) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for MainThreadMarker
impl Eq for MainThreadMarker
impl StructuralEq for MainThreadMarker
impl StructuralPartialEq for MainThreadMarker
Auto Trait Implementations
impl RefUnwindSafe for MainThreadMarker
impl !Send for MainThreadMarker
impl !Sync for MainThreadMarker
impl Unpin for MainThreadMarker
impl UnwindSafe for MainThreadMarker
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more