use crate::{Error, Originator}; use commonware_codec::Codec; use commonware_cryptography::{Committable, Digestible, PublicKey}; use commonware_p2p::Recipients; use futures::{ channel::{mpsc, oneshot}, SinkExt, }; /// Messages that can be sent to a [Mailbox]. pub enum Message { Send { request: R, recipients: Recipients

, responder: oneshot::Sender, Error>>, }, Cancel { commitment: R::Commitment, }, } /// A mailbox that can be used to send and receive [Message]s. #[derive(Clone)] pub struct Mailbox { sender: mpsc::Sender>, } impl Mailbox { /// Creates a new [Mailbox] with the given [mpsc::Sender]. pub fn new(sender: mpsc::Sender>) -> Self { Self { sender } } } impl Originator for Mailbox { type Request = R; type PublicKey = P; async fn send(&mut self, recipients: Recipients

, request: R) -> Result, Error> { let (tx, rx) = oneshot::channel(); let _ = self .sender .send(Message::Send { request, recipients, responder: tx, }) .await; rx.await.map_err(|_| Error::Canceled)? } async fn cancel(&mut self, commitment: R::Commitment) { let _ = self.sender.send(Message::Cancel { commitment }).await; } }