ridl-backend-rust 0.2.0

Compiles a RIDL IR package to Rust source, plus the generated interaction face.
Documentation
---
source: crates/ridl-backend-rust/src/tests.rs
expression: rust_for(decls)
---
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct WarningFlags(i64);
impl WarningFlags {
    pub const LOW_FUEL: WarningFlags = WarningFlags(1 << 0);
    pub const CHECK_ENGINE: WarningFlags = WarningFlags(1 << 1);
    pub const DOOR_OPEN: WarningFlags = WarningFlags(1 << 2);
    pub const SEATBELT: WarningFlags = WarningFlags(1 << 3);
    /// The union of every declared bit. `TryFrom` refuses a value
    /// that carries any other bit.
    pub const DECLARED_MASK: i64 = 15;
    pub const fn get(self) -> i64 {
        self.0
    }
}
impl ::core::convert::TryFrom<i64> for WarningFlags {
    type Error = ::ridl_rt::payload::Violation;
    fn try_from(value: i64) -> ::core::result::Result<Self, Self::Error> {
        if value & !Self::DECLARED_MASK != 0 {
            return ::core::result::Result::Err(::ridl_rt::payload::Violation {
                type_name: "WarningFlags",
                rule: ::ridl_rt::payload::Rule::Variant,
            });
        }
        ::core::result::Result::Ok(Self(value))
    }
}
impl ::core::convert::From<WarningFlags> for i64 {
    fn from(value: WarningFlags) -> Self {
        value.0
    }
}
impl Default for WarningFlags {
    fn default() -> Self {
        WarningFlags(0)
    }
}
/// An accessor over FlatBuffers bytes `WarningFlags`'s `verify` accepted.
///
/// The buffer's root is the box table ADR-0019 decision 8
/// gives this declaration: one required `value` field. A
/// buffer carrying no slot for it is `MissingRequired`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(deprecated)]
pub struct WarningFlagsFbView<'a> {
    pub(crate) buf: &'a [u8],
    pub(crate) table: usize,
}
#[allow(deprecated)]
impl<'a> WarningFlagsFbView<'a> {
    /// The verified bytes this view reads.
    pub fn bytes(&self) -> &'a [u8] {
        self.buf
    }
    /// The value the box carries. `WarningFlags` is one value, so this decodes it rather than borrowing it, which costs one read.
    pub fn value(&self) -> WarningFlags {
        __ridl_fb_decode_warning_flags(self.buf, self.table)
    }
}
/// Writes `WarningFlags` as its box table and returns its position (ADR-0019 decision 8).
#[allow(deprecated)]
pub(crate) fn __ridl_fb_encode_warning_flags(
    value: &WarningFlags,
    builder: &mut ::ridl_rt::flatbuffers::Builder<'_>,
) -> ::core::result::Result<
    ::ridl_rt::flatbuffers::Pos,
    ::ridl_rt::payload::EncodeError,
> {
    ::core::result::Result::Ok({
        let __box = [
            ::ridl_rt::flatbuffers::TableField {
                slot: 0u16,
                offset: 4u16,
                value: {
                    let __s = *value;
                    ::ridl_rt::flatbuffers::Field::U8(i64::from(__s) as u8)
                },
            },
        ];
        builder.push_table(5usize, 4usize, 1u16, &__box)?
    })
}
#[allow(deprecated)]
pub(crate) fn __ridl_fb_verify_warning_flags(
    buf: &[u8],
    table: usize,
) -> ::core::result::Result<(), ::ridl_rt::payload::VerifyError> {
    match ::ridl_rt::flatbuffers::field(buf, table, 0u16, 1usize)
        .map_err(::ridl_rt::payload::VerifyError::Structure)?
    {
        ::core::option::Option::Some(__p) => {
            let __raw = ::ridl_rt::flatbuffers::read_u8(buf, __p)
                .map_err(::ridl_rt::payload::VerifyError::Structure)?;
            <WarningFlags as ::core::convert::TryFrom<i64>>::try_from(i64::from(__raw))
                .map_err(::ridl_rt::payload::VerifyError::Contract)?;
        }
        ::core::option::Option::None => {
            return ::core::result::Result::Err(
                ::ridl_rt::payload::VerifyError::Structure(
                    ::ridl_rt::payload::Malformed::MissingRequired,
                ),
            );
        }
    }
    ::core::result::Result::Ok(())
}
#[allow(deprecated)]
pub(crate) fn __ridl_fb_decode_warning_flags(buf: &[u8], table: usize) -> WarningFlags {
    {
        let __p = ::ridl_rt::flatbuffers::field(buf, table, 0u16, 1usize)
            .unwrap_or(::core::option::Option::None)
            .unwrap_or(0usize);
        <WarningFlags as ::core::convert::TryFrom<
            i64,
        >>::try_from(i64::from(::ridl_rt::flatbuffers::read_u8(buf, __p).unwrap_or(0u8)))
            .unwrap_or(WarningFlags(0i64))
    }
}
#[allow(deprecated)]
impl ::ridl_rt::payload::Payload<::ridl_rt::encoding::FlatBuffers> for WarningFlags {
    /// The largest FlatBuffers buffer any legal `WarningFlags` encodes to: 43 bytes.
    ///
    /// `ridl_ir::projection::flatbuffers::max_size` computed it,
    /// which is the one implementation of the bound (design note
    /// D-6): each table is charged its `soffset`, its inline
    /// fields, its vtable and one alignment event per slot; a
    /// string four bytes per declared character plus a
    /// terminator; a collection its declared maximum. It is a
    /// literal rather than an expression over the field types
    /// because that slack is not expressible in Rust's type
    /// system.
    const MAX_SIZE: usize = 43usize;
    type View<'a> = WarningFlagsFbView<'a>;
    fn encode<'o>(
        &self,
        out: &'o mut [u8],
    ) -> ::core::result::Result<
        ::ridl_rt::payload::Encoded<'o, Self::View<'o>>,
        ::ridl_rt::payload::EncodeError,
    > {
        let mut builder = ::ridl_rt::flatbuffers::Builder::new(out);
        let __root = __ridl_fb_encode_warning_flags(self, &mut builder)?;
        let bytes = builder.finish(__root, 8usize)?;
        let table = ::ridl_rt::flatbuffers::root(bytes).unwrap_or(0usize);
        ::core::result::Result::Ok(::ridl_rt::payload::Encoded {
            bytes,
            view: WarningFlagsFbView {
                buf: bytes,
                table,
            },
        })
    }
    fn verify(
        buf: &[u8],
    ) -> ::core::result::Result<Self::View<'_>, ::ridl_rt::payload::VerifyError> {
        if buf.len()
            > <Self as ::ridl_rt::payload::Payload<
                ::ridl_rt::encoding::FlatBuffers,
            >>::MAX_SIZE
        {
            return ::core::result::Result::Err(
                ::ridl_rt::payload::VerifyError::Structure(
                    ::ridl_rt::payload::Malformed::TooLarge,
                ),
            );
        }
        let table = ::ridl_rt::flatbuffers::root(buf)
            .map_err(::ridl_rt::payload::VerifyError::Structure)?;
        __ridl_fb_verify_warning_flags(buf, table)?;
        ::core::result::Result::Ok(WarningFlagsFbView { buf, table })
    }
    fn decode(
        r: ::ridl_rt::payload::Ref<'_, Self, ::ridl_rt::encoding::FlatBuffers>,
    ) -> Self {
        let __view = r.view();
        __ridl_fb_decode_warning_flags(__view.buf, __view.table)
    }
}