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>
impl<T> MainThreadBound<T>
Sourcepub fn new(value: T) -> Self
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.
Sourcepub fn into_inner(self) -> T
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.