1#[derive(Debug, Default, Clone, Copy)]
8pub struct SizeHint {
9 lower: u64,
10 upper: Option<u64>,
11}
12
13impl SizeHint {
14 #[inline]
16 pub fn new() -> SizeHint {
17 SizeHint::default()
18 }
19
20 #[inline]
23 pub fn with_exact(value: u64) -> SizeHint {
24 SizeHint {
25 lower: value,
26 upper: Some(value),
27 }
28 }
29
30 #[inline]
33 pub fn lower(&self) -> u64 {
34 self.lower
35 }
36
37 #[inline]
43 pub fn set_lower(&mut self, value: u64) {
44 assert!(value <= self.upper.unwrap_or(u64::MAX));
45 self.lower = value;
46 }
47
48 #[inline]
51 pub fn upper(&self) -> Option<u64> {
52 self.upper
53 }
54
55 #[inline]
61 pub fn set_upper(&mut self, value: u64) {
62 assert!(value >= self.lower, "`value` is less than than `lower`");
63
64 self.upper = Some(value);
65 }
66
67 #[inline]
70 pub fn exact(&self) -> Option<u64> {
71 if Some(self.lower) == self.upper {
72 self.upper
73 } else {
74 None
75 }
76 }
77
78 #[inline]
80 pub fn set_exact(&mut self, value: u64) {
81 self.lower = value;
82 self.upper = Some(value);
83 }
84}
85
86impl core::ops::Add for SizeHint {
88 type Output = SizeHint;
89
90 fn add(self, rhs: Self) -> Self::Output {
91 SizeHint {
92 lower: self.lower() + rhs.lower(),
93 upper: self
94 .upper()
95 .and_then(|this| rhs.upper().map(|rhs| this + rhs)),
96 }
97 }
98}
99
100#[test]
102fn size_hint_addition_proof() {
103 fn to_parts(s: SizeHint) -> (u64, Option<u64>) {
105 (s.lower(), s.upper())
106 }
107
108 match (to_parts(SizeHint::new()), to_parts(SizeHint::new())) {
115 ((_, Some(_)), (_, Some(_))) => {} ((_, None), (_, None)) => {} ((_, Some(_)), (_, None)) => {} ((_, None), (_, Some(_))) => {}
122 }
123 macro_rules! reciprocal_add_eq {
130 ($a:expr, $b:expr, $eq:expr) => {
131 assert_eq!(to_parts($a.clone() + $b.clone()), $eq);
132 assert_eq!(to_parts($b.clone() + $a.clone()), $eq);
133 };
134 }
135
136 let exact_1 = SizeHint::with_exact(1);
140 let exact_2 = SizeHint::with_exact(2);
141
142 reciprocal_add_eq!(exact_1, exact_2, to_parts(SizeHint::with_exact(1 + 2)));
144
145 let some_lhs = SizeHint {
146 lower: 4,
147 upper: Some(8),
148 };
149
150 let some_rhs = SizeHint {
151 lower: 16,
152 upper: Some(32),
153 };
154
155 reciprocal_add_eq!(some_lhs, some_rhs, (4 + 16, Some(8 + 32)));
157
158 let none_lhs = SizeHint {
159 lower: 64,
160 upper: None,
161 };
162
163 let none_rhs = SizeHint {
164 lower: 128,
165 upper: None,
166 };
167
168 reciprocal_add_eq!(none_lhs, none_rhs, (64 + 128, None));
170
171 reciprocal_add_eq!(some_lhs, none_rhs, (4 + 128, None));
173}
174
175#[test]
177fn size_hint_addition_basic() {
178 let exact_l = SizeHint::with_exact(20);
179 let exact_r = SizeHint::with_exact(5);
180
181 assert_eq!(Some(25), (exact_l + exact_r).exact());
182
183 let inexact_l = SizeHint {
184 lower: 25,
185 upper: None,
186 };
187 let inexact_r = SizeHint {
188 lower: 10,
189 upper: Some(50),
190 };
191
192 let inexact = inexact_l + inexact_r;
193
194 assert_eq!(inexact.lower(), 35);
195 assert_eq!(inexact.upper(), None);
196
197 let exact_inexact = exact_l + inexact_r;
198
199 assert_eq!(exact_inexact.lower(), 30);
200 assert_eq!(exact_inexact.upper(), Some(70));
201
202 let inexact_exact = inexact_r + exact_l;
204
205 assert_eq!(inexact_exact.lower(), 30);
206 assert_eq!(inexact_exact.upper(), Some(70));
207}