Skip to content

Commit fce4cfa

Browse files
committed
expose try_recv and try_send on channels
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
1 parent ac7a796 commit fce4cfa

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

src/sync/channel.rs

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cell::UnsafeCell;
2-
use std::fmt;
2+
use std::error::Error;
3+
use std::fmt::{Debug, Display, self};
34
use std::future::Future;
45
use std::isize;
56
use std::marker::PhantomData;
@@ -192,6 +193,27 @@ impl<T> Sender<T> {
192193
.await
193194
}
194195

196+
/// Attempts to send a message into the channel.
197+
///
198+
/// If the channel is full, this method will return an error.
199+
///
200+
/// # Examples
201+
///
202+
/// ```
203+
/// # async_std::task::block_on(async {
204+
/// #
205+
/// use async_std::sync::channel;
206+
///
207+
/// let (s, r) = channel(1);
208+
/// assert!(s.try_send(1).is_ok());
209+
/// assert!(s.try_send(2).is_err());
210+
/// #
211+
/// # })
212+
/// ```
213+
pub fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
214+
self.channel.try_send(msg)
215+
}
216+
195217
/// Returns the channel capacity.
196218
///
197219
/// # Examples
@@ -405,6 +427,30 @@ impl<T> Receiver<T> {
405427
.await
406428
}
407429

430+
/// Attempts to receive a message from the channel.
431+
///
432+
/// If the channel is empty, this method will return an error.
433+
///
434+
/// # Examples
435+
///
436+
/// ```
437+
/// # async_std::task::block_on(async {
438+
/// #
439+
/// use async_std::sync::channel;
440+
///
441+
/// let (s, r) = channel(1);
442+
///
443+
/// s.send(1u8).await;
444+
///
445+
/// assert!(r.try_recv().is_ok());
446+
/// assert!(r.try_recv().is_err());
447+
/// #
448+
/// # })
449+
/// ```
450+
pub fn try_recv(&self) -> Result<T, TryRecvError> {
451+
self.channel.try_recv()
452+
}
453+
408454
/// Returns the channel capacity.
409455
///
410456
/// # Examples
@@ -932,20 +978,52 @@ impl<T> Drop for Channel<T> {
932978
}
933979
}
934980

935-
/// An error returned from the `try_send()` method.
936-
enum TrySendError<T> {
981+
/// An error returned from the `try_send` method.
982+
pub enum TrySendError<T> {
937983
/// The channel is full but not disconnected.
938984
Full(T),
939985

940986
/// The channel is full and disconnected.
941987
Disconnected(T),
942988
}
943989

944-
/// An error returned from the `try_recv()` method.
945-
enum TryRecvError {
990+
impl<T> Error for TrySendError<T> {}
991+
992+
impl<T> Debug for TrySendError<T> {
993+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
994+
match self {
995+
Self::Full(_) => Debug::fmt("Full<T>", f),
996+
Self::Disconnected(_) => Debug::fmt("Disconnected<T>", f),
997+
}
998+
}
999+
}
1000+
1001+
impl<T> Display for TrySendError<T> {
1002+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1003+
match self {
1004+
Self::Full(_) => Display::fmt("The channel is full.", f),
1005+
Self::Disconnected(_) => Display::fmt("The channel is full and disconnected.", f),
1006+
}
1007+
}
1008+
}
1009+
1010+
/// An error returned from the `try_recv` method.
1011+
#[derive(Debug)]
1012+
pub enum TryRecvError {
9461013
/// The channel is empty but not disconnected.
9471014
Empty,
9481015

9491016
/// The channel is empty and disconnected.
9501017
Disconnected,
9511018
}
1019+
1020+
impl Error for TryRecvError {}
1021+
1022+
impl Display for TryRecvError {
1023+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1024+
match self {
1025+
Self::Empty => Display::fmt("The channel is empty.", f),
1026+
Self::Disconnected => Display::fmt("The channel is empty and disconnected.", f),
1027+
}
1028+
}
1029+
}

src/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ mod rwlock;
184184

185185
cfg_unstable! {
186186
pub use barrier::{Barrier, BarrierWaitResult};
187-
pub use channel::{channel, Sender, Receiver};
187+
pub use channel::{channel, Sender, Receiver, TryRecvError, TrySendError};
188188

189189
mod barrier;
190190
mod channel;

0 commit comments

Comments
 (0)