Struct Mailbox

Source
pub struct Mailbox<SenderMessage: Send + Sync, ReceiverMessage: Send + Sync> { /* private fields */ }
Expand description

An object that has the ability to act as a two-way channel. This allows for two-way communications across threads, for example.

Implementations§

Source§

impl<MessageA: Send + Sync + 'static, MessageB: Send + Sync + 'static> Mailbox<MessageA, MessageB>

Source

pub fn new_entangled_pair() -> (Mailbox<MessageA, MessageB>, Mailbox<MessageB, MessageA>)

Creates a new pair of Mailboxs in the form of (Mailbox<MessageA, MessageB>, Mailbox<MessageB, MessageA>)

§Example
let (renderer_mailbox, game_mailbox) = Mailbox::new_entangled_pair();

renderer_mailbox.send(RenderLoopMessage::SyncWithGame);
if let Ok(RenderLoopMessage::SyncWithGame) = game_mailbox.poll() {
    // ...
}
 
game_mailbox.send_and_wait(GameLoopMessage::SyncWithRender)?;
Source§

impl<SenderMessage: Send + Sync, ReceiverMessage: Send + Sync> Mailbox<SenderMessage, ReceiverMessage>

Source

pub fn new( sender: Sender<SenderMessage>, receiver: Receiver<ReceiverMessage>, ) -> Self

Creates a new Mailbox. Since new_entangled_pair() exists, this is only exposed in case that doesn’t cover your use case. Otherwise you should just use that method instead.

§Example
let (sender_a, receiver_a) = std::sync::mpsc::channel();
let (sender_b, receiver_b) = std::sync::mpsc::channel();

let renderer_mailbox = Mailbox::new(sender_a, receiver_b);
let game_mailbox = Mailbox::new(sender_b, receiver_a);

renderer_mailbox.send(RenderLoopMessage::SyncWithGame);
if let Ok(RenderLoopMessage::SyncWithGame) = game_mailbox.poll() {
    // ...
}
 
game_mailbox.send_and_wait(GameLoopMessage::SyncWithRender)?;
Source

pub fn send( &self, message: SenderMessage, ) -> Result<(), MessagingError<SenderMessage>>

Sends a message through the held sender. Wrapper around sender.send()

Source

pub fn wait(&mut self) -> Result<ReceiverMessage, MessagingError<SenderMessage>>

Waits for a message from the held receiver. Wrapper around receiver.recv()

Source

pub fn poll(&mut self) -> Result<ReceiverMessage, MessagingError<SenderMessage>>

Polls for a message from the held receiver, otherwise immediately returning. Wrapper around receiver.try_recv()

Source

pub fn send_and_wait( &mut self, message: SenderMessage, ) -> Result<ReceiverMessage, MessagingError<SenderMessage>>

Does a send and a wait. Good for syncing between two threads.

Trait Implementations§

Source§

impl<SenderMessage: Debug + Send + Sync, ReceiverMessage: Debug + Send + Sync> Debug for Mailbox<SenderMessage, ReceiverMessage>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<SenderMessage, ReceiverMessage> Freeze for Mailbox<SenderMessage, ReceiverMessage>

§

impl<SenderMessage, ReceiverMessage> RefUnwindSafe for Mailbox<SenderMessage, ReceiverMessage>

§

impl<SenderMessage, ReceiverMessage> Send for Mailbox<SenderMessage, ReceiverMessage>

§

impl<SenderMessage, ReceiverMessage> !Sync for Mailbox<SenderMessage, ReceiverMessage>

§

impl<SenderMessage, ReceiverMessage> Unpin for Mailbox<SenderMessage, ReceiverMessage>

§

impl<SenderMessage, ReceiverMessage> UnwindSafe for Mailbox<SenderMessage, ReceiverMessage>

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.