1#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
11pub struct Depth(u32);
12
13impl Depth {
14 #[inline]
16 pub fn dec(&mut self) {
17 self.0 = self.0.saturating_sub(1);
18 }
19
20 #[inline]
22 #[must_use]
23 pub fn get(self) -> u32 {
24 self.0
25 }
26
27 #[inline]
29 pub fn inc(&mut self) {
30 self.0 = self.0.saturating_add(1);
31 }
32
33 #[inline]
35 #[must_use]
36 pub fn is_positive(self) -> bool {
37 self.0 > 0
38 }
39
40 #[inline]
42 #[must_use]
43 pub fn is_zero(self) -> bool {
44 self.0 == 0
45 }
46
47 #[inline]
50 pub fn reset(&mut self) {
51 self.0 = 0;
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn default_is_zero() {
61 let d = Depth::default();
62 assert_eq!(d.get(), 0);
63 assert!(d.is_zero());
64 assert!(!d.is_positive());
65 }
66
67 #[test]
68 fn inc_increases_by_one() {
69 let mut d = Depth::default();
70 d.inc();
71 assert_eq!(d.get(), 1);
72 assert!(d.is_positive());
73 assert!(!d.is_zero());
74 }
75
76 #[test]
77 fn dec_decreases_by_one() {
78 let mut d = Depth::default();
79 d.inc();
80 d.inc();
81 d.dec();
82 assert_eq!(d.get(), 1);
83 }
84
85 #[test]
86 fn dec_at_zero_does_not_panic_or_underflow() {
87 let mut d = Depth::default();
88 d.dec();
89 assert_eq!(d.get(), 0);
90 d.dec();
91 d.dec();
92 assert_eq!(d.get(), 0);
93 }
94
95 #[test]
96 fn inc_at_max_does_not_panic_or_overflow() {
97 let mut d = Depth(u32::MAX);
98 d.inc();
99 assert_eq!(d.get(), u32::MAX);
100 }
101
102 #[test]
103 fn reset_returns_to_zero() {
104 let mut d = Depth::default();
105 d.inc();
106 d.inc();
107 d.inc();
108 d.reset();
109 assert_eq!(d.get(), 0);
110 assert!(d.is_zero());
111 }
112}