use core::{fmt, num::NonZeroUsize};
use crate::{Deadline, RetainedBytes};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TimerScheduleFailure {
TimerCapacity {
limit: NonZeroUsize,
},
RetainedByteOverflow {
current: RetainedBytes,
timer: RetainedBytes,
},
RetainedByteCapacity {
limit: RetainedBytes,
current: RetainedBytes,
timer: RetainedBytes,
},
TimerIdsExhausted,
}
impl fmt::Display for TimerScheduleFailure {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TimerCapacity { limit } => {
write!(formatter, "timer capacity of {limit} was reached")
}
Self::RetainedByteOverflow { current, timer } => write!(
formatter,
"adding {} retained bytes to {} would overflow timer accounting",
timer.get(),
current.get()
),
Self::RetainedByteCapacity {
limit,
current,
timer,
} => write!(
formatter,
"adding {} retained bytes to {} would exceed timer limit {}",
timer.get(),
current.get(),
limit.get()
),
Self::TimerIdsExhausted => formatter.write_str("timer identities are exhausted"),
}
}
}
impl core::error::Error for TimerScheduleFailure {}
#[derive(Debug)]
pub struct TimerScheduleError<T> {
deadline: Deadline,
value: T,
failure: TimerScheduleFailure,
}
impl<T> TimerScheduleError<T> {
pub(super) const fn new(deadline: Deadline, value: T, failure: TimerScheduleFailure) -> Self {
Self {
deadline,
value,
failure,
}
}
pub const fn failure(&self) -> TimerScheduleFailure {
self.failure
}
pub const fn deadline(&self) -> Deadline {
self.deadline
}
pub const fn value(&self) -> &T {
&self.value
}
pub fn into_value(self) -> T {
self.value
}
pub fn into_parts(self) -> (Deadline, T, TimerScheduleFailure) {
(self.deadline, self.value, self.failure)
}
}
impl<T> fmt::Display for TimerScheduleError<T> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
self.failure.fmt(formatter)
}
}
impl<T: fmt::Debug> core::error::Error for TimerScheduleError<T> {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
Some(&self.failure)
}
}