use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChannelError {
ZeroSenders,
TooManySenders {
requested: usize,
max: usize,
},
ZeroCapacity,
CapacityTooLarge {
requested: usize,
max: usize,
},
}
impl fmt::Display for ChannelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::ZeroSenders => f.write_str("max_senders must be > 0"),
Self::TooManySenders { max, .. } => {
write!(f, "max_senders must be <= {max} for the ready bitmask")
}
Self::ZeroCapacity => f.write_str("capacity_per_sender must be > 0"),
Self::CapacityTooLarge { max, .. } => {
write!(f, "capacity_per_sender must be <= {max}")
}
}
}
}
impl std::error::Error for ChannelError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TryRegisterError {
Disconnected,
NoSenderSlot,
}
impl fmt::Display for TryRegisterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Disconnected => f.write_str("receiver has been dropped"),
Self::NoSenderSlot => f.write_str("all sender slots are already claimed"),
}
}
}
impl std::error::Error for TryRegisterError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SendError<T> {
Full(T),
Disconnected(T),
}
impl<T> SendError<T> {
#[inline]
pub fn into_inner(self) -> T {
match self {
Self::Full(value) | Self::Disconnected(value) => value,
}
}
}
impl<T> fmt::Display for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Full(_) => f.write_str("sender ring is full"),
Self::Disconnected(_) => f.write_str("receiver has been dropped"),
}
}
}
impl<T: fmt::Debug> std::error::Error for SendError<T> {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecvError {
Empty,
Disconnected,
}
impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => f.write_str("channel is empty"),
Self::Disconnected => f.write_str("all senders have been dropped"),
}
}
}
impl std::error::Error for RecvError {}