Crate typenum [] [src]

This crate provides type-level numbers evaluated at compile time.

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,

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 the stdlib 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::consts::{P3, P4};
use typenum::int::Integer;

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

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

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.

uint

Type-level unsigned integers.

Enums

Equal
Greater
Less

Traits

Cmp

A type operator for comparing Self and Rhs. It provides a similar functionality to the function std::cmp::Ord::cmp but for types.

NonZero

A marker trait to designate that a type is not zero. All number types in this crate implement NonZero except B0, U0, and Z0.

Ord

A Marker trait for the types Greater, Equal, and Less.

Pow

A type operator that provides exponentiation by repeated squaring.

Same

A type operator that ensures that Rhs is the same as Self, it is mainly useful for writing macros that can take arbitrary binary or unary operators.

Type Definitions

Add1

Alias to make it easy to add 1: Add1<A> = <A as Add<B1>>::Output

And

Alias for the associated type of BitAnd: And<A, B> = <A as BitAnd<B>>::Output

Compare

Alias for the associated type of Cmp: Compare<A, B> = <A as Cmp<B>>::Output

Cube

Alias to make it easy to square. Cube<A> = <Square<A> as Mul<A>>::Output

Diff

Alias for the associated type of Sub: Diff<A, B> = <A as Sub<B>>::Output

Exp

Alias for the associated type of Pow: Exp<A, B> = <A as Pow<B>>::Output

Mod

Alias for the associated type of Rem: Mod<A, B> = <A as Rem<B>>::Output

Negate

Alias for the associated type of Neg: Negate<A> = <A as Neg>::Output

Or

Alias for the associated type of BitOr: Or<A, B> = <A as BitOr<B>>::Output

Prod

Alias for the associated type of Mul: Prod<A, B> = <A as Mul<B>>::Output

Quot

Alias for the associated type of Div: Quot<A, B> = <A as Div<B>>::Output

Shleft

Alias for the associated type of Shl: Shleft<A, B> = <A as Shl<B>>::Output

Shright

Alias for the associated type of Shr: Shright<A, B> = <A as Shr<B>>::Output

Square

Alias to make it easy to square. Square<A> = <A as Mul<A>>::Output

Sub1

Alias to make it easy to subtract 1: Sub1<A> = <A as Sub<B1>>::Output

Sum

Alias for the associated type of Add: Sum<A, B> = <A as Add<B>>::Output

Xor

Alias for the associated type of BitXor: Xor<A, B> = <A as BitXor<B>>::Output