|
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
|
@@ -409,6 +431,30 @@ impl<T> Receiver<T> {
|
409 | 431 | .await
|
410 | 432 | }
|
411 | 433 |
|
| 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 | + |
412 | 458 | /// Returns the channel capacity.
|
413 | 459 | ///
|
414 | 460 | /// # Examples
|
@@ -936,20 +982,52 @@ impl<T> Drop for Channel<T> {
|
936 | 982 | }
|
937 | 983 | }
|
938 | 984 |
|
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> { |
941 | 987 | /// The channel is full but not disconnected.
|
942 | 988 | Full(T),
|
943 | 989 |
|
944 | 990 | /// The channel is full and disconnected.
|
945 | 991 | Disconnected(T),
|
946 | 992 | }
|
947 | 993 |
|
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 { |
950 | 1017 | /// The channel is empty but not disconnected.
|
951 | 1018 | Empty,
|
952 | 1019 |
|
953 | 1020 | /// The channel is empty and disconnected.
|
954 | 1021 | Disconnected,
|
955 | 1022 | }
|
| 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 | +} |
0 commit comments