---
source: crates/ridl-backend-rust/src/tests.rs
expression: rust_for(decls)
---
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Name(String);
impl Name {
/// Constructs the value, enforcing its typl constraints.
pub fn new(
value: String,
) -> ::core::result::Result<Self, ::ridl_rt::payload::Violation> {
Self::check(&value)?;
::core::result::Result::Ok(Self::new_unchecked(value))
}
/// Checks `value` against this type's typl constraints, without
/// constructing it. `pub(crate)` rather than `pub`: a caller
/// outside `new` is a function generated into this crate — since
/// driftsys/ridl#467 that includes the codec of *another* package
/// of the same build, which reaches this type through the module
/// tree and so cannot see a private item here. The emitted crate
/// is one crate per build, so `pub(crate)` reaches every such
/// caller while adding nothing to the crate's public surface.
/// Whether this becomes `pub` is Epic 10's call, still open.
/// `new` is the composition of this and `new_unchecked`.
pub(crate) fn check(
value: &str,
) -> ::core::result::Result<(), ::ridl_rt::payload::Violation> {
if (value.chars().count() as u64) < 1 {
return ::core::result::Result::Err(::ridl_rt::payload::Violation {
type_name: "Name",
rule: ::ridl_rt::payload::Rule::Length,
});
}
if (value.chars().count() as u64) > 64 {
return ::core::result::Result::Err(::ridl_rt::payload::Violation {
type_name: "Name",
rule: ::ridl_rt::payload::Rule::Length,
});
}
::core::result::Result::Ok(())
}
/// Constructs the value without checking its constraints.
///
/// Safe: nothing here relies on the invariant for memory
/// soundness. Use it only for a value already known to satisfy
/// the contract.
pub const fn new_unchecked(value: String) -> Self {
Self(value)
}
pub fn get(&self) -> &str {
&self.0
}
pub fn into_inner(self) -> String {
self.0
}
}
impl ::core::convert::TryFrom<String> for Name {
type Error = ::ridl_rt::payload::Violation;
fn try_from(value: String) -> ::core::result::Result<Self, Self::Error> {
Self::new(value)
}
}
impl ::core::convert::From<Name> for String {
fn from(value: Name) -> Self {
value.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Digest(Vec<u8>);
impl Digest {
/// Constructs the value, enforcing its typl constraints.
pub fn new(
value: Vec<u8>,
) -> ::core::result::Result<Self, ::ridl_rt::payload::Violation> {
Self::check(&value)?;
::core::result::Result::Ok(Self::new_unchecked(value))
}
/// Checks `value` against this type's typl constraints, without
/// constructing it. `pub(crate)` rather than `pub`: a caller
/// outside `new` is a function generated into this crate — since
/// driftsys/ridl#467 that includes the codec of *another* package
/// of the same build, which reaches this type through the module
/// tree and so cannot see a private item here. The emitted crate
/// is one crate per build, so `pub(crate)` reaches every such
/// caller while adding nothing to the crate's public surface.
/// Whether this becomes `pub` is Epic 10's call, still open.
/// `new` is the composition of this and `new_unchecked`.
pub(crate) fn check(
value: &[u8],
) -> ::core::result::Result<(), ::ridl_rt::payload::Violation> {
if (value.len() as u64) > 32 {
return ::core::result::Result::Err(::ridl_rt::payload::Violation {
type_name: "Digest",
rule: ::ridl_rt::payload::Rule::Length,
});
}
::core::result::Result::Ok(())
}
/// Constructs the value without checking its constraints.
///
/// Safe: nothing here relies on the invariant for memory
/// soundness. Use it only for a value already known to satisfy
/// the contract.
pub const fn new_unchecked(value: Vec<u8>) -> Self {
Self(value)
}
pub fn get(&self) -> &[u8] {
&self.0
}
pub fn into_inner(self) -> Vec<u8> {
self.0
}
}
impl ::core::convert::TryFrom<Vec<u8>> for Digest {
type Error = ::ridl_rt::payload::Violation;
fn try_from(value: Vec<u8>) -> ::core::result::Result<Self, Self::Error> {
Self::new(value)
}
}
impl ::core::convert::From<Digest> for Vec<u8> {
fn from(value: Digest) -> Self {
value.0
}
}
impl Default for Digest {
fn default() -> Self {
Digest::new_unchecked(Vec::new())
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Ratio(f64);
impl Ratio {
/// Constructs the value, enforcing its typl constraints.
pub fn new(
value: f64,
) -> ::core::result::Result<Self, ::ridl_rt::payload::Violation> {
Self::check(&value)?;
::core::result::Result::Ok(Self::new_unchecked(value))
}
/// Checks `value` against this type's typl constraints, without
/// constructing it. `pub(crate)` rather than `pub`: a caller
/// outside `new` is a function generated into this crate — since
/// driftsys/ridl#467 that includes the codec of *another* package
/// of the same build, which reaches this type through the module
/// tree and so cannot see a private item here. The emitted crate
/// is one crate per build, so `pub(crate)` reaches every such
/// caller while adding nothing to the crate's public surface.
/// Whether this becomes `pub` is Epic 10's call, still open.
/// `new` is the composition of this and `new_unchecked`.
pub(crate) fn check(
value: &f64,
) -> ::core::result::Result<(), ::ridl_rt::payload::Violation> {
let value = *value;
if value < 0.0 {
return ::core::result::Result::Err(::ridl_rt::payload::Violation {
type_name: "Ratio",
rule: ::ridl_rt::payload::Rule::Range,
});
}
if value > 1.0 {
return ::core::result::Result::Err(::ridl_rt::payload::Violation {
type_name: "Ratio",
rule: ::ridl_rt::payload::Rule::Range,
});
}
::core::result::Result::Ok(())
}
/// Constructs the value without checking its constraints.
///
/// Safe: nothing here relies on the invariant for memory
/// soundness. Use it only for a value already known to satisfy
/// the contract.
pub const fn new_unchecked(value: f64) -> Self {
Self(value)
}
pub const fn get(self) -> f64 {
self.0
}
}
impl ::core::convert::TryFrom<f64> for Ratio {
type Error = ::ridl_rt::payload::Violation;
fn try_from(value: f64) -> ::core::result::Result<Self, Self::Error> {
Self::new(value)
}
}
impl ::core::convert::From<Ratio> for f64 {
fn from(value: Ratio) -> Self {
value.0
}
}
impl Default for Ratio {
fn default() -> Self {
Ratio::new_unchecked(0.0)
}
}
/// An accessor over FlatBuffers bytes `Name`'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 NameFbView<'a> {
pub(crate) buf: &'a [u8],
pub(crate) table: usize,
}
#[allow(deprecated)]
impl<'a> NameFbView<'a> {
/// The verified bytes this view reads.
pub fn bytes(&self) -> &'a [u8] {
self.buf
}
/// The value the box carries. `Name` owns its bytes, so this allocates — unlike a struct field of the same type, which a view borrows in place.
pub fn value(&self) -> Name {
__ridl_fb_decode_name(self.buf, self.table)
}
}
/// Writes `Name` as its box table and returns its position (ADR-0019 decision 8).
#[allow(deprecated)]
pub(crate) fn __ridl_fb_encode_name(
value: &Name,
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: ::ridl_rt::flatbuffers::Field::Offset(
builder.push_string(value.get())?,
),
},
];
builder.push_table(8usize, 4usize, 1u16, &__box)?
})
}
#[allow(deprecated)]
pub(crate) fn __ridl_fb_verify_name(
buf: &[u8],
table: usize,
) -> ::core::result::Result<(), ::ridl_rt::payload::VerifyError> {
match ::ridl_rt::flatbuffers::field(buf, table, 0u16, 4usize)
.map_err(::ridl_rt::payload::VerifyError::Structure)?
{
::core::option::Option::Some(__p) => {
let __s = ::ridl_rt::flatbuffers::string(buf, __p)
.map_err(::ridl_rt::payload::VerifyError::Structure)?;
Name::check(__s).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_name(buf: &[u8], table: usize) -> Name {
{
let __p = ::ridl_rt::flatbuffers::field(buf, table, 0u16, 4usize)
.unwrap_or(::core::option::Option::None)
.unwrap_or(0usize);
Name::new_unchecked(
String::from(::ridl_rt::flatbuffers::string(buf, __p).unwrap_or("")),
)
}
}
#[allow(deprecated)]
impl ::ridl_rt::payload::Payload<::ridl_rt::encoding::FlatBuffers> for Name {
/// The largest FlatBuffers buffer any legal `Name` encodes to: 314 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 = 314usize;
type View<'a> = NameFbView<'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_name(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: NameFbView { 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_name(buf, table)?;
::core::result::Result::Ok(NameFbView { buf, table })
}
fn decode(
r: ::ridl_rt::payload::Ref<'_, Self, ::ridl_rt::encoding::FlatBuffers>,
) -> Self {
let __view = r.view();
__ridl_fb_decode_name(__view.buf, __view.table)
}
}
/// An accessor over FlatBuffers bytes `Digest`'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 DigestFbView<'a> {
pub(crate) buf: &'a [u8],
pub(crate) table: usize,
}
#[allow(deprecated)]
impl<'a> DigestFbView<'a> {
/// The verified bytes this view reads.
pub fn bytes(&self) -> &'a [u8] {
self.buf
}
/// The value the box carries. `Digest` owns its bytes, so this allocates — unlike a struct field of the same type, which a view borrows in place.
pub fn value(&self) -> Digest {
__ridl_fb_decode_digest(self.buf, self.table)
}
}
/// Writes `Digest` as its box table and returns its position (ADR-0019 decision 8).
#[allow(deprecated)]
pub(crate) fn __ridl_fb_encode_digest(
value: &Digest,
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: ::ridl_rt::flatbuffers::Field::Offset(
builder.push_vector(value.get(), 1usize)?,
),
},
];
builder.push_table(8usize, 4usize, 1u16, &__box)?
})
}
#[allow(deprecated)]
pub(crate) fn __ridl_fb_verify_digest(
buf: &[u8],
table: usize,
) -> ::core::result::Result<(), ::ridl_rt::payload::VerifyError> {
match ::ridl_rt::flatbuffers::field(buf, table, 0u16, 4usize)
.map_err(::ridl_rt::payload::VerifyError::Structure)?
{
::core::option::Option::Some(__p) => {
let __v = ::ridl_rt::flatbuffers::vector(buf, __p, 1usize)
.map_err(::ridl_rt::payload::VerifyError::Structure)?;
Digest::check(&buf[__v.first..__v.first + __v.len])
.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_digest(buf: &[u8], table: usize) -> Digest {
{
let __p = ::ridl_rt::flatbuffers::field(buf, table, 0u16, 4usize)
.unwrap_or(::core::option::Option::None)
.unwrap_or(0usize);
Digest::new_unchecked({
let __v = ::ridl_rt::flatbuffers::vector(buf, __p, 1usize)
.unwrap_or(::ridl_rt::flatbuffers::Vector {
len: 0,
first: 0,
});
buf.get(__v.first..__v.first + __v.len).unwrap_or(&[]).to_vec()
})
}
}
#[allow(deprecated)]
impl ::ridl_rt::payload::Payload<::ridl_rt::encoding::FlatBuffers> for Digest {
/// The largest FlatBuffers buffer any legal `Digest` encodes to: 89 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 = 89usize;
type View<'a> = DigestFbView<'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_digest(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: DigestFbView { 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_digest(buf, table)?;
::core::result::Result::Ok(DigestFbView { buf, table })
}
fn decode(
r: ::ridl_rt::payload::Ref<'_, Self, ::ridl_rt::encoding::FlatBuffers>,
) -> Self {
let __view = r.view();
__ridl_fb_decode_digest(__view.buf, __view.table)
}
}
/// An accessor over FlatBuffers bytes `Ratio`'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 RatioFbView<'a> {
pub(crate) buf: &'a [u8],
pub(crate) table: usize,
}
#[allow(deprecated)]
impl<'a> RatioFbView<'a> {
/// The verified bytes this view reads.
pub fn bytes(&self) -> &'a [u8] {
self.buf
}
/// The value the box carries. `Ratio` is one value, so this decodes it rather than borrowing it, which costs one read.
pub fn value(&self) -> Ratio {
__ridl_fb_decode_ratio(self.buf, self.table)
}
}
/// Writes `Ratio` as its box table and returns its position (ADR-0019 decision 8).
#[allow(deprecated)]
pub(crate) fn __ridl_fb_encode_ratio(
value: &Ratio,
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: 8u16,
value: {
let __s = *value;
::ridl_rt::flatbuffers::Field::F64(__s.get())
},
},
];
builder.push_table(16usize, 8usize, 1u16, &__box)?
})
}
#[allow(deprecated)]
pub(crate) fn __ridl_fb_verify_ratio(
buf: &[u8],
table: usize,
) -> ::core::result::Result<(), ::ridl_rt::payload::VerifyError> {
match ::ridl_rt::flatbuffers::field(buf, table, 0u16, 8usize)
.map_err(::ridl_rt::payload::VerifyError::Structure)?
{
::core::option::Option::Some(__p) => {
let __raw = ::ridl_rt::flatbuffers::read_f64(buf, __p)
.map_err(::ridl_rt::payload::VerifyError::Structure)?;
Ratio::check(&(__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_ratio(buf: &[u8], table: usize) -> Ratio {
{
let __p = ::ridl_rt::flatbuffers::field(buf, table, 0u16, 8usize)
.unwrap_or(::core::option::Option::None)
.unwrap_or(0usize);
Ratio::new_unchecked(
::ridl_rt::flatbuffers::read_f64(buf, __p).unwrap_or(0.0f64),
)
}
}
#[allow(deprecated)]
impl ::ridl_rt::payload::Payload<::ridl_rt::encoding::FlatBuffers> for Ratio {
/// The largest FlatBuffers buffer any legal `Ratio` encodes to: 50 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 = 50usize;
type View<'a> = RatioFbView<'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_ratio(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: RatioFbView { 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_ratio(buf, table)?;
::core::result::Result::Ok(RatioFbView { buf, table })
}
fn decode(
r: ::ridl_rt::payload::Ref<'_, Self, ::ridl_rt::encoding::FlatBuffers>,
) -> Self {
let __view = r.view();
__ridl_fb_decode_ratio(__view.buf, __view.table)
}
}