From 9994d24749127681b65ff3feb625a33b056e4515 Mon Sep 17 00:00:00 2001 From: Jakub Horak Date: Thu, 8 Jun 2023 07:37:00 +0200 Subject: [PATCH] Add `len` method for `UnboundedSender` Add `len` method to inspect how many messages are enqueued in the message queue. --- futures-channel/src/mpsc/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/futures-channel/src/mpsc/mod.rs b/futures-channel/src/mpsc/mod.rs index edbac7aa3..6e88c2626 100644 --- a/futures-channel/src/mpsc/mod.rs +++ b/futures-channel/src/mpsc/mod.rs @@ -836,6 +836,15 @@ impl UnboundedSender { let ptr = self.0.as_ref().map(|inner| inner.ptr()); ptr.hash(hasher); } + + /// Return the number of messages in the queue or 0 if channel is disconnected. + pub fn len(&self) -> usize { + if let Some(sender) = &self.0 { + decode_state(sender.inner.state.load(SeqCst)).num_messages + } else { + 0 + } + } } impl Clone for Sender {