use std::error::Error;
use std::fmt;
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct SendError<T>(pub T);
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct RecvError;
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum TrySendError<T> {
Full(T),
Disconnected(T),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum TryRecvError {
Empty,
Disconnected,
}
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum SendTimeoutError<T> {
Timeout(T),
Disconnected(T),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum RecvTimeoutError {
Timeout,
Disconnected,
}
impl<T> SendError<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> TrySendError<T> {
pub fn into_inner(self) -> T {
match self {
Self::Full(m) | Self::Disconnected(m) => m,
}
}
pub fn is_full(&self) -> bool {
matches!(self, Self::Full(_))
}
pub fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected(_))
}
}
impl<T> SendTimeoutError<T> {
pub fn into_inner(self) -> T {
match self {
Self::Timeout(m) | Self::Disconnected(m) => m,
}
}
pub fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout(_))
}
pub fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected(_))
}
}
impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("SendError(..)")
}
}
impl<T> fmt::Debug for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Full(_) => f.write_str("Full(..)"),
Self::Disconnected(_) => f.write_str("Disconnected(..)"),
}
}
}
impl<T> fmt::Debug for SendTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout(_) => f.write_str("Timeout(..)"),
Self::Disconnected(_) => f.write_str("Disconnected(..)"),
}
}
}
impl<T> fmt::Display for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("sending on a disconnected channel")
}
}
impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("receiving on an empty and disconnected channel")
}
}
impl<T> fmt::Display for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Full(_) => f.write_str("sending on a full channel"),
Self::Disconnected(_) => f.write_str("sending on a disconnected channel"),
}
}
}
impl fmt::Display for TryRecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("receiving on an empty channel"),
Self::Disconnected => f.write_str("receiving on an empty and disconnected channel"),
}
}
}
impl<T> fmt::Display for SendTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout(_) => f.write_str("timed out sending on a full channel"),
Self::Disconnected(_) => f.write_str("sending on a disconnected channel"),
}
}
}
impl fmt::Display for RecvTimeoutError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout => f.write_str("timed out receiving on an empty channel"),
Self::Disconnected => f.write_str("receiving on an empty and disconnected channel"),
}
}
}
impl<T> Error for SendError<T> {}
impl Error for RecvError {}
impl<T> Error for TrySendError<T> {}
impl Error for TryRecvError {}
impl<T> Error for SendTimeoutError<T> {}
impl Error for RecvTimeoutError {}