zerocopy

Derive Macro Unaligned

#[derive(Unaligned)]
Expand description

Analyzes whether a type is Unaligned.

This derive analyzes, at compile time, whether the annotated type satisfies the safety conditions of Unaligned and implements Unaligned if it is sound to do so. This derive can be applied to structs, enums, and unions; e.g.:

#[derive(Unaligned)]
#[repr(C)]
struct MyStruct {
    ...
}

#[derive(Unaligned)]
#[repr(u8)]
enum MyEnum {
    ...
}

#[derive(Unaligned)]
#[repr(packed)]
union MyUnion {
    ...
}

§Analysis

This section describes, roughly, the analysis performed by this derive to determine whether it is sound to implement Unaligned for a given type. Unless you are modifying the implementation of this derive, or attempting to manually implement Unaligned for a type yourself, you don’t need to read this section.

If a type has the following properties, then this derive can implement Unaligned for that type:

  • If the type is a struct or union:
    • If repr(align(N)) is provided, N must equal 1.
    • If the type is repr(C) or repr(transparent), all fields must be Unaligned.
    • If the type is not repr(C) or repr(transparent), it must be repr(packed) or repr(packed(1)).
  • If the type is an enum:
    • If repr(align(N)) is provided, N must equal 1.
    • It must be a field-less enum (meaning that all variants have no fields).
    • It must be repr(i8) or repr(u8).