Crate typenum [] [src]

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::{N4, Integer};

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::{Sum, Integer, 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.

Reexports

pub use consts::*;
pub use marker_traits::{NonZero, Ord, Bit, Unsigned, Integer};
pub use type_operators::{Cmp, Pow, Same};
pub use operator_aliases::{And, Or, Xor, Shleft, Shright, Sum, Diff, Prod, Quot, Mod, Negate, Exp, Add1, Sub1, Square, Cube, Compare};
pub use bit::{B0, B1};
pub use uint::{UInt, UTerm};
pub use int::{NInt, PInt};

Modules

bit

Type-level bits. These are rather simple and are used as the building blocks of the other number types in this crate.

consts

This module defines aliases for many constants. It is generated by typenum's build script.

int

Type-level signed integers.

marker_traits

These are all of the marker traits used in typenum. Note that the definition here for marker traits is slightly different than the conventional one --- we include traits with functions that convert a type to the corresponding value.

operator_aliases

This module provides aliases for the type operators used in this crate. Their purpose is to increase the ergonomics of performing operations on the types defined here.

type_operators

This module provides useful type operators that are not defined in core.

uint

Type-level unsigned integers.

Enums

Equal

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

Greater

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

Less

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