1 | /// A `Body` size hint |
2 | /// |
3 | /// The default implementation returns: |
4 | /// |
5 | /// * 0 for `lower` |
6 | /// * `None` for `upper`. |
7 | #[derive (Debug, Default, Clone)] |
8 | pub struct SizeHint { |
9 | lower: u64, |
10 | upper: Option<u64>, |
11 | } |
12 | |
13 | impl SizeHint { |
14 | /// Returns a new `SizeHint` with default values |
15 | #[inline ] |
16 | pub fn new() -> SizeHint { |
17 | SizeHint::default() |
18 | } |
19 | |
20 | /// Returns a new `SizeHint` with both upper and lower bounds set to the |
21 | /// given value. |
22 | #[inline ] |
23 | pub fn with_exact(value: u64) -> SizeHint { |
24 | SizeHint { |
25 | lower: value, |
26 | upper: Some(value), |
27 | } |
28 | } |
29 | |
30 | /// Returns the lower bound of data that the `Body` will yield before |
31 | /// completing. |
32 | #[inline ] |
33 | pub fn lower(&self) -> u64 { |
34 | self.lower |
35 | } |
36 | |
37 | /// Set the value of the `lower` hint. |
38 | /// |
39 | /// # Panics |
40 | /// |
41 | /// The function panics if `value` is greater than `upper`. |
42 | #[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 | /// Returns the upper bound of data the `Body` will yield before |
49 | /// completing, or `None` if the value is unknown. |
50 | #[inline ] |
51 | pub fn upper(&self) -> Option<u64> { |
52 | self.upper |
53 | } |
54 | |
55 | /// Set the value of the `upper` hint value. |
56 | /// |
57 | /// # Panics |
58 | /// |
59 | /// This function panics if `value` is less than `lower`. |
60 | #[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 | /// Returns the exact size of data that will be yielded **if** the |
68 | /// `lower` and `upper` bounds are equal. |
69 | #[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 | /// Set the value of the `lower` and `upper` bounds to exactly the same. |
79 | #[inline ] |
80 | pub fn set_exact(&mut self, value: u64) { |
81 | self.lower = value; |
82 | self.upper = Some(value); |
83 | } |
84 | } |
85 | |