1 //! Channel error types. 2 3 use std::error::Error; 4 use std::fmt; 5 6 /// Error returned by the `Sender`. 7 #[derive(Debug)] 8 pub struct SendError<T>(pub T); 9 10 impl<T> fmt::Display for SendError<T> { fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result11 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 12 write!(fmt, "channel closed") 13 } 14 } 15 16 impl<T: fmt::Debug> std::error::Error for SendError<T> {} 17 18 // ===== TrySendError ===== 19 20 /// This enumeration is the list of the possible error outcomes for the 21 /// [try_send](super::Sender::try_send) method. 22 #[derive(Debug)] 23 pub enum TrySendError<T> { 24 /// The data could not be sent on the channel because the channel is 25 /// currently full and sending would require blocking. 26 Full(T), 27 28 /// The receive half of the channel was explicitly closed or has been 29 /// dropped. 30 Closed(T), 31 } 32 33 impl<T: fmt::Debug> Error for TrySendError<T> {} 34 35 impl<T> fmt::Display for TrySendError<T> { fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result36 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 37 write!( 38 fmt, 39 "{}", 40 match self { 41 TrySendError::Full(..) => "no available capacity", 42 TrySendError::Closed(..) => "channel closed", 43 } 44 ) 45 } 46 } 47 48 impl<T> From<SendError<T>> for TrySendError<T> { from(src: SendError<T>) -> TrySendError<T>49 fn from(src: SendError<T>) -> TrySendError<T> { 50 TrySendError::Closed(src.0) 51 } 52 } 53 54 // ===== TryRecvError ===== 55 56 /// Error returned by `try_recv`. 57 #[derive(PartialEq, Eq, Clone, Copy, Debug)] 58 pub enum TryRecvError { 59 /// This **channel** is currently empty, but the **Sender**(s) have not yet 60 /// disconnected, so data may yet become available. 61 Empty, 62 /// The **channel**'s sending half has become disconnected, and there will 63 /// never be any more data received on it. 64 Disconnected, 65 } 66 67 impl fmt::Display for TryRecvError { fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result68 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 69 match *self { 70 TryRecvError::Empty => "receiving on an empty channel".fmt(fmt), 71 TryRecvError::Disconnected => "receiving on a closed channel".fmt(fmt), 72 } 73 } 74 } 75 76 impl Error for TryRecvError {} 77 78 // ===== RecvError ===== 79 80 /// Error returned by `Receiver`. 81 #[derive(Debug)] 82 #[doc(hidden)] 83 #[deprecated(note = "This type is unused because recv returns an Option.")] 84 pub struct RecvError(()); 85 86 #[allow(deprecated)] 87 impl fmt::Display for RecvError { fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result88 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 89 write!(fmt, "channel closed") 90 } 91 } 92 93 #[allow(deprecated)] 94 impl Error for RecvError {} 95 96 cfg_time! { 97 // ===== SendTimeoutError ===== 98 99 #[derive(Debug)] 100 /// Error returned by [`Sender::send_timeout`](super::Sender::send_timeout)]. 101 pub enum SendTimeoutError<T> { 102 /// The data could not be sent on the channel because the channel is 103 /// full, and the timeout to send has elapsed. 104 Timeout(T), 105 106 /// The receive half of the channel was explicitly closed or has been 107 /// dropped. 108 Closed(T), 109 } 110 111 impl<T: fmt::Debug> Error for SendTimeoutError<T> {} 112 113 impl<T> fmt::Display for SendTimeoutError<T> { 114 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 115 write!( 116 fmt, 117 "{}", 118 match self { 119 SendTimeoutError::Timeout(..) => "timed out waiting on send operation", 120 SendTimeoutError::Closed(..) => "channel closed", 121 } 122 ) 123 } 124 } 125 } 126