fidl_next_codec/
into_natural.rs

1// Copyright 2025 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
5use crate::{FromWire, WireF32, WireF64, WireI16, WireI32, WireI64, WireU16, WireU32, WireU64};
6
7/// Associates a good default type for a wire type to convert into.
8pub trait IntoNatural: Sized {
9    /// A good default type for this wire type to convert into.
10    type Natural: FromWire<Self>;
11
12    /// Converts this type into its natural equivalent.
13    fn into_natural(self) -> Self::Natural {
14        Self::Natural::from_wire(self)
15    }
16}
17
18macro_rules! impl_primitive {
19    ($ty:ty) => {
20        impl_primitive!($ty, $ty);
21    };
22    ($wire:ty, $natural:ty) => {
23        impl IntoNatural for $wire {
24            type Natural = $natural;
25        }
26    };
27}
28
29macro_rules! impl_primitives {
30    ($($wire:ty $(, $natural:ty)?);* $(;)?) => {
31        $(
32            impl_primitive!($wire $(, $natural)?);
33        )*
34    }
35}
36
37impl_primitives! {
38    ();
39
40    bool;
41
42    i8;
43    WireI16, i16;
44    WireI32, i32;
45    WireI64, i64;
46
47    u8;
48    WireU16, u16;
49    WireU32, u32;
50    WireU64, u64;
51
52    WireF32, f32;
53    WireF64, f64;
54}
55
56impl<W: IntoNatural, const N: usize> IntoNatural for [W; N] {
57    type Natural = [W::Natural; N];
58}