forked from mirror/async-std
expose try_recv and try_send on channels
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
This commit is contained in:
parent
49dd02b4de
commit
32dce319d3
2 changed files with 84 additions and 6 deletions
|
@ -1,5 +1,6 @@
|
||||||
use std::cell::UnsafeCell;
|
use std::cell::UnsafeCell;
|
||||||
use std::fmt;
|
use std::error::Error;
|
||||||
|
use std::fmt::{Debug, Display, self};
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::isize;
|
use std::isize;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
@ -192,6 +193,27 @@ impl<T> Sender<T> {
|
||||||
.await
|
.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.
|
/// Returns the channel capacity.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -409,6 +431,30 @@ impl<T> Receiver<T> {
|
||||||
.await
|
.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.
|
/// Returns the channel capacity.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -936,8 +982,8 @@ impl<T> Drop for Channel<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An error returned from the `try_send()` method.
|
/// An error returned from the `try_send` method.
|
||||||
enum TrySendError<T> {
|
pub enum TrySendError<T> {
|
||||||
/// The channel is full but not disconnected.
|
/// The channel is full but not disconnected.
|
||||||
Full(T),
|
Full(T),
|
||||||
|
|
||||||
|
@ -945,11 +991,43 @@ enum TrySendError<T> {
|
||||||
Disconnected(T),
|
Disconnected(T),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An error returned from the `try_recv()` method.
|
impl<T> Error for TrySendError<T> {}
|
||||||
enum TryRecvError {
|
|
||||||
|
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.
|
/// The channel is empty but not disconnected.
|
||||||
Empty,
|
Empty,
|
||||||
|
|
||||||
/// The channel is empty and disconnected.
|
/// The channel is empty and disconnected.
|
||||||
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! {
|
cfg_unstable! {
|
||||||
pub use barrier::{Barrier, BarrierWaitResult};
|
pub use barrier::{Barrier, BarrierWaitResult};
|
||||||
pub use channel::{channel, Sender, Receiver};
|
pub use channel::{channel, Sender, Receiver, TryRecvError, TrySendError};
|
||||||
|
|
||||||
mod barrier;
|
mod barrier;
|
||||||
mod channel;
|
mod channel;
|
||||||
|
|
Loading…
Reference in a new issue