Module mailbox

Module mailbox 

Source
Expand description

Mailbox-based message passing for safe cross-thread communication.

This module provides a Mailbox type that enables asynchronous message passing to a value owned by a background task. The mailbox ensures thread-safe access to the contained value by serializing all operations through a message queue.

§Overview

The mailbox pattern is useful when you need to:

  • Share mutable state across threads safely
  • Process operations on a value sequentially
  • Avoid blocking when sending updates
  • Make async calls that return values

§Examples

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

// Create a mailbox containing a HashMap on the main executor
let mailbox = Mailbox::main(HashMap::<String, i32>::new());

// Send updates to the value (non-blocking)
mailbox.handle(|map| {
    map.insert("key".to_string(), 42);
});

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

Structs§

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