1 use std::u64; 2 3 /// A `Body` size hint 4 /// 5 /// The default implementation returns: 6 /// 7 /// * 0 for `lower` 8 /// * `None` for `upper`. 9 #[derive(Debug, Default, Clone)] 10 pub struct SizeHint { 11 lower: u64, 12 upper: Option<u64>, 13 } 14 15 impl SizeHint { 16 /// Returns a new `SizeHint` with default values 17 #[inline] new() -> SizeHint18 pub fn new() -> SizeHint { 19 SizeHint::default() 20 } 21 22 /// Returns a new `SizeHint` with both upper and lower bounds set to the 23 /// given value. 24 #[inline] with_exact(value: u64) -> SizeHint25 pub fn with_exact(value: u64) -> SizeHint { 26 SizeHint { 27 lower: value, 28 upper: Some(value), 29 } 30 } 31 32 /// Returns the lower bound of data that the `Body` will yield before 33 /// completing. 34 #[inline] lower(&self) -> u6435 pub fn lower(&self) -> u64 { 36 self.lower 37 } 38 39 /// Set the value of the `lower` hint. 40 /// 41 /// # Panics 42 /// 43 /// The function panics if `value` is greater than `upper`. 44 #[inline] set_lower(&mut self, value: u64)45 pub fn set_lower(&mut self, value: u64) { 46 assert!(value <= self.upper.unwrap_or(u64::MAX)); 47 self.lower = value; 48 } 49 50 /// Returns the upper bound of data the `Body` will yield before 51 /// completing, or `None` if the value is unknown. 52 #[inline] upper(&self) -> Option<u64>53 pub fn upper(&self) -> Option<u64> { 54 self.upper 55 } 56 57 /// Set the value of the `upper` hint value. 58 /// 59 /// # Panics 60 /// 61 /// This function panics if `value` is less than `lower`. 62 #[inline] set_upper(&mut self, value: u64)63 pub fn set_upper(&mut self, value: u64) { 64 assert!(value >= self.lower, "`value` is less than than `lower`"); 65 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] exact(&self) -> Option<u64>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] set_exact(&mut self, value: u64)82 pub fn set_exact(&mut self, value: u64) { 83 self.lower = value; 84 self.upper = Some(value); 85 } 86 } 87