use std::error::Error;
use std::fmt;
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct PushError<T>(pub T);
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct PopError;
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum TryPushError<T> {
Full(T),
Closed(T),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum TryPopError {
Empty,
Closed,
}
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum PushTimeoutError<T> {
Timeout(T),
Closed(T),
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PopTimeoutError {
Timeout,
Closed,
}
impl<T> PushError<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> TryPushError<T> {
pub fn into_inner(self) -> T {
match self {
Self::Full(v) | Self::Closed(v) => v,
}
}
pub fn is_full(&self) -> bool {
matches!(self, Self::Full(_))
}
pub fn is_closed(&self) -> bool {
matches!(self, Self::Closed(_))
}
}
impl<T> PushTimeoutError<T> {
pub fn into_inner(self) -> T {
match self {
Self::Timeout(v) | Self::Closed(v) => v,
}
}
pub fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout(_))
}
pub fn is_closed(&self) -> bool {
matches!(self, Self::Closed(_))
}
}
impl<T> fmt::Debug for PushError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("PushError(..)")
}
}
impl<T> fmt::Debug for TryPushError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Full(_) => f.write_str("Full(..)"),
Self::Closed(_) => f.write_str("Closed(..)"),
}
}
}
impl<T> fmt::Debug for PushTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout(_) => f.write_str("Timeout(..)"),
Self::Closed(_) => f.write_str("Closed(..)"),
}
}
}
impl<T> fmt::Display for PushError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("pushing into a closed queue")
}
}
impl fmt::Display for PopError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("popping from a closed and empty queue")
}
}
impl<T> fmt::Display for TryPushError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Full(_) => f.write_str("pushing into a full queue"),
Self::Closed(_) => f.write_str("pushing into a closed queue"),
}
}
}
impl fmt::Display for TryPopError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("popping from an empty queue"),
Self::Closed => f.write_str("popping from a closed and empty queue"),
}
}
}
impl<T> fmt::Display for PushTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout(_) => f.write_str("timed out pushing into a full queue"),
Self::Closed(_) => f.write_str("pushing into a closed queue"),
}
}
}
impl fmt::Display for PopTimeoutError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout => f.write_str("timed out popping from an empty queue"),
Self::Closed => f.write_str("popping from a closed and empty queue"),
}
}
}
impl<T> Error for PushError<T> {}
impl Error for PopError {}
impl<T> Error for TryPushError<T> {}
impl Error for TryPopError {}
impl<T> Error for PushTimeoutError<T> {}
impl Error for PopTimeoutError {}