errors.rs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //! Errors returned by channels.
  2. use core::fmt;
  3. /// Error returned by the [`Sender::send_timeout`] or [`Sender::send_ref_timeout`]
  4. /// (and [`StaticSender::send_timeout`]/[`StaticSender::send_ref_timeout`]) methods
  5. /// (blocking only).
  6. ///
  7. /// [`Sender::send_timeout`]: super::blocking::Sender::send_timeout
  8. /// [`Sender::send_ref_timeout`]: super::blocking::Sender::send_ref_timeout
  9. /// [`StaticSender::send_timeout`]: super::blocking::StaticSender::send_timeout
  10. /// [`StaticSender::send_ref_timeout`]: super::blocking::StaticSender::send_ref_timeout
  11. #[cfg(feature = "std")]
  12. #[non_exhaustive]
  13. #[derive(PartialEq, Eq)]
  14. pub enum SendTimeoutError<T = ()> {
  15. /// The data could not be sent on the channel because the channel is
  16. /// currently full and sending would require waiting for capacity.
  17. Timeout(T),
  18. /// The data could not be sent because the [`Receiver`] half of the channel
  19. /// has been dropped.
  20. ///
  21. /// [`Receiver`]: super::Receiver
  22. Closed(T),
  23. }
  24. /// Error returned by the [`Sender::try_send`] or [`Sender::try_send_ref`] (and
  25. /// [`StaticSender::try_send`]/[`StaticSender::try_send_ref`]) methods.
  26. ///
  27. /// [`Sender::try_send`]: super::Sender::try_send
  28. /// [`Sender::try_send_ref`]: super::Sender::try_send_ref
  29. /// [`StaticSender::try_send`]: super::StaticSender::try_send
  30. /// [`StaticSender::try_send_ref`]: super::StaticSender::try_send_ref
  31. #[non_exhaustive]
  32. #[derive(PartialEq, Eq)]
  33. pub enum TrySendError<T = ()> {
  34. /// The data could not be sent on the channel because the channel is
  35. /// currently full and sending would require waiting for capacity.
  36. Full(T),
  37. /// The data could not be sent because the [`Receiver`] half of the channel
  38. /// has been dropped.
  39. ///
  40. /// [`Receiver`]: super::Receiver
  41. Closed(T),
  42. }
  43. /// Error returned by the [`Receiver::recv_timeout`] and [`Receiver::recv_ref_timeout`] methods
  44. /// (blocking only).
  45. ///
  46. /// [`Receiver::recv_timeout`]: super::blocking::Receiver::recv_timeout
  47. /// [`Receiver::recv_ref_timeout`]: super::blocking::Receiver::recv_ref_timeout
  48. #[cfg(feature = "std")]
  49. #[non_exhaustive]
  50. #[derive(Debug, PartialEq, Eq)]
  51. pub enum RecvTimeoutError {
  52. /// The timeout elapsed before data could be received.
  53. Timeout,
  54. /// The channel is closed.
  55. Closed,
  56. }
  57. /// Error returned by the [`Receiver::try_recv`] and [`Receiver::try_recv_ref`] methods.
  58. ///
  59. /// [`Receiver::try_recv`]: super::Receiver::try_recv
  60. /// [`Receiver::try_recv_ref`]: super::Receiver::try_recv_ref
  61. #[non_exhaustive]
  62. #[derive(Debug, PartialEq, Eq)]
  63. pub enum TryRecvError {
  64. /// The channel is empty.
  65. Empty,
  66. /// The channel is closed.
  67. Closed,
  68. }
  69. /// Error returned by [`Sender::send`] or [`Sender::send_ref`] (and
  70. /// [`StaticSender::send`]/[`StaticSender::send_ref`]), if the
  71. /// [`Receiver`] half of the channel has been dropped.
  72. ///
  73. /// [`Sender::send`]: super::Sender::send
  74. /// [`Sender::send_ref`]: super::Sender::send_ref
  75. /// [`StaticSender::send`]: super::StaticSender::send
  76. /// [`StaticSender::send_ref`]: super::StaticSender::send_ref
  77. /// [`Receiver`]: super::Receiver
  78. #[derive(PartialEq, Eq)]
  79. pub struct Closed<T = ()>(pub(crate) T);
  80. // === impl Closed ===
  81. impl<T> Closed<T> {
  82. /// Unwraps the inner `T` value held by this error.
  83. ///
  84. /// This method allows recovering the original message when sending to a
  85. /// channel has failed.
  86. pub fn into_inner(self) -> T {
  87. self.0
  88. }
  89. }
  90. impl<T> fmt::Debug for Closed<T> {
  91. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  92. f.write_str("Closed(..)")
  93. }
  94. }
  95. impl<T> fmt::Display for Closed<T> {
  96. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  97. f.write_str("channel closed")
  98. }
  99. }
  100. #[cfg(feature = "std")]
  101. impl<T> std::error::Error for Closed<T> {}
  102. // === impl SendTimeoutError ===
  103. #[cfg(feature = "std")]
  104. impl SendTimeoutError {
  105. pub(crate) fn with_value<T>(self, value: T) -> SendTimeoutError<T> {
  106. match self {
  107. Self::Timeout(()) => SendTimeoutError::Timeout(value),
  108. Self::Closed(()) => SendTimeoutError::Closed(value),
  109. }
  110. }
  111. }
  112. #[cfg(feature = "std")]
  113. impl<T> SendTimeoutError<T> {
  114. /// Returns `true` if this error was returned because the channel is still
  115. /// full after the timeout has elapsed.
  116. pub fn is_timeout(&self) -> bool {
  117. matches!(self, Self::Timeout(_))
  118. }
  119. /// Returns `true` if this error was returned because the channel has closed
  120. /// (e.g. the [`Receiver`] end has been dropped).
  121. ///
  122. /// If this returns `true`, no future [`try_send`] or [`send`] operation on
  123. /// this channel will succeed.
  124. ///
  125. /// [`Receiver`]: super::blocking::Receiver
  126. /// [`try_send`]: super::blocking::Sender::try_send
  127. /// [`send`]: super::blocking::Sender::send
  128. /// [`Receiver`]: super::blocking::Receiver
  129. pub fn is_closed(&self) -> bool {
  130. matches!(self, Self::Timeout(_))
  131. }
  132. /// Unwraps the inner `T` value held by this error.
  133. ///
  134. /// This method allows recovering the original message when sending to a
  135. /// channel has failed.
  136. pub fn into_inner(self) -> T {
  137. match self {
  138. Self::Timeout(val) => val,
  139. Self::Closed(val) => val,
  140. }
  141. }
  142. }
  143. #[cfg(feature = "std")]
  144. impl<T> fmt::Debug for SendTimeoutError<T> {
  145. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  146. f.write_str(match self {
  147. Self::Timeout(_) => "SendTimeoutError::Timeout(..)",
  148. Self::Closed(_) => "SendTimeoutError::Closed(..)",
  149. })
  150. }
  151. }
  152. #[cfg(feature = "std")]
  153. impl<T> fmt::Display for SendTimeoutError<T> {
  154. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  155. f.write_str(match self {
  156. Self::Timeout(_) => "timed out waiting for channel capacity",
  157. Self::Closed(_) => "channel closed",
  158. })
  159. }
  160. }
  161. #[cfg(feature = "std")]
  162. impl<T> std::error::Error for SendTimeoutError<T> {}
  163. // === impl TrySendError ===
  164. impl TrySendError {
  165. pub(crate) fn with_value<T>(self, value: T) -> TrySendError<T> {
  166. match self {
  167. Self::Full(()) => TrySendError::Full(value),
  168. Self::Closed(()) => TrySendError::Closed(value),
  169. }
  170. }
  171. }
  172. impl<T> TrySendError<T> {
  173. /// Returns `true` if this error was returned because the channel was at
  174. /// capacity.
  175. pub fn is_full(&self) -> bool {
  176. matches!(self, Self::Full(_))
  177. }
  178. /// Returns `true` if this error was returned because the channel has closed
  179. /// (e.g. the [`Receiver`] end has been dropped).
  180. ///
  181. /// If this returns `true`, no future [`try_send`] or [`send`] operation on
  182. /// this channel will succeed.
  183. ///
  184. /// [`Receiver`]: super::Receiver
  185. /// [`try_send`]: super::Sender::try_send
  186. /// [`send`]: super::Sender::send
  187. /// [`Receiver`]: super::Receiver
  188. pub fn is_closed(&self) -> bool {
  189. matches!(self, Self::Full(_))
  190. }
  191. /// Unwraps the inner `T` value held by this error.
  192. ///
  193. /// This method allows recovering the original message when sending to a
  194. /// channel has failed.
  195. pub fn into_inner(self) -> T {
  196. match self {
  197. Self::Full(val) => val,
  198. Self::Closed(val) => val,
  199. }
  200. }
  201. }
  202. impl<T> fmt::Debug for TrySendError<T> {
  203. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  204. f.write_str(match self {
  205. Self::Full(_) => "TrySendError::Full(..)",
  206. Self::Closed(_) => "TrySendError::Closed(..)",
  207. })
  208. }
  209. }
  210. impl<T> fmt::Display for TrySendError<T> {
  211. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  212. f.write_str(match self {
  213. Self::Full(_) => "no available capacity",
  214. Self::Closed(_) => "channel closed",
  215. })
  216. }
  217. }
  218. #[cfg(feature = "std")]
  219. impl<T> std::error::Error for TrySendError<T> {}
  220. // === impl RecvTimeoutError ===
  221. #[cfg(feature = "std")]
  222. impl std::error::Error for RecvTimeoutError {}
  223. #[cfg(feature = "std")]
  224. impl fmt::Display for RecvTimeoutError {
  225. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  226. f.write_str(match self {
  227. Self::Timeout => "timed out waiting on channel",
  228. Self::Closed => "channel closed",
  229. })
  230. }
  231. }
  232. // == impl TryRecvError ==
  233. #[cfg(feature = "std")]
  234. impl std::error::Error for TryRecvError {}
  235. impl fmt::Display for TryRecvError {
  236. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  237. f.write_str(match self {
  238. Self::Empty => "channel is empty",
  239. Self::Closed => "channel closed",
  240. })
  241. }
  242. }