pub struct MainThreadToken(/* private fields */);Expand description
A witness of execution that exists solely on a designated “Main Thread”.
In FFI contexts, many libraries (especially legacy C++ or UI frameworks) are not thread-safe and must only be initialized, called, or dropped from the same thread that originally created them.
MainThreadToken is a zero-sized proof carrier. Possessing it proves
(at a type-system level) that the holder previously executed on the
designated main thread. The PhantomData<*mut ()> makes the token
!Send + !Sync, so a token obtained on the main thread cannot leak to
another thread through ordinary safe code.
Implementations§
Source§impl MainThreadToken
impl MainThreadToken
Sourcepub unsafe fn new_unchecked() -> Self
pub unsafe fn new_unchecked() -> Self
Create a new MainThreadToken without verifying the current thread.
§Safety
The caller must guarantee that:
- They are executing on the thread that was (or will be) passed to
designate_main_thread, and - The resulting token will not be transmitted to another thread
through
unsafechannels (the type is!Send + !Sync, which prevents safe channels from doing so).
Sourcepub fn try_new() -> Option<Self>
pub fn try_new() -> Option<Self>
Obtain a token if the current thread matches the one previously passed
to designate_main_thread.
Returns None if designate_main_thread has never been called, or
if the current thread is not the designated main thread.
Trait Implementations§
Source§impl Clone for MainThreadToken
impl Clone for MainThreadToken
Source§fn clone(&self) -> MainThreadToken
fn clone(&self) -> MainThreadToken
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MainThreadToken
impl Debug for MainThreadToken
Source§impl PartialEq for MainThreadToken
impl PartialEq for MainThreadToken
Source§fn eq(&self, other: &MainThreadToken) -> bool
fn eq(&self, other: &MainThreadToken) -> bool
self and other values to be equal, and is used by ==.impl Copy for MainThreadToken
impl Eq for MainThreadToken
impl StructuralPartialEq for MainThreadToken
Auto Trait Implementations§
impl Freeze for MainThreadToken
impl RefUnwindSafe for MainThreadToken
impl !Send for MainThreadToken
impl !Sync for MainThreadToken
impl Unpin for MainThreadToken
impl UnsafeUnpin for MainThreadToken
impl UnwindSafe for MainThreadToken
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more