Skip to content

Commit b8f1ac6

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

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
@@ -409,6 +431,30 @@ impl<T> Receiver<T> {
409431
.await
410432
}
411433

434+
/// Attempts to receive a message from the channel.
435+
///
436+
/// If the channel is empty, this method will return an error.
437+
///
438+
/// # Examples
439+
///
440+
/// ```
441+
/// # async_std::task::block_on(async {
442+
/// #
443+
/// use async_std::sync::channel;
444+
///
445+
/// let (s, r) = channel(1);
446+
///
447+
/// s.send(1u8).await;
448+
///
449+
/// assert!(r.try_recv().is_ok());
450+
/// assert!(r.try_recv().is_err());
451+
/// #
452+
/// # })
453+
/// ```
454+
pub fn try_recv(&self) -> Result<T, TryRecvError> {
455+
self.channel.try_recv()
456+
}
457+
412458
/// Returns the channel capacity.
413459
///
414460
/// # Examples
@@ -936,20 +982,52 @@ impl<T> Drop for Channel<T> {
936982
}
937983
}
938984

939-
/// An error returned from the `try_send()` method.
940-
enum TrySendError<T> {
985+
/// An error returned from the `try_send` method.
986+
pub enum TrySendError<T> {
941987
/// The channel is full but not disconnected.
942988
Full(T),
943989

944990
/// The channel is full and disconnected.
945991
Disconnected(T),
946992
}
947993

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

9531020
/// The channel is empty and disconnected.
9541021
Disconnected,
9551022
}
1023+
1024+
impl Error for TryRecvError {}
1025+
1026+
impl Display for TryRecvError {
1027+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1028+
match self {
1029+
Self::Empty => Display::fmt("The channel is empty.", f),
1030+
Self::Disconnected => Display::fmt("The channel is empty and disconnected.", f),
1031+
}
1032+
}
1033+
}

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)