Compile-time classification of types according to Rust specification.
RustSpec describes a type across the following axes:
Layout: representation stability.Size: statically sized, metadata-sized, or extern-type-like shape.Alignment: ABI alignment in bytes as atypenumunsigned integer.Trap: whether the value representation has trap values.Niche: stable, unstable, or absent niche value.
Describes total reachable (through pointer indirection) representation stability:
Stable: compiler-guaranteed representation.Unstable: representation is not guaranteed.
Describes compile-time size and pointer metadata shape:
Sized<Zst>: compile-time known zero-sized type.Sized<NonZst>: compile-time known non-zero-sized type.MetaSized<SliceLike>: dynamically sized slice-like type.MetaSized<DynTraitLike>: dynamically sized trait-object-like type.ExternTypeLike: dynamically sized extern-type-like type.
The ABI alignment in bytes as a power-of-two typenum value.
Describes total reachable (through pointer indirection) value-representation validity:
Robust: every bit pattern is valid.NonRobust: some bit patterns are trap/invalid values.
Describes whether and what kind of niche is available for the type:
WithoutNiche: no niche is available.WithNiche<Stable>: compiler-guaranteed niche.WithNiche<Unstable>: niche exists but is not guaranteed.
Derive RustSpec for your types, then use its associated marker families as bounds when implementing other traits:
use disjoint_impls::disjoint_impls;
use rust_spec::{
RustSpec,
niche::{self, WithNiche, WithoutNiche},
};
trait NullableEncoding {
const NEEDS_TAG: bool;
}
#[derive(RustSpec)]
struct Header {
id: u32,
flags: u16,
}
disjoint_impls! {
impl<T: RustSpec<Niche = WithoutNiche>> NullableEncoding for T {
const NEEDS_TAG: bool = true;
}
impl<T: RustSpec<Niche = WithNiche<niche::Stable>>> NullableEncoding for T {
const NEEDS_TAG: bool = false;
}
impl<T: RustSpec<Niche = WithNiche<niche::Unstable>>> NullableEncoding for T {
const NEEDS_TAG: bool = false;
}
}
const HEADER_OPTION_NEEDS_TAG: bool = Header::NEEDS_TAG;