musli_core/de/
size_hint.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use core::fmt;

#[derive(Default, Debug, Clone, Copy)]
enum SizeHintKind {
    /// The length isn't known.
    #[default]
    Any,
    /// The length is exactly known.
    Exact(usize),
}

/// A length hint.
#[derive(Default, Debug, Clone, Copy)]
#[non_exhaustive]
pub struct SizeHint {
    kind: SizeHintKind,
}

impl SizeHint {
    /// Construct a size hint of unknown size.
    ///
    /// # Examples
    ///
    /// ```
    /// use musli::de::SizeHint;
    ///
    /// let hint = SizeHint::any();
    /// assert_eq!(hint.or_default(), 0);
    /// ```
    #[inline]
    pub const fn any() -> Self {
        SizeHint {
            kind: SizeHintKind::Any,
        }
    }

    /// Construct an exactly sized hint.
    ///
    /// # Examples
    ///
    /// ```
    /// use musli::de::SizeHint;
    ///
    /// let hint = SizeHint::exact(16);
    /// assert_eq!(hint.or_default(), 16);
    /// ```
    #[inline]
    pub const fn exact(length: usize) -> Self {
        SizeHint {
            kind: SizeHintKind::Exact(length),
        }
    }

    /// Get a size hint or a default value.
    ///
    /// # Examples
    ///
    /// ```
    /// use musli::de::SizeHint;
    ///
    /// let hint = SizeHint::any();
    /// assert_eq!(hint.or_default(), 0);
    /// ```
    pub fn or_default(self) -> usize {
        match self.kind {
            SizeHintKind::Any => 0,
            SizeHintKind::Exact(n) => n,
        }
    }
}

impl From<Option<usize>> for SizeHint {
    fn from(value: Option<usize>) -> Self {
        let kind = match value {
            Some(n) => SizeHintKind::Exact(n),
            None => SizeHintKind::Any,
        };

        SizeHint { kind }
    }
}

impl fmt::Display for SizeHint {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind {
            SizeHintKind::Any => write!(f, "unknown length"),
            SizeHintKind::Exact(length) => write!(f, "{length} items"),
        }
    }
}

impl SizeHint {
    /// Coerce into an `Option`.
    pub fn into_option(self) -> Option<usize> {
        match self.kind {
            SizeHintKind::Any => None,
            SizeHintKind::Exact(len) => Some(len),
        }
    }
}