|
1 | 1 | use std::cell::UnsafeCell;
|
2 |
| -use std::fmt; |
| 2 | +use std::error::Error; |
| 3 | +use std::fmt::{Debug, Display, self}; |
3 | 4 | use std::future::Future;
|
4 | 5 | use std::isize;
|
5 | 6 | use std::marker::PhantomData;
|
@@ -192,6 +193,27 @@ impl<T> Sender<T> {
|
192 | 193 | .await
|
193 | 194 | }
|
194 | 195 |
|
| 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 | + |
195 | 217 | /// Returns the channel capacity.
|
196 | 218 | ///
|
197 | 219 | /// # Examples
|
@@ -405,6 +427,30 @@ impl<T> Receiver<T> {
|
405 | 427 | .await
|
406 | 428 | }
|
407 | 429 |
|
| 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 | + |
408 | 454 | /// Returns the channel capacity.
|
409 | 455 | ///
|
410 | 456 | /// # Examples
|
@@ -932,20 +978,52 @@ impl<T> Drop for Channel<T> {
|
932 | 978 | }
|
933 | 979 | }
|
934 | 980 |
|
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> { |
937 | 983 | /// The channel is full but not disconnected.
|
938 | 984 | Full(T),
|
939 | 985 |
|
940 | 986 | /// The channel is full and disconnected.
|
941 | 987 | Disconnected(T),
|
942 | 988 | }
|
943 | 989 |
|
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 { |
946 | 1013 | /// The channel is empty but not disconnected.
|
947 | 1014 | Empty,
|
948 | 1015 |
|
949 | 1016 | /// The channel is empty and disconnected.
|
950 | 1017 | Disconnected,
|
951 | 1018 | }
|
| 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 | +} |
0 commit comments