tokio_buf/size_hint.rs
1use std::u64;
2
3/// A `BufStream` size hint
4///
5/// The default implementation returns:
6///
7/// * 0 for `available`
8/// * 0 for `lower`
9/// * `None` for `upper`.
10#[derive(Debug, Default, Clone)]
11pub struct SizeHint {
12 lower: u64,
13 upper: Option<u64>,
14}
15
16impl SizeHint {
17 /// Returns a new `SizeHint` with default values
18 pub fn new() -> SizeHint {
19 SizeHint::default()
20 }
21
22 /// Returns the lower bound of data that the `BufStream` will yield before
23 /// completing.
24 pub fn lower(&self) -> u64 {
25 self.lower
26 }
27
28 /// Set the value of the `lower` hint.
29 ///
30 /// # Panics
31 ///
32 /// The function panics if `value` is greater than `upper`.
33 pub fn set_lower(&mut self, value: u64) {
34 assert!(value <= self.upper.unwrap_or(u64::MAX));
35 self.lower = value;
36 }
37
38 /// Returns the upper bound of data the `BufStream` will yield before
39 /// completing, or `None` if the value is unknown.
40 pub fn upper(&self) -> Option<u64> {
41 self.upper
42 }
43
44 /// Set the value of the `upper` hint value.
45 ///
46 /// # Panics
47 ///
48 /// This function panics if `value` is less than `lower`.
49 pub fn set_upper(&mut self, value: u64) {
50 // There is no need to check `available` as that is guaranteed to be
51 // less than or equal to `lower`.
52 assert!(value >= self.lower, "`value` is less than than `lower`");
53
54 self.upper = Some(value);
55 }
56}