puzz_http/body/
size_hint.rs

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)]
8pub struct SizeHint {
9    lower: u64,
10    upper: Option<u64>,
11}
12
13impl 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!(
45            value <= self.upper.unwrap_or(u64::MAX),
46            "`value` is greater than `upper`"
47        );
48        self.lower = value;
49    }
50
51    /// Returns the upper bound of data the `Body` will yield before
52    /// completing, or `None` if the value is unknown.
53    #[inline]
54    pub fn upper(&self) -> Option<u64> {
55        self.upper
56    }
57
58    /// Set the value of the `upper` hint value.
59    ///
60    /// # Panics
61    ///
62    /// This function panics if `value` is less than `lower`.
63    #[inline]
64    pub fn set_upper(&mut self, value: u64) {
65        assert!(value >= self.lower, "`value` is less than `lower`");
66        self.upper = Some(value);
67    }
68
69    /// Returns the exact size of data that will be yielded **if** the
70    /// `lower` and `upper` bounds are equal.
71    #[inline]
72    pub fn exact(&self) -> Option<u64> {
73        if Some(self.lower) == self.upper {
74            self.upper
75        } else {
76            None
77        }
78    }
79
80    /// Set the value of the `lower` and `upper` bounds to exactly the same.
81    #[inline]
82    pub fn set_exact(&mut self, value: u64) {
83        self.lower = value;
84        self.upper = Some(value);
85    }
86}