use core::fmt;
#[cfg(feature = "std")]
#[non_exhaustive]
#[derive(PartialEq, Eq)]
pub enum SendTimeoutError<T = ()> {
Timeout(T),
Closed(T),
}
#[non_exhaustive]
#[derive(PartialEq, Eq)]
pub enum TrySendError<T = ()> {
Full(T),
Closed(T),
}
#[cfg(feature = "std")]
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq)]
pub enum RecvTimeoutError {
Timeout,
Closed,
}
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq)]
pub enum TryRecvError {
Empty,
Closed,
}
#[derive(PartialEq, Eq)]
pub struct Closed<T = ()>(pub(crate) T);
impl<T> Closed<T> {
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> fmt::Debug for Closed<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Closed(..)")
}
}
impl<T> fmt::Display for Closed<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("channel closed")
}
}
#[cfg(feature = "std")]
impl<T> std::error::Error for Closed<T> {}
#[cfg(feature = "std")]
impl SendTimeoutError {
pub(crate) fn with_value<T>(self, value: T) -> SendTimeoutError<T> {
match self {
Self::Timeout(()) => SendTimeoutError::Timeout(value),
Self::Closed(()) => SendTimeoutError::Closed(value),
}
}
}
#[cfg(feature = "std")]
impl<T> SendTimeoutError<T> {
pub fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout(_))
}
pub fn is_closed(&self) -> bool {
matches!(self, Self::Timeout(_))
}
pub fn into_inner(self) -> T {
match self {
Self::Timeout(val) => val,
Self::Closed(val) => val,
}
}
}
#[cfg(feature = "std")]
impl<T> fmt::Debug for SendTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Timeout(_) => "SendTimeoutError::Timeout(..)",
Self::Closed(_) => "SendTimeoutError::Closed(..)",
})
}
}
#[cfg(feature = "std")]
impl<T> fmt::Display for SendTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Timeout(_) => "timed out waiting for channel capacity",
Self::Closed(_) => "channel closed",
})
}
}
#[cfg(feature = "std")]
impl<T> std::error::Error for SendTimeoutError<T> {}
impl TrySendError {
pub(crate) fn with_value<T>(self, value: T) -> TrySendError<T> {
match self {
Self::Full(()) => TrySendError::Full(value),
Self::Closed(()) => TrySendError::Closed(value),
}
}
}
impl<T> TrySendError<T> {
pub fn is_full(&self) -> bool {
matches!(self, Self::Full(_))
}
pub fn is_closed(&self) -> bool {
matches!(self, Self::Full(_))
}
pub fn into_inner(self) -> T {
match self {
Self::Full(val) => val,
Self::Closed(val) => val,
}
}
}
impl<T> fmt::Debug for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Full(_) => "TrySendError::Full(..)",
Self::Closed(_) => "TrySendError::Closed(..)",
})
}
}
impl<T> fmt::Display for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Full(_) => "no available capacity",
Self::Closed(_) => "channel closed",
})
}
}
#[cfg(feature = "std")]
impl<T> std::error::Error for TrySendError<T> {}
#[cfg(feature = "std")]
impl std::error::Error for RecvTimeoutError {}
#[cfg(feature = "std")]
impl fmt::Display for RecvTimeoutError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Timeout => "timed out waiting on channel",
Self::Closed => "channel closed",
})
}
}
#[cfg(feature = "std")]
impl std::error::Error for TryRecvError {}
impl fmt::Display for TryRecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Empty => "channel is empty",
Self::Closed => "channel closed",
})
}
}