Skip to content

Commit

Permalink
Add len method for UnboundedSender
Browse files Browse the repository at this point in the history
Add `len` method to inspect how many messages are enqueued in the
message queue.
  • Loading branch information
jxhor committed Jul 21, 2023
1 parent 28cb6a2 commit 9994d24
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions futures-channel/src/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,15 @@ impl<T> UnboundedSender<T> {
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<T> Clone for Sender<T> {
Expand Down

0 comments on commit 9994d24

Please sign in to comment.