Skip to main content

fidl_next_codec/decode/
mod.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Provides decoding for FIDL types.
6
7mod error;
8
9pub use self::error::*;
10
11use crate::{Constrained, Slot};
12
13/// Decodes a value from the given slot.
14///
15/// # Safety
16///
17/// If `decode` returns `Ok`, then the provided `slot` will contain a valid decoded value after the
18/// decoder is committed.
19pub unsafe trait Decode<D: ?Sized>: Constrained {
20    /// Decodes a value into a slot using a decoder.
21    ///
22    /// If decoding succeeds, `slot` will contain a valid decoded value after the decoder is
23    /// committed. If decoding fails, an error will be returned.
24    fn decode(
25        slot: Slot<'_, Self>,
26        decoder: &mut D,
27        constraint: Self::Constraint,
28    ) -> Result<(), DecodeError>;
29}
30
31unsafe impl<D: ?Sized, T: Decode<D>, const N: usize> Decode<D> for [T; N] {
32    fn decode(
33        mut slot: Slot<'_, Self>,
34        decoder: &mut D,
35        constraint: T::Constraint,
36    ) -> Result<(), DecodeError> {
37        for i in 0..N {
38            T::decode(slot.index(i), decoder, constraint)?;
39        }
40        Ok(())
41    }
42}