Skip to main content

mlt_core/frames/v01/id/
decode.rs

1use crate::enc_dec::Decode;
2use crate::utils::apply_present;
3use crate::v01::{Id, IdValues, RawId, RawIdValue, RawPresence};
4use crate::{Decoder, MltResult};
5
6impl Decode<IdValues> for RawId<'_> {
7    fn decode(self, decoder: &mut Decoder) -> MltResult<IdValues> {
8        RawId::decode(self, decoder)
9    }
10}
11
12impl RawId<'_> {
13    /// Decode into [`IdValues`], charging `dec` before each `Vec` allocation.
14    pub fn decode(self, dec: &mut Decoder) -> MltResult<IdValues> {
15        let RawId { presence, value } = self;
16
17        // Decode the raw integer stream, charging for it before allocation.
18        let ids_u64: Vec<u64> = match value {
19            RawIdValue::Id32(stream) => {
20                // FIXME: IdValues should be an enum of i32 or i64 values to avoid extra allocations
21                let ids = stream.decode_u32s(dec)?;
22                dec.consume_items::<u64>(ids.len())?;
23                ids.into_iter().map(u64::from).collect()
24            }
25            RawIdValue::Id64(stream) => stream.decode_u64s(dec)?,
26        };
27
28        Ok(IdValues(apply_present(presence, ids_u64, dec)?))
29    }
30}
31
32impl<'a> Id<'a> {
33    #[must_use]
34    pub fn new_raw(presence: RawPresence<'a>, value: RawIdValue<'a>) -> Self {
35        Self::Raw(RawId { presence, value })
36    }
37}
38
39impl IdValues {
40    #[must_use]
41    pub fn values(&self) -> &[Option<u64>] {
42        &self.0
43    }
44}