use std::{error::Error, fmt::{Debug, Display, Formatter}};
pub type SendResult<T> = Result<(), T>;
pub enum BoundedSendError<T>
{
Full(T),
Closed(T),
}
impl<T> BoundedSendError<T>
{
pub fn is_full(&self) -> bool
{
if let BoundedSendError::Full(_) = self
{
true
}
else
{
false
}
}
pub fn has_no_receivers(&self) -> bool
{
if let BoundedSendError::Closed(_) = self
{
true
}
else
{
false
}
}
pub fn take(self) -> T
{
match self
{
BoundedSendError::Full(val) => val,
BoundedSendError::Closed(val) => val
}
}
}
impl<T> Display for BoundedSendError<T>
where T: Display
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
match self
{
BoundedSendError::Full(val) => write!(f, "Full({val})"),
BoundedSendError::Closed(val) => write!(f, "Closed({val})")
}
}
}
impl<T> Debug for BoundedSendError<T>
where T: Debug
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Full(arg0) => f.debug_tuple("Full").field(arg0).finish(),
Self::Closed(arg0) => f.debug_tuple("Closed").field(arg0).finish(),
}
}
}
impl<T> PartialEq for BoundedSendError<T>
where T: PartialEq
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Full(l0), Self::Full(r0)) => l0 == r0,
(Self::Closed(l0), Self::Closed(r0)) => l0 == r0,
_ => false,
}
}
}
impl<T> Eq for BoundedSendError<T>
where T: Eq
{
}
pub type BoundedSendResult<T> = Result<(), BoundedSendError<T>>;
#[derive(Debug, PartialEq, Eq)]
pub enum ReceiveError
{
Empty,
Closed
}
impl Display for ReceiveError
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
match self
{
ReceiveError::Empty => write!(f, "Empty"),
ReceiveError::Closed => write!(f, "Closed")
}
}
}
pub type ReceiveResult<T> = Result<T, ReceiveError>;
#[derive(Debug)]
pub enum TimeoutBoundedSendError<T>
{
NotTimedOut(BoundedSendError<T>),
TimedOut(T)
}
impl<T> Display for TimeoutBoundedSendError<T>
where T: Display
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
match self
{
TimeoutBoundedSendError::NotTimedOut(val) => write!(f, "NotTimedOut({val})"),
TimeoutBoundedSendError::TimedOut(val) => write!(f, "TimedOut({val})")
}
}
}
#[derive(Debug)]
pub enum TimeoutSendError<T>
{
NotTimedOut(T),
TimedOut(T)
}
impl<T> Display for TimeoutSendError<T>
where T: Display
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
match self
{
TimeoutSendError::NotTimedOut(val) => write!(f, "NotTimedOut({val})"),
TimeoutSendError::TimedOut(val) => write!(f, "TimedOut({val})")
}
}
}
#[derive(Debug)]
pub enum TimeoutReceiveError
{
NotTimedOut(ReceiveError),
TimedOut
}
impl Display for TimeoutReceiveError
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result
{
match self
{
TimeoutReceiveError::NotTimedOut(val) => write!(f, "NotTimedOut({val})"),
TimeoutReceiveError::TimedOut=> write!(f, "TimedOut")
}
}
}