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#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
88pub struct RecvError;
89
90impl fmt::Display for RecvError {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 "receiving on a closed channel".fmt(f)
93 }
94}
95
96#[cfg(feature = "std")]
97impl std::error::Error for RecvError {}
98
99#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
102pub enum TryRecvError {
103 Empty,
105
106 Disconnected,
109}
110
111impl fmt::Display for TryRecvError {
112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113 let msg = match self {
114 TryRecvError::Empty => "receiving on an empty channel",
115 TryRecvError::Disconnected => "receiving on a closed channel",
116 };
117 msg.fmt(f)
118 }
119}
120
121#[cfg(feature = "std")]
122impl std::error::Error for TryRecvError {}
123
124#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
127pub enum RecvTimeoutError {
128 Timeout,
130
131 Disconnected,
134}
135
136impl fmt::Display for RecvTimeoutError {
137 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138 let msg = match self {
139 RecvTimeoutError::Timeout => "timed out waiting on channel",
140 RecvTimeoutError::Disconnected => "channel is empty and sending half is closed",
141 };
142 msg.fmt(f)
143 }
144}
145
146#[cfg(feature = "std")]
147impl std::error::Error for RecvTimeoutError {}