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::foundation::MainThreadMarker;
use objc2::runtime::Object;
use objc2::msg_send;
// 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§
source§impl 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§
source§impl Clone for MainThreadMarker
impl Clone for MainThreadMarker
source§fn clone(&self) -> MainThreadMarker
fn clone(&self) -> MainThreadMarker
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for MainThreadMarker
impl Debug for MainThreadMarker
source§impl Hash for MainThreadMarker
impl Hash for MainThreadMarker
source§impl Ord for MainThreadMarker
impl Ord for MainThreadMarker
source§fn cmp(&self, other: &MainThreadMarker) -> Ordering
fn cmp(&self, other: &MainThreadMarker) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl PartialEq<MainThreadMarker> for MainThreadMarker
impl PartialEq<MainThreadMarker> for MainThreadMarker
source§fn 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 ==
.source§impl PartialOrd<MainThreadMarker> for MainThreadMarker
impl PartialOrd<MainThreadMarker> for MainThreadMarker
source§fn partial_cmp(&self, other: &MainThreadMarker) -> Option<Ordering>
fn partial_cmp(&self, other: &MainThreadMarker) -> Option<Ordering>
1.0.0 · source§fn 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 moreimpl 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§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more