Skip to main content

FixedArray

Derive Macro FixedArray 

Source
#[derive(FixedArray)]
{
    // Attributes available to this derive:
    #[fixed_array]
}
Expand description

Derives byte-array conversion impls for a fixed-size type.

Generates:

  • TryFrom<[u8; SIZE]> and TryFrom<&[u8; SIZE]>, or From<[u8; SIZE]> and From<&[u8; SIZE]> when infallible (decoding via DecodeFixed).
  • TryFrom<&[u8]>
  • From<T> for [u8; SIZE]
  • From<&T> for [u8; SIZE]

The type must implement Read<Cfg = ()> and EncodeFixed.

§Attributes

  • #[fixed_array(infallible)]: emit From<[u8; SIZE]> instead of TryFrom<[u8; SIZE]>. The type’s decode must never fail (any [u8; SIZE] is a valid value), since the generated From unwraps the DecodeFixed result.
  • #[fixed_array(bytes([u8; N]))]: required for any generic type (lifetime, type, or const). Stable Rust forbids a generic parameter inside the const expression [u8; <T as FixedSize>::SIZE], so the byte array type must be named.