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