use core::fmt::{Debug, Display};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TooManyConsumers(pub(crate) usize);
impl TooManyConsumers {
#[must_use]
pub fn get_limit(&self) -> usize {
self.0
}
}
impl Display for TooManyConsumers {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"cannot create another consumer because the limit ({}) was reached.",
self.0
)
}
}
impl core::error::Error for TooManyConsumers {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrySendError<T> {
Full(T),
NoData,
Disconnected(T),
}
impl<T> Display for TrySendError<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Full(_) => write!(f, "failed to send: the channel is full"),
Self::NoData => write!(f, "failed to send: no data to send"),
Self::Disconnected(_) => write!(f, "failed to send: the channel is disconnected"),
}
}
}
impl<T: Debug> core::error::Error for TrySendError<T> {}
impl<T> TrySendError<T> {
#[must_use]
pub fn into_inner(self) -> Option<T> {
match self {
Self::Full(item) | Self::Disconnected(item) => Some(item),
Self::NoData => None,
}
}
#[must_use]
pub fn is_full(&self) -> bool {
matches!(self, Self::Full(_))
}
#[must_use]
pub fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected(_))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SendError<T>(pub Option<T>);
impl<T> SendError<T> {
#[must_use]
pub fn into_inner(self) -> Option<T> {
self.0
}
}
impl<T> From<TrySendError<T>> for SendError<T> {
fn from(value: TrySendError<T>) -> Self {
Self(value.into_inner())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TryRecvError {
NoCapacity,
Empty,
Lagging(usize),
Disconnected,
}
impl Display for TryRecvError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::NoCapacity => write!(f, "failed to receive: input buffer is empty"),
Self::Empty => write!(f, "failed to receive: the channel is empty"),
Self::Lagging(count) => write!(f, "failed to receive: lagging behind {count} messages"),
Self::Disconnected => write!(f, "failed to receive: the channel is disconnected"),
}
}
}
impl core::error::Error for TryRecvError {}
impl TryRecvError {
#[must_use]
pub fn is_empty(&self) -> bool {
matches!(self, Self::Empty)
}
#[must_use]
pub fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecvError {
Lagging(usize),
Disconnected,
}
impl Display for RecvError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Lagging(count) => write!(f, "failed to receive: lagging behind {count} messages"),
Self::Disconnected => write!(f, "failed to receive: the channel is disconnected"),
}
}
}
impl core::error::Error for RecvError {}