From f12b28b121b7ee38cfd5d8124c10e279c6b91ef5 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 18 Aug 2022 17:18:37 +0200 Subject: [PATCH] Make ignoring server reply easier --- src/conn.rs | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/conn.rs b/src/conn.rs index 8d00989..05054d5 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use std::convert::Infallible; use std::error; +use std::future::Future; use std::time::Duration; use futures::channel::oneshot; @@ -388,15 +389,13 @@ pub struct ConnTx { } impl ConnTx { - pub async fn send(&self, cmd: C) -> Result + async fn finish_send( + rx: oneshot::Receiver>>, + ) -> Result where - C: Command + Into, + C: Command, C::Reply: TryFrom, { - let (tx, rx) = oneshot::channel(); - self.event_tx - .send(Event::SendCmd(cmd.into(), tx)) - .map_err(|_| Error::ConnectionClosed)?; let pending_reply = rx .await // This should only happen if something goes wrong during encoding @@ -414,6 +413,27 @@ impl ConnTx { data.try_into().map_err(|_| Error::IncorrectReplyType) } + /// Send a command to the server. + /// + /// Returns a future containing the server's reply. This future does not + /// have to be awaited and can be safely ignored if you are not interested + /// in the reply. + /// + /// This function may return before the command was sent. To ensure that it + /// was sent, await the returned future first. + /// + /// When called multiple times, this function guarantees that the commands + /// are sent in the order that the function is called. + pub fn send(&self, cmd: C) -> impl Future> + where + C: Command + Into, + C::Reply: TryFrom, + { + let (tx, rx) = oneshot::channel(); + let _ = self.event_tx.send(Event::SendCmd(cmd.into(), tx)); + Self::finish_send::(rx) + } + pub async fn status(&self) -> Result { let (tx, rx) = oneshot::channel(); self.event_tx