Trait ArrayStruct

Source
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§

Source

type Value

Helper type which is identical to the original field-struct declaration

Source

type Array

The underlying array type

Source

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

Source

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

Source

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§

Source

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);
Source

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 });
Source

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]));
Source

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());
Source

fn refs(&self) -> Self::Refs<'_>

Construct the reference-named-field type from the tuple-struct type.

This is the primary way of accessing the named fields

Source

fn muts(&mut self) -> Self::Muts<'_>

Construct the mutable-reference-named-field type from the tuple-struct type

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.

Implementors§