Struct objc2_foundation::MainThreadBound

source ·
pub struct MainThreadBound<T>(/* private fields */);
Available on crate features NSThread and dispatch only.
Expand description

Make a type that can only be used on the main thread be Send + Sync.

On Drop, the inner type is sent to the main thread’s runloop and dropped there. This may lead to deadlocks if the main runloop is not running, or if it is waiting on a lock that the dropping thread is holding. See run_on_main for some of the caveats around that.

This type takes inspiration from threadbound::ThreadBound.

The functionality also somewhat resembles Swift’s @MainActor, which ensures that a type is only usable from the main thread.

Implementations§

source§

impl<T> MainThreadBound<T>

Main functionality.

source

pub fn new(inner: T, _mtm: MainThreadMarker) -> Self

Create a new MainThreadBound value of type T.

§Example
use objc2_foundation::{MainThreadMarker, MainThreadBound};

let foo;
let mtm = MainThreadMarker::new().expect("must be on the main thread");
let foo = MainThreadBound::new(foo, mtm);

// `foo` is now `Send + Sync`.
source

pub fn get(&self, _mtm: MainThreadMarker) -> &T

Returns a reference to the value.

source

pub fn get_mut(&mut self, _mtm: MainThreadMarker) -> &mut T

Returns a mutable reference to the value.

source

pub fn into_inner(self, _mtm: MainThreadMarker) -> T

Extracts the value from the MainThreadBound container.

source§

impl<T> MainThreadBound<T>

Helper functions for running run_on_main.

source

pub fn get_on_main<F, R>(&self, f: F) -> R
where F: Send + FnOnce(&T) -> R, R: Send,

Access the item on the main thread.

See run_on_main for caveats.

source

pub fn get_on_main_mut<F, R>(&mut self, f: F) -> R
where F: Send + FnOnce(&mut T) -> R, R: Send,

Access the item mutably on the main thread.

See run_on_main for caveats.

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> Drop for MainThreadBound<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> Send for MainThreadBound<T>

source§

impl<T> Sync for MainThreadBound<T>

Auto Trait Implementations§

§

impl<T> Freeze for MainThreadBound<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for MainThreadBound<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for MainThreadBound<T>
where T: Unpin,

§

impl<T> UnwindSafe for MainThreadBound<T>
where T: UnwindSafe,

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, 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<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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>,

§

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.
source§

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