pub struct MainThreadCell<T: 'static> { /* private fields */ }Expand description
A thread-safe cell that ensures all access to its contents happens on the main thread.
MainThreadCell<T> allows you to share T across threads while guaranteeing that
all access to the inner value occurs on the main thread. This is particularly useful
for platform-specific resources that have main-thread-only requirements.
§Thread Safety
- The cell itself can be cloned and sent across threads
- All access methods verify they’re called from the main thread
- The
withandwith_asyncmethods automatically dispatch to the main thread - Drop operations are automatically handled on the main thread
Implementations§
Source§impl<T> MainThreadCell<T>
impl<T> MainThreadCell<T>
Sourcepub fn new(t: T) -> Self
pub fn new(t: T) -> Self
Creates a new MainThreadCell containing the given value.
This must be called on the main thread. Constructing the cell elsewhere would
allow a non-Send value (and any aliases it contains) to cross threads.
§Panics
Panics if called from a non-main thread.
Sourcepub fn lock(&self) -> MainThreadGuard<'_, T>
pub fn lock(&self) -> MainThreadGuard<'_, T>
Locks the cell and returns a guard providing mutable access to the inner value.
This method can only be called from the main thread.
§Panics
Panics if called from a non-main thread.
Sourcepub fn assume<C, R>(&self, c: C) -> R
pub fn assume<C, R>(&self, c: C) -> R
Runs a closure with immutable access to the inner value.
This method can only be called from the main thread.
§Panics
Panics if called from a non-main thread.
Sourcepub async fn with<C, R>(&self, c: C) -> R
pub async fn with<C, R>(&self, c: C) -> R
Runs a closure with the inner value, ensuring execution on the main thread.
If called from the main thread, the closure executes immediately. If called from another thread, it’s dispatched to the main thread.
§Panics
For the duration of this function, the cell may not be otherwise used.
Sourcepub async fn with_async<C, R, F>(&self, c: C) -> R
pub async fn with_async<C, R, F>(&self, c: C) -> R
Runs an async closure with the inner value, ensuring execution on the main thread.
If called from the main thread, the closure executes immediately. If called from another thread, it’s dispatched to the main thread.
This method is more restrictive than with_async - it requires that access to the
inner value doesn’t cross async boundaries within the closure.
§Panics
For the duration of this function, the cell may not be otherwise used.
Sourcepub async fn new_on_main_thread<C, F>(c: C) -> MainThreadCell<T>
pub async fn new_on_main_thread<C, F>(c: C) -> MainThreadCell<T>
Creates a new MainThreadCell by running a constructor closure on the main thread.
This function ensures the value is created on the main thread, which is useful for resources that must be constructed there.
Trait Implementations§
Source§impl<T> Clone for MainThreadCell<T>
impl<T> Clone for MainThreadCell<T>
Source§impl<T: Debug> Debug for MainThreadCell<T>
impl<T: Debug> Debug for MainThreadCell<T>
Source§impl<T: Default> Default for MainThreadCell<T>
impl<T: Default> Default for MainThreadCell<T>
Source§impl<T> From<T> for MainThreadCell<T>
impl<T> From<T> for MainThreadCell<T>
Source§impl<T> PartialEq for MainThreadCell<T>
impl<T> PartialEq for MainThreadCell<T>
impl<T> Send for MainThreadCell<T>
Auto Trait Implementations§
impl<T> !RefUnwindSafe for MainThreadCell<T>
impl<T> !UnwindSafe for MainThreadCell<T>
impl<T> Freeze for MainThreadCell<T>
impl<T> Sync for MainThreadCell<T>
impl<T> Unpin for MainThreadCell<T>
impl<T> UnsafeUnpin for MainThreadCell<T>
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<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.