expose try_recv and try_send on channels

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
mio-0-7
Yoshua Wuyts 5 years ago
parent 49dd02b4de
commit 32dce319d3

@ -1,5 +1,6 @@
use std::cell::UnsafeCell;
use std::fmt;
use std::error::Error;
use std::fmt::{Debug, Display, self};
use std::future::Future;
use std::isize;
use std::marker::PhantomData;
@ -192,6 +193,27 @@ impl<T> Sender<T> {
.await
}
/// Attempts to send a message into the channel.
///
/// If the channel is full, this method will return an error.
///
/// # Examples
///
/// ```
/// # async_std::task::block_on(async {
/// #
/// use async_std::sync::channel;
///
/// let (s, r) = channel(1);
/// assert!(s.try_send(1).is_ok());
/// assert!(s.try_send(2).is_err());
/// #
/// # })
/// ```
pub fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
self.channel.try_send(msg)
}
/// Returns the channel capacity.
///
/// # Examples
@ -409,6 +431,30 @@ impl<T> Receiver<T> {
.await
}
/// Attempts to receive a message from the channel.
///
/// If the channel is empty, this method will return an error.
///
/// # Examples
///
/// ```
/// # async_std::task::block_on(async {
/// #
/// use async_std::sync::channel;
///
/// let (s, r) = channel(1);
///
/// s.send(1u8).await;
///
/// assert!(r.try_recv().is_ok());
/// assert!(r.try_recv().is_err());
/// #
/// # })
/// ```
pub fn try_recv(&self) -> Result<T, TryRecvError> {
self.channel.try_recv()
}
/// Returns the channel capacity.
///
/// # Examples
@ -936,8 +982,8 @@ impl<T> Drop for Channel<T> {
}
}
/// An error returned from the `try_send()` method.
enum TrySendError<T> {
/// An error returned from the `try_send` method.
pub enum TrySendError<T> {
/// The channel is full but not disconnected.
Full(T),
@ -945,11 +991,43 @@ enum TrySendError<T> {
Disconnected(T),
}
/// An error returned from the `try_recv()` method.
enum TryRecvError {
impl<T> Error for TrySendError<T> {}
impl<T> Debug for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Full(_) => Debug::fmt("Full<T>", f),
Self::Disconnected(_) => Debug::fmt("Disconnected<T>", f),
}
}
}
impl<T> Display for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Full(_) => Display::fmt("The channel is full.", f),
Self::Disconnected(_) => Display::fmt("The channel is full and disconnected.", f),
}
}
}
/// An error returned from the `try_recv` method.
#[derive(Debug)]
pub enum TryRecvError {
/// The channel is empty but not disconnected.
Empty,
/// The channel is empty and disconnected.
Disconnected,
}
impl Error for TryRecvError {}
impl Display for TryRecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => Display::fmt("The channel is empty.", f),
Self::Disconnected => Display::fmt("The channel is empty and disconnected.", f),
}
}
}

@ -184,7 +184,7 @@ mod rwlock;
cfg_unstable! {
pub use barrier::{Barrier, BarrierWaitResult};
pub use channel::{channel, Sender, Receiver};
pub use channel::{channel, Sender, Receiver, TryRecvError, TrySendError};
mod barrier;
mod channel;

Loading…
Cancel
Save