pub trait ArrayStruct {
type Value;
type Array;
type Refs<'a>
where Self: 'a;
type Muts<'a>
where Self: 'a;
type Index;
// Required methods
fn from_val(value: Self::Value) -> Self;
fn val(self) -> Self::Value;
fn from_array(array: Self::Array) -> Self;
fn to_array(self) -> Self::Array;
fn refs(&self) -> Self::Refs<'_>;
fn muts(&mut self) -> Self::Muts<'_>;
}
Expand description
A trait to name all the associated types and simplify the conversion to and from the helper types.
Required Associated Types§
Sourcetype Refs<'a>
where
Self: 'a
type Refs<'a> where Self: 'a
Helper type which is similar to the original field-struct declaration,
but with &'a T
instead of T
for the field type
Sourcetype Muts<'a>
where
Self: 'a
type Muts<'a> where Self: 'a
Helper type which is similar to the original field-struct declaration,
but with &'a mut T
instead of T
for the field type
Sourcetype Index
type Index
Helper type which contains helper functions to get the index of each field by name.
use array_as_struct::{array_as_struct, ArrayStruct};
#[array_as_struct]
pub struct Foo {
bar: u32,
baz: u32,
}
assert_eq!(<Foo as ArrayStruct>::Index::bar(), 0);
assert_eq!(<Foo as ArrayStruct>::Index::baz(), 1);
Required Methods§
Sourcefn from_val(value: Self::Value) -> Self
fn from_val(value: Self::Value) -> Self
Construct the tuple-struct type from the named-field type
use array_as_struct::{array_as_struct, ArrayStruct};
#[array_as_struct]
#[derive(Debug, PartialEq, Eq)]
pub struct Foo {
bar: u32,
baz: u32,
}
// Workaround rust-lang/rust#86935
type Value = <Foo as ArrayStruct>::Value;
let f = Foo::from_val(Value { bar: 10, baz: 15 });
assert_eq!(Foo([10, 15]), f);
Sourcefn val(self) -> Self::Value
fn val(self) -> Self::Value
Construct the named-field type from the tuple-struct type
use array_as_struct::{array_as_struct, ArrayStruct};
#[array_as_struct]
#[derive(Debug, PartialEq, Eq)]
pub struct Foo {
bar: u32,
baz: u32,
}
// Workaround rust-lang/rust#86935
type Value = <Foo as ArrayStruct>::Value;
let f = Foo([10, 15]);
assert_eq!(f.val(), Value { bar: 10, baz: 15 });
Sourcefn from_array(array: Self::Array) -> Self
fn from_array(array: Self::Array) -> Self
Construct the tuple-struct type from the underlying array type
This is basically the same as just calling the tuple-constructor
use array_as_struct::{array_as_struct, ArrayStruct};
#[array_as_struct]
#[derive(Debug, PartialEq, Eq)]
pub struct Foo {
bar: u32,
baz: u32,
}
assert_eq!(Foo([1, 2]), <Foo as ArrayStruct>::from_array([1, 2]));
Sourcefn to_array(self) -> Self::Array
fn to_array(self) -> Self::Array
Construct the tuple-struct type from the underlying array type
This is basically the same as just calling the .0
use array_as_struct::{array_as_struct, ArrayStruct};
#[array_as_struct]
#[derive(Copy, Clone)]
pub struct Foo {
bar: u32,
baz: u32,
}
let f = Foo([1, 2]);
assert_eq!(f.0, f.to_array());
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.