musli_core/hint/
map_hint.rs1use crate::Context;
2use crate::de::SizeHint;
3
4mod sealed {
5 pub trait Sealed {}
6 impl<T, M> Sealed for crate::__priv::EncodeMapHint<'_, T, M> where T: ?Sized {}
7 impl Sealed for usize {}
8 impl Sealed for Option<usize> {}
9}
10
11pub trait MapHint: Sized + self::sealed::Sealed {
38 fn get(self) -> Option<usize>;
40
41 #[inline]
44 fn require<C>(self, cx: C) -> Result<usize, C::Error>
45 where
46 C: Context,
47 {
48 let Some(size) = self.get() else {
49 return Err(cx.message("Format cannot handle maps with an unknown number of entries"));
50 };
51
52 Ok(size)
53 }
54
55 #[inline]
57 fn size_hint(self) -> SizeHint {
58 match self.get() {
59 Some(size) => SizeHint::exact(size),
60 None => SizeHint::any(),
61 }
62 }
63}
64
65impl MapHint for usize {
66 #[inline]
67 fn get(self) -> Option<usize> {
68 Some(self)
69 }
70
71 #[inline]
72 fn require<C>(self, _: C) -> Result<usize, C::Error>
73 where
74 C: Context,
75 {
76 Ok(self)
77 }
78
79 #[inline]
80 fn size_hint(self) -> SizeHint {
81 SizeHint::exact(self)
82 }
83}
84
85impl MapHint for Option<usize> {
86 #[inline]
87 fn get(self) -> Option<usize> {
88 self
89 }
90}