Skip to content

Latest commit

 

History

36 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Specification

crates.io docs.rs CI

Compile-time classification of types according to Rust specification.

Classification Axes

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 a typenum unsigned integer.
  • Trap: whether the value representation has trap values.
  • Niche: stable, unstable, or absent niche value.

Layout

Describes total reachable (through pointer indirection) representation stability:

  • Stable: compiler-guaranteed representation.
  • Unstable: representation is not guaranteed.

Size

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.

Alignment

The ABI alignment in bytes as a power-of-two typenum value.

Trap

Describes total reachable (through pointer indirection) value-representation validity:

  • Robust: every bit pattern is valid.
  • NonRobust: some bit patterns are trap/invalid values.

Niche

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.

How to Use

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;

About

Classification of types in accordance with the Rust specification

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages