commonware_p2p/authenticated/
mailbox.rs

1use futures::channel::mpsc;
2
3/// A mailbox wraps a sender for messages of type `T`.
4#[derive(Debug)]
5pub struct Mailbox<T>(pub(crate) mpsc::Sender<T>);
6
7impl<T> Mailbox<T> {
8    /// Returns a new mailbox with the given sender.
9    pub fn new(size: usize) -> (Self, mpsc::Receiver<T>) {
10        let (sender, receiver) = mpsc::channel(size);
11        (Self(sender), receiver)
12    }
13}
14
15impl<T> Clone for Mailbox<T> {
16    fn clone(&self) -> Self {
17        Self(self.0.clone())
18    }
19}
20
21/// A mailbox wraps an unbounded sender for messages of type `T`.
22#[derive(Debug)]
23pub struct UnboundedMailbox<T>(pub(crate) mpsc::UnboundedSender<T>);
24
25impl<T> UnboundedMailbox<T> {
26    /// Returns a new mailbox with the given sender.
27    pub fn new() -> (Self, mpsc::UnboundedReceiver<T>) {
28        let (sender, receiver) = mpsc::unbounded();
29        (Self(sender), receiver)
30    }
31}
32
33impl<T> Clone for UnboundedMailbox<T> {
34    fn clone(&self) -> Self {
35        Self(self.0.clone())
36    }
37}