From 8a93fed5f1bd197e131deca058cb6dc32cc96cbf Mon Sep 17 00:00:00 2001 From: Joshua Chapman Date: Thu, 9 Jan 2025 11:54:34 +0100 Subject: [PATCH] feat(acked): add a non consuming method to the notice This will make it possible to not block while trying to wait for the acknowledgement of multiple packets. For example, you are storing the `NoticeFutures` in a `Vec`, and you want to find the first acknowledge packet. This would be similar to the `try_recv(&mut)` of the inner channel. --- rumqttc/src/notice.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rumqttc/src/notice.rs b/rumqttc/src/notice.rs index 3896e5c4d..a5a9afba0 100644 --- a/rumqttc/src/notice.rs +++ b/rumqttc/src/notice.rs @@ -42,6 +42,21 @@ impl NoticeFuture { pub async fn wait_async(self) -> NoticeResult { self.0.await? } + + /// Attempts to check if the broker acknowledged the packet, without blocking the current thread + /// or consuming the notice. + /// + /// It will return [`None`] if the packet wasn't acknowledged. + /// + /// Multiple calls to this functions can fail with [`NoticeError::Recv`] if the notice was + /// already waited and the packet was already acknowledged and [`Some`] value was returned. + pub fn try_wait(&mut self) -> Option { + match self.0.try_recv() { + Ok(res) => Some(res), + Err(oneshot::error::TryRecvError::Closed) => Some(Err(NoticeError::Recv)), + Err(oneshot::error::TryRecvError::Empty) => None, + } + } } #[derive(Debug)]