type-const 1.1.1

Type-level associated consts
Documentation
  • Coverage
  • 100%
    13 out of 13 items documented2 out of 8 items with examples
  • Size
  • Source code size: 10.11 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.06 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • maxdexh/type-const
    1 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • maxdexh

Crates.io Documentation Rust

This crate provides simple utils for passing const values around through generics, in the form of types.

The concept of a type const is expressed through the Const trait, which holds the type and the value of the constant.

Passing values that are known at compile time through generics is different from passing them through arguments, for example:

const fn array_of_const<C: type_const::Const, const N: usize>() -> [C::Type; N] {
    [C::VALUE; N] // no `Copy` needed!
}
assert_eq!(array_of_const::<type_const::DefaultOf<i32>, 3>(), [0; 3]);

This may also be used to write const "functions" in traits without the nightly const_trait feature. Note that unlike const fn, these can only be evaluated at compile time.

trait AddConst<Rhs=Self> {
    type Output;
    type Add<
        LhsC: type_const::Const<Type = Self>,
        RhsC: type_const::Const<Type = Rhs>,
    >: Const<Type = Self::Output>;
}