1use super::{dealloc, Channel};
2use core::fmt;
3use core::mem;
4use core::ptr::NonNull;
5
6pub struct SendError<T> {
12 channel_ptr: NonNull<Channel<T>>,
13}
14
15unsafe impl<T: Send> Send for SendError<T> {}
16unsafe impl<T: Sync> Sync for SendError<T> {}
17
18impl<T> SendError<T> {
19 pub(crate) const unsafe fn new(channel_ptr: NonNull<Channel<T>>) -> Self {
26 Self { channel_ptr }
27 }
28
29 #[inline]
31 pub fn into_inner(self) -> T {
32 let channel_ptr = self.channel_ptr;
33
34 mem::forget(self);
36
37 let channel: &Channel<T> = unsafe { channel_ptr.as_ref() };
39
40 let message = unsafe { channel.take_message() };
43
44 unsafe { dealloc(channel_ptr) };
46
47 message
48 }
49
50 #[inline]
52 pub fn as_inner(&self) -> &T {
53 unsafe { self.channel_ptr.as_ref().message().assume_init_ref() }
54 }
55}
56
57impl<T> Drop for SendError<T> {
58 fn drop(&mut self) {
59 unsafe {
62 self.channel_ptr.as_ref().drop_message();
63 dealloc(self.channel_ptr);
64 }
65 }
66}
67
68impl<T> fmt::Display for SendError<T> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 "sending on a closed channel".fmt(f)
71 }
72}
73
74impl<T> fmt::Debug for SendError<T> {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 write!(f, "SendError<{}>(_)", stringify!(T))
77 }
78}
79
80#[cfg(feature = "std")]
81impl<T> std::error::Error for SendError<T> {}
82
83#[cfg(any(feature = "std", feature = "async"))]
88#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
89pub struct RecvError;
90
91#[cfg(any(feature = "std", feature = "async"))]
92impl fmt::Display for RecvError {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 "receiving on a closed channel".fmt(f)
95 }
96}
97
98#[cfg(feature = "std")]
99impl std::error::Error for RecvError {}
100
101#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
104pub enum TryRecvError {
105 Empty,
107
108 Disconnected,
111}
112
113impl fmt::Display for TryRecvError {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 let msg = match self {
116 TryRecvError::Empty => "receiving on an empty channel",
117 TryRecvError::Disconnected => "receiving on a closed channel",
118 };
119 msg.fmt(f)
120 }
121}
122
123#[cfg(feature = "std")]
124impl std::error::Error for TryRecvError {}
125
126#[cfg(feature = "std")]
129#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
130pub enum RecvTimeoutError {
131 Timeout,
133
134 Disconnected,
137}
138
139#[cfg(feature = "std")]
140impl fmt::Display for RecvTimeoutError {
141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142 let msg = match self {
143 RecvTimeoutError::Timeout => "timed out waiting on channel",
144 RecvTimeoutError::Disconnected => "channel is empty and sending half is closed",
145 };
146 msg.fmt(f)
147 }
148}
149
150#[cfg(feature = "std")]
151impl std::error::Error for RecvTimeoutError {}