Mailbox

Struct Mailbox 

Source
pub struct Mailbox<T: 'static> { /* private fields */ }
Expand description

A mailbox for sending messages to a value owned by a background task.

Mailbox<T> provides thread-safe access to a value of type T by serializing all operations through an async message queue. The value is owned by a background task that processes incoming messages sequentially.

§Type Parameters

  • T - The type of value contained in the mailbox. Must be 'static to ensure the background task can own it safely.

§Thread Safety

The mailbox enables other threads to safely access a value living on another thread without explicit locks. The mailbox handle itself is always Send and Sync, allowing it to be shared across threads. All operations on the value are serialized through an async message queue, providing lock-free concurrent access. When T is not Send, the value remains pinned to its original thread but can still be safely accessed from other threads through the mailbox.

Implementations§

Source§

impl<T: 'static> Mailbox<T>

Source

pub fn new<E: LocalExecutor>(executor: E, value: T) -> Self

Creates a new mailbox with the given value on the specified executor.

The value will be moved to a background task that processes incoming messages. The executor is consumed to spawn the background task.

§Parameters
  • executor - The executor to spawn the background task on
  • value - The value to be owned by the background task
§Examples
use native_executor::{Mailbox, MainExecutor};
use std::collections::HashMap;

let mailbox = Mailbox::new(MainExecutor, HashMap::<String, i32>::new());
Source

pub fn main(value: T) -> Self

Creates a new mailbox with the given value on the main executor.

This is a convenience method equivalent to Mailbox::new(MainExecutor, value). The background task will be spawned on the main executor.

§Parameters
  • value - The value to be owned by the background task
§Examples
use native_executor::Mailbox;
use std::collections::HashMap;

let mailbox = Mailbox::main(HashMap::<String, i32>::new());
Source

pub fn handle(&self, update: impl FnOnce(&T) + Send + 'static)

Sends a non-blocking update to the mailbox value.

The provided closure will be called with a reference to the value in the background task. This operation is non-blocking and will not wait for the update to be processed.

If the background task has been dropped or the channel is full, the update may be silently discarded.

§Parameters
  • update - A closure that will be called with a reference to the value
§Examples
use native_executor::Mailbox;
use std::collections::HashMap;

let mailbox = Mailbox::main(HashMap::<String, i32>::new());

// Send a non-blocking update
mailbox.handle(|map| {
    map.insert("key".to_string(), 42);
});
Source

pub async fn call<R>(&self, f: impl FnOnce(&T) -> R + Send + 'static) -> R
where R: Send + 'static,

Makes an asynchronous call to the mailbox value and returns the result.

The provided closure will be called with a reference to the value in the background task, and the result will be returned to the caller. This operation blocks until the call is processed and the result is available.

§Parameters
  • f - A closure that will be called with a reference to the value and returns a result
§Returns

The result returned by the closure after it has been executed on the value.

§Panics

Panics if the background task has been dropped or the channel is closed, making it impossible to receive the result.

§Examples
use native_executor::Mailbox;
use std::collections::HashMap;

let mailbox = Mailbox::main(HashMap::<String, i32>::new());

// Make an async call that returns a value
let value = mailbox.call(|map| {
    map.get("key").copied().unwrap_or(0)
}).await;

Trait Implementations§

Source§

impl<T: Debug + 'static> Debug for Mailbox<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Mailbox<T>

§

impl<T> RefUnwindSafe for Mailbox<T>

§

impl<T> Send for Mailbox<T>

§

impl<T> Sync for Mailbox<T>

§

impl<T> Unpin for Mailbox<T>

§

impl<T> UnwindSafe for Mailbox<T>

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

Source§

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

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.