use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum StopReason {
Cancelled,
TimedOut,
}
impl StopReason {
#[inline]
pub fn is_transient(&self) -> bool {
matches!(self, Self::TimedOut)
}
#[inline]
pub fn is_cancelled(&self) -> bool {
matches!(self, Self::Cancelled)
}
#[inline]
pub fn is_timed_out(&self) -> bool {
matches!(self, Self::TimedOut)
}
}
impl fmt::Display for StopReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cancelled => write!(f, "operation cancelled"),
Self::TimedOut => write!(f, "operation timed out"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stop_reason_display() {
extern crate alloc;
use alloc::format;
assert_eq!(format!("{}", StopReason::Cancelled), "operation cancelled");
assert_eq!(format!("{}", StopReason::TimedOut), "operation timed out");
}
#[test]
fn stop_reason_equality() {
assert_eq!(StopReason::Cancelled, StopReason::Cancelled);
assert_eq!(StopReason::TimedOut, StopReason::TimedOut);
assert_ne!(StopReason::Cancelled, StopReason::TimedOut);
}
#[test]
fn stop_reason_is_transient() {
assert!(!StopReason::Cancelled.is_transient());
assert!(StopReason::TimedOut.is_transient());
}
#[test]
fn stop_reason_copy() {
let a = StopReason::Cancelled;
let b = a; assert_eq!(a, b);
}
#[test]
fn stop_reason_hash() {
use core::hash::{Hash, Hasher};
struct DummyHasher(u64);
impl Hasher for DummyHasher {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, bytes: &[u8]) {
for &b in bytes {
self.0 = self.0.wrapping_add(b as u64);
}
}
}
let mut h1 = DummyHasher(0);
let mut h2 = DummyHasher(0);
StopReason::Cancelled.hash(&mut h1);
StopReason::Cancelled.hash(&mut h2);
assert_eq!(h1.finish(), h2.finish());
}
}