musli_core/de/
size_hint.rs
1use core::fmt;
2
3#[derive(Default, Debug, Clone, Copy)]
4enum SizeHintKind {
5 #[default]
7 Any,
8 Exact(usize),
10}
11
12#[derive(Default, Debug, Clone, Copy)]
14#[non_exhaustive]
15pub struct SizeHint {
16 kind: SizeHintKind,
17}
18
19impl SizeHint {
20 #[inline]
31 pub const fn any() -> Self {
32 SizeHint {
33 kind: SizeHintKind::Any,
34 }
35 }
36
37 #[inline]
48 pub const fn exact(length: usize) -> Self {
49 SizeHint {
50 kind: SizeHintKind::Exact(length),
51 }
52 }
53
54 pub fn or_default(self) -> usize {
65 match self.kind {
66 SizeHintKind::Any => 0,
67 SizeHintKind::Exact(n) => n,
68 }
69 }
70}
71
72impl From<Option<usize>> for SizeHint {
73 fn from(value: Option<usize>) -> Self {
74 let kind = match value {
75 Some(n) => SizeHintKind::Exact(n),
76 None => SizeHintKind::Any,
77 };
78
79 SizeHint { kind }
80 }
81}
82
83impl fmt::Display for SizeHint {
84 #[inline]
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 match self.kind {
87 SizeHintKind::Any => write!(f, "unknown length"),
88 SizeHintKind::Exact(length) => write!(f, "{length} items"),
89 }
90 }
91}
92
93impl SizeHint {
94 pub fn into_option(self) -> Option<usize> {
96 match self.kind {
97 SizeHintKind::Any => None,
98 SizeHintKind::Exact(len) => Some(len),
99 }
100 }
101}