From 975576952f33c64e4faaa616f67ae9d6b596e4aa Mon Sep 17 00:00:00 2001 From: "Michael P. Jung" Date: Tue, 10 Dec 2019 17:01:23 +0100 Subject: Add Mutex::try_lock and (Unbounded)Receiver::try_recv (#1939) --- tokio/src/sync/mpsc/unbounded.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'tokio/src/sync/mpsc/unbounded.rs') diff --git a/tokio/src/sync/mpsc/unbounded.rs b/tokio/src/sync/mpsc/unbounded.rs index 07a173c2..4a6ba8ee 100644 --- a/tokio/src/sync/mpsc/unbounded.rs +++ b/tokio/src/sync/mpsc/unbounded.rs @@ -1,6 +1,6 @@ use crate::loom::sync::atomic::AtomicUsize; use crate::sync::mpsc::chan; -use crate::sync::mpsc::error::SendError; +use crate::sync::mpsc::error::{SendError, TryRecvError}; use std::fmt; use std::task::{Context, Poll}; @@ -123,6 +123,21 @@ impl UnboundedReceiver { poll_fn(|cx| self.poll_recv(cx)).await } + /// Attempts to return a pending value on this receiver without blocking. + /// + /// This method will never block the caller in order to wait for data to + /// become available. Instead, this will always return immediately with + /// a possible option of pending data on the channel. + /// + /// This is useful for a flavor of "optimistic check" before deciding to + /// block on a receiver. + /// + /// Compared with recv, this function has two failure cases instead of + /// one (one for disconnection, one for an empty buffer). + pub fn try_recv(&mut self) -> Result { + self.chan.try_recv() + } + /// Closes the receiving half of a channel, without dropping it. /// /// This prevents any further messages from being sent on the channel while -- cgit v1.2.3