Crate typenum

source ·
Expand description

This crate provides type-level numbers evaluated at compile time. It depends only on libcore.

The traits defined or used in this crate are used in a typical manner. They can be divided into two categories: marker traits and type operators.

Many of the marker traits have functions defined, but they all do essentially the same thing: convert a type into its runtime counterpart, and are really just there for debugging. For example,

use typenum::{Integer, N4};

assert_eq!(N4::to_i32(), -4);

Type operators are traits that behave as functions at the type level. These are the meat of this library. Where possible, traits defined in libcore have been used, but their attached functions have not been implemented.

For example, the Add trait is implemented for both unsigned and signed integers, but the add function is not. As there are never any objects of the types defined here, it wouldn’t make sense to implement it. What is important is its associated type Output, which is where the addition happens.

use std::ops::Add;
use typenum::{Integer, P3, P4};

type X = <P3 as Add<P4>>::Output;
assert_eq!(<X as Integer>::to_i32(), 7);

In addition, helper aliases are defined for type operators. For example, the above snippet could be replaced with

use typenum::{Integer, Sum, P3, P4};

type X = Sum<P3, P4>;
assert_eq!(<X as Integer>::to_i32(), 7);

Documented in each module is the full list of type operators implemented.

Re-exports§

  • pub use crate::array::ATerm;
  • pub use crate::array::TArr;
  • pub use crate::int::NInt;
  • pub use crate::int::PInt;
  • pub use crate::uint::UInt;
  • pub use crate::uint::UTerm;
  • pub use consts::False;
  • pub use consts::True;
  • pub use consts::B0;
  • pub use consts::B1;
  • pub use consts::U0;
  • pub use consts::U1;
  • pub use consts::U2;
  • pub use consts::N1;
  • pub use consts::N2;
  • pub use consts::Z0;
  • pub use consts::P1;
  • pub use consts::P2;
  • pub use generic_const_mappings::Const;
    const-generics
  • pub use generic_const_mappings::ToUInt;
    const-generics
  • pub use generic_const_mappings::U;
    const-generics
  • pub use crate::marker_traits::*;
  • pub use crate::operator_aliases::*;
  • pub use crate::type_operators::*;
  • pub use consts::*;
  • pub use consts::*;

Modules§

  • A type-level array of type-level numbers.
  • Type-level bits.
  • Type aliases for many constants.
  • Module with some const-generics-friendly definitions, to help bridge the gap between those and typenum types.
  • Type-level signed integers.
  • All of the marker traits used in typenum.
  • Aliases for the type operators used in this crate. Their purpose is to increase the ergonomics of performing operations on the types defined here. For even more ergonomics, consider using the op! macro instead.
  • Useful type operators that are not defined in core::ops.
  • Type-level unsigned integers.

Macros§

  • Asserts that a type is True, aka B1.
  • Asserts that two types are the same.
  • cmpDeprecated
    A convenience macro for comparing type numbers. Use op! instead.
  • Convenient type operations.
  • Create a new type-level array. Only usable on Rust 1.13.0 or newer.

Structs§

  • A potential output from Cmp, this is the type equivalent to the enum variant core::cmp::Ordering::Equal.
  • A potential output from Cmp, this is the type equivalent to the enum variant core::cmp::Ordering::Greater.
  • A potential output from Cmp, this is the type equivalent to the enum variant core::cmp::Ordering::Less.