1use core::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28#[non_exhaustive]
29pub enum StopReason {
30 Cancelled,
35
36 TimedOut,
41}
42
43impl StopReason {
44 #[inline]
51 pub fn is_transient(&self) -> bool {
52 matches!(self, Self::TimedOut)
53 }
54
55 #[inline]
57 pub fn is_cancelled(&self) -> bool {
58 matches!(self, Self::Cancelled)
59 }
60
61 #[inline]
63 pub fn is_timed_out(&self) -> bool {
64 matches!(self, Self::TimedOut)
65 }
66}
67
68impl fmt::Display for StopReason {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 match self {
71 Self::Cancelled => write!(f, "operation cancelled"),
72 Self::TimedOut => write!(f, "operation timed out"),
73 }
74 }
75}
76
77#[cfg(feature = "std")]
78impl std::error::Error for StopReason {}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn stop_reason_display() {
86 extern crate alloc;
87 use alloc::format;
88 assert_eq!(format!("{}", StopReason::Cancelled), "operation cancelled");
89 assert_eq!(format!("{}", StopReason::TimedOut), "operation timed out");
90 }
91
92 #[test]
93 fn stop_reason_equality() {
94 assert_eq!(StopReason::Cancelled, StopReason::Cancelled);
95 assert_eq!(StopReason::TimedOut, StopReason::TimedOut);
96 assert_ne!(StopReason::Cancelled, StopReason::TimedOut);
97 }
98
99 #[test]
100 fn stop_reason_is_transient() {
101 assert!(!StopReason::Cancelled.is_transient());
102 assert!(StopReason::TimedOut.is_transient());
103 }
104
105 #[test]
106 fn stop_reason_copy() {
107 let a = StopReason::Cancelled;
108 let b = a; assert_eq!(a, b);
110 }
111
112 #[test]
113 fn stop_reason_hash() {
114 use core::hash::{Hash, Hasher};
115
116 struct DummyHasher(u64);
117 impl Hasher for DummyHasher {
118 fn finish(&self) -> u64 {
119 self.0
120 }
121 fn write(&mut self, bytes: &[u8]) {
122 for &b in bytes {
123 self.0 = self.0.wrapping_add(b as u64);
124 }
125 }
126 }
127
128 let mut h1 = DummyHasher(0);
129 let mut h2 = DummyHasher(0);
130 StopReason::Cancelled.hash(&mut h1);
131 StopReason::Cancelled.hash(&mut h2);
132 assert_eq!(h1.finish(), h2.finish());
133 }
134}