use std::any::type_name;
use std::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct SendError<T>(T);
impl<T> SendError<T> {
pub fn as_inner(&self) -> &T {
&self.0
}
pub fn into_inner(self) -> T {
self.0
}
pub(super) fn new(msg: T) -> SendError<T> {
SendError(msg)
}
}
impl<T> fmt::Display for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("sending on a closed channel")
}
}
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SendError<{}>(..)", type_name::<T>())
}
}
impl<T> std::error::Error for SendError<T> {}
#[derive(Clone, PartialEq, Eq)]
pub enum TrySendError<T> {
Full(T),
Disconnected(T),
}
impl<T> TrySendError<T> {
pub fn as_inner(&self) -> &T {
match self {
TrySendError::Full(msg) | TrySendError::Disconnected(msg) => msg,
}
}
pub fn into_inner(self) -> T {
match self {
TrySendError::Full(msg) | TrySendError::Disconnected(msg) => msg,
}
}
}
impl<T> fmt::Display for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
TrySendError::Full(_) => "sending on a full channel",
TrySendError::Disconnected(_) => "sending on a closed channel",
})
}
}
impl<T> fmt::Debug for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ty = type_name::<T>();
match self {
TrySendError::Full(_) => write!(f, "TrySendError<{ty}>::Full(..)"),
TrySendError::Disconnected(_) => write!(f, "TrySendError<{ty}>::Disconnected(..)"),
}
}
}
impl<T> std::error::Error for TrySendError<T> {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecvError {
Disconnected,
}
impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("receiving on a closed channel")
}
}
impl std::error::Error for RecvError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TryRecvError {
Empty,
Disconnected,
}
impl fmt::Display for TryRecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
TryRecvError::Empty => "receiving on an empty channel",
TryRecvError::Disconnected => "receiving on a closed channel",
})
}
}
impl std::error::Error for TryRecvError {}