use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChannelError {
ZeroCapacity,
CapacityTooLarge {
requested: usize,
max: usize,
},
}
impl ChannelError {
#[inline]
#[must_use]
pub const fn is_zero_capacity(&self) -> bool {
matches!(self, Self::ZeroCapacity)
}
#[inline]
#[must_use]
pub const fn is_capacity_too_large(&self) -> bool {
matches!(self, Self::CapacityTooLarge { .. })
}
}
impl fmt::Display for ChannelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
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,
}
impl TryRegisterError {
#[inline]
#[must_use]
pub const fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected)
}
}
impl fmt::Display for TryRegisterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Disconnected => f.write_str("all receivers have been dropped"),
}
}
}
impl std::error::Error for TryRegisterError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SendError<T>(pub T);
impl<T> SendError<T> {
#[inline]
pub fn into_inner(self) -> T {
self.0
}
#[inline]
#[must_use]
pub const fn is_disconnected(&self) -> bool {
true
}
}
impl<T> fmt::Display for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("all receivers have been dropped")
}
}
impl<T: fmt::Debug> std::error::Error for SendError<T> {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrySendError<T> {
Full(T),
Disconnected(T),
}
impl<T> TrySendError<T> {
#[inline]
pub fn into_inner(self) -> T {
match self {
Self::Full(value) | Self::Disconnected(value) => value,
}
}
#[inline]
#[must_use]
pub const fn is_full(&self) -> bool {
matches!(self, Self::Full(_))
}
#[inline]
#[must_use]
pub const fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected(_))
}
}
impl<T> fmt::Display for TrySendError<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("all receivers have been dropped"),
}
}
}
impl<T: fmt::Debug> std::error::Error for TrySendError<T> {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SendTimeoutError<T> {
Timeout(T),
Disconnected(T),
}
impl<T> SendTimeoutError<T> {
#[inline]
pub fn into_inner(self) -> T {
match self {
Self::Timeout(value) | Self::Disconnected(value) => value,
}
}
#[inline]
#[must_use]
pub const fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout(_))
}
#[inline]
#[must_use]
pub const fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected(_))
}
}
impl<T> fmt::Display for SendTimeoutError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout(_) => f.write_str("timed out waiting for sender capacity"),
Self::Disconnected(_) => f.write_str("all receivers have been dropped"),
}
}
}
impl<T: fmt::Debug> std::error::Error for SendTimeoutError<T> {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RecvError;
impl RecvError {
#[inline]
#[must_use]
pub const fn is_disconnected(&self) -> bool {
true
}
}
impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("all senders have been dropped")
}
}
impl std::error::Error for RecvError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TryRecvError {
Empty,
Disconnected,
}
impl TryRecvError {
#[inline]
#[must_use]
pub const fn is_empty(&self) -> bool {
matches!(self, Self::Empty)
}
#[inline]
#[must_use]
pub const fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected)
}
}
impl fmt::Display for TryRecvError {
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 TryRecvError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecvTimeoutError {
Timeout,
Disconnected,
}
impl RecvTimeoutError {
#[inline]
#[must_use]
pub const fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout)
}
#[inline]
#[must_use]
pub const fn is_disconnected(&self) -> bool {
matches!(self, Self::Disconnected)
}
}
impl fmt::Display for RecvTimeoutError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout => f.write_str("timed out waiting for a message"),
Self::Disconnected => f.write_str("all senders have been dropped"),
}
}
}
impl std::error::Error for RecvTimeoutError {}