Skip to main content

miden_protobuf/decode/
build.rs

1use alloc::collections::BTreeMap;
2use alloc::vec::Vec;
3use core::fmt::Debug;
4
5use super::{MapField, OptionalField, RepeatedField};
6use crate::{BuildUnchecked, ConversionError};
7
8/// Builds the present value; the caller must ensure the invariants documented by `S`.
9impl<S: BuildUnchecked> BuildUnchecked for OptionalField<S> {
10    type Output = Option<S::Output>;
11    type Error = ConversionError;
12
13    fn build_unchecked(self) -> Result<Self::Output, Self::Error> {
14        self.try_map(BuildUnchecked::build_unchecked)
15    }
16}
17
18/// Builds each value in input order, preserving duplicates. The caller must ensure the
19/// invariants documented by `S` for every value, as well as any collection-wide invariants.
20impl<S: BuildUnchecked> BuildUnchecked for RepeatedField<S> {
21    type Output = Vec<S::Output>;
22    type Error = ConversionError;
23
24    fn build_unchecked(self) -> Result<Self::Output, Self::Error> {
25        self.try_map(BuildUnchecked::build_unchecked)
26    }
27}
28
29/// Builds each value in key order, preserving keys. The caller must ensure the invariants
30/// documented by `S` for every value, as well as any collection-wide invariants.
31impl<K: Ord + Debug, S: BuildUnchecked> BuildUnchecked for MapField<BTreeMap<K, S>> {
32    type Output = BTreeMap<K, S::Output>;
33    type Error = ConversionError;
34
35    fn build_unchecked(self) -> Result<Self::Output, Self::Error> {
36        self.try_map(BuildUnchecked::build_unchecked)
37    }
38}
39
40/// Builds each value in unspecified order, preserving keys. The caller must ensure the
41/// invariants documented by `S` for every value, as well as any collection-wide invariants.
42#[cfg(feature = "std")]
43impl<K: Eq + core::hash::Hash + Debug, S: BuildUnchecked> BuildUnchecked
44    for MapField<std::collections::HashMap<K, S>>
45{
46    type Output = std::collections::HashMap<K, S::Output>;
47    type Error = ConversionError;
48
49    fn build_unchecked(self) -> Result<Self::Output, Self::Error> {
50        self.try_map(BuildUnchecked::build_unchecked)
51    }
52}