1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use super::*;

/// Shorthand for an [`Error`] resulting from a symmetrically serialized/encoded connection.
pub type SymmetricalError<F, E> = Error<F, F, E, E>;

/// An error during operations on a [`Sender`] or [`Receiver`], unifying [`SendError`] and
/// [`RecvError`].
pub enum Error<F: Serializer, G: Deserializer<D::Item>, E: Encoder<F::Output>, D: Decoder> {
    /// An error occurred while attempting to [`send`](Transmit::send) a value.
    Send(SendError<F, E>),
    /// An error occurred while attempting to [`recv`](Receive::recv) a value.
    Recv(RecvError<G, D>),
}

/// An error while sending on a [`Sender`].
pub enum SendError<F: Serializer, E: Encoder<F::Output>> {
    /// An error occurred while attempting to serialize a value.
    Serialize(F::Error),
    /// An error occurred while attempting to encode and transmit a serialized value as a frame.
    Encode(E::Error),
}

/// An error while receiving from a [`Receiver`].
pub enum RecvError<F: Deserializer<D::Item>, D: Decoder> {
    /// An error occurred while attempting to deserialize a value.
    Deserialize(F::Error),
    /// An error occurred while attempting to receive and decode a serialized value as a frame.
    Decode(D::Error),
    /// The underlying stream was closed.
    Closed,
}

impl<F, G, E, D> From<SendError<F, E>> for Error<F, G, E, D>
where
    F: Serializer,
    G: Deserializer<D::Item>,
    E: Encoder<F::Output>,
    D: Decoder,
{
    fn from(err: SendError<F, E>) -> Self {
        Error::Send(err)
    }
}

impl<F, G, E, D> From<RecvError<G, D>> for Error<F, G, E, D>
where
    F: Serializer,
    G: Deserializer<D::Item>,
    E: Encoder<F::Output>,
    D: Decoder,
{
    fn from(err: RecvError<G, D>) -> Self {
        Error::Recv(err)
    }
}

impl<F, G, E, D> std::error::Error for Error<F, G, E, D>
where
    F: Serializer,
    G: Deserializer<D::Item>,
    E: Encoder<F::Output>,
    D: Decoder,
    F::Error: Debug + Display,
    G::Error: Debug + Display,
    E::Error: Debug + Display,
    D::Error: Debug + Display,
{
}

impl<F: Serializer, G: Deserializer<D::Item>, E: Encoder<F::Output>, D: Decoder> Debug
    for Error<F, G, E, D>
where
    F::Error: Debug,
    G::Error: Debug,
    E::Error: Debug,
    D::Error: Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::Send(err) => write!(f, "{:?}", err),
            Error::Recv(err) => write!(f, "{:?}", err),
        }
    }
}

impl<F: Serializer, G: Deserializer<D::Item>, E: Encoder<F::Output>, D: Decoder> Display
    for Error<F, G, E, D>
where
    F::Error: Display,
    G::Error: Display,
    E::Error: Display,
    D::Error: Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::Send(err) => write!(f, "{}", err),
            Error::Recv(err) => write!(f, "{}", err),
        }
    }
}

impl<F: Serializer, E: Encoder<F::Output>> Debug for SendError<F, E>
where
    F::Error: Debug,
    E::Error: Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            SendError::Serialize(err) => write!(f, "{:?}", err),
            SendError::Encode(err) => write!(f, "{:?}", err),
        }
    }
}

impl<F: Serializer, E: Encoder<F::Output>> Display for SendError<F, E>
where
    F::Error: Display,
    E::Error: Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            SendError::Serialize(err) => write!(f, "{}", err),
            SendError::Encode(err) => write!(f, "{}", err),
        }
    }
}

impl<F, E> std::error::Error for SendError<F, E>
where
    F: Serializer,
    E: Encoder<F::Output>,
    F::Error: Display + Debug,
    E::Error: Display + Debug,
{
}

impl<F: Deserializer<D::Item>, D: Decoder> Debug for RecvError<F, D>
where
    F::Error: Debug,
    D::Error: Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            RecvError::Deserialize(err) => write!(f, "{:?}", err),
            RecvError::Decode(err) => write!(f, "{:?}", err),
            RecvError::Closed => write!(f, "Closed"),
        }
    }
}

impl<F: Deserializer<D::Item>, D: Decoder> Display for RecvError<F, D>
where
    F::Error: Display,
    D::Error: Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            RecvError::Deserialize(err) => write!(f, "{}", err),
            RecvError::Decode(err) => write!(f, "{}", err),
            RecvError::Closed => write!(f, "connection closed"),
        }
    }
}

impl<F, D> std::error::Error for RecvError<F, D>
where
    F: Deserializer<D::Item>,
    D: Decoder,
    F::Error: Display + Debug,
    D::Error: Display + Debug,
{
}

impl<F, G, E, D> Clone for Error<F, G, E, D>
where
    F: Serializer,
    G: Deserializer<D::Item>,
    E: Encoder<F::Output>,
    D: Decoder,
    F::Error: Clone,
    G::Error: Clone,
    E::Error: Clone,
    D::Error: Clone,
{
    fn clone(&self) -> Self {
        match self {
            Error::Send(err) => Error::Send(err.clone()),
            Error::Recv(err) => Error::Recv(err.clone()),
        }
    }
}

impl<F, E> Clone for SendError<F, E>
where
    F: Serializer,
    E: Encoder<F::Output>,
    F::Error: Clone,
    E::Error: Clone,
{
    fn clone(&self) -> Self {
        match self {
            SendError::Serialize(err) => SendError::Serialize(err.clone()),
            SendError::Encode(err) => SendError::Encode(err.clone()),
        }
    }
}

impl<F, D> Clone for RecvError<F, D>
where
    F: Deserializer<D::Item>,
    D: Decoder,
    F::Error: Clone,
    D::Error: Clone,
{
    fn clone(&self) -> Self {
        match self {
            RecvError::Deserialize(err) => RecvError::Deserialize(err.clone()),
            RecvError::Decode(err) => RecvError::Decode(err.clone()),
            RecvError::Closed => RecvError::Closed,
        }
    }
}