Skip to main content

MainThreadBound

Struct MainThreadBound 

Source
pub struct MainThreadBound<T> { /* private fields */ }
Expand description

Wraps a !Send/!Sync value so it satisfies Send + Sync, while enforcing at runtime that it is only accessed on the thread that constructed it.

§Safety model

The Send and Sync impls are unsafe: they are sound only because every access goes through Deref / DerefMut / into_inner, each of which asserts the caller is on the owning thread and panics otherwise, so the inner !Send value never actually crosses a thread boundary. If the wrapper is dropped on a non-owning thread the inner value is leaked rather than dropped, since running a !Send destructor off-thread would be undefined behavior.

The owner-thread check requires the std feature, which is on by default and must stay on for any build that can spawn a second thread. Disabling default features is reserved for genuinely single-threaded no_std builds, where there is no other thread to violate the confinement and the checks compile to no-ops; opting out on a threaded target silently removes the soundness net.

Implementations§

Source§

impl<T> MainThreadBound<T>

Source

pub fn new(value: T) -> Self

Bind value to the current (main) thread.

Recording the owning thread needs std; without it there is no thread to record and the binding is a plain wrapper, so that build gets a const constructor.

Source

pub fn into_inner(self) -> T

Consume the wrapper and return the inner value, asserting the caller is on the owning thread.

§Panics

Panics if called from a thread other than the one that constructed the value.

Trait Implementations§

Source§

impl<T> Debug for MainThreadBound<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for MainThreadBound<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for MainThreadBound<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T> Drop for MainThreadBound<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T> Send for MainThreadBound<T>

Source§

impl<T> Sync for MainThreadBound<T>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> IdentifiableExt for T

Source§

fn use_id<F, Id>(self, f: F) -> UseId<Self, F>
where F: Fn(&Self) -> Id, Id: Ord + Hash,

Wraps the value in a UseId with the provided identification function.
Source§

fn self_id(self) -> SelfId<Self>

Wraps the value in a SelfId, making the value serve as its own identifier.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.