#compile-time #numbers #unsigned-integer #signed-integer #type-level #bit #evaluated

no-std typenum

Typenum is a Rust library for type-level numbers evaluated at compile time. It currently supports bits, unsigned integers, and signed integers. It also provides a type-level array of type-level numbers, but its implementation is incomplete.

26 stable releases

1.17.0 Sep 15, 2023
1.16.0 Dec 5, 2022
1.15.0 Dec 26, 2021
1.14.0 Sep 1, 2021
0.1.1 Oct 16, 2015

#43 in Rust patterns

Download history 1982616/week @ 2024-08-13 2043564/week @ 2024-08-20 1976279/week @ 2024-08-27 2099366/week @ 2024-09-03 2012061/week @ 2024-09-10 2038973/week @ 2024-09-17 2196858/week @ 2024-09-24 2515521/week @ 2024-10-01 2727769/week @ 2024-10-08 2659153/week @ 2024-10-15 2223013/week @ 2024-10-22 2105848/week @ 2024-10-29 2093323/week @ 2024-11-05 2147642/week @ 2024-11-12 2172365/week @ 2024-11-19 1521621/week @ 2024-11-26

8,312,736 downloads per month
Used in 29,500 crates (300 directly)

MIT/Apache

200KB
6K SLoC

crates.io Build Status

Typenum

Typenum is a Rust library for type-level numbers evaluated at compile time. It currently supports bits, unsigned integers, and signed integers.

Typenum depends only on libcore, and so is suitable for use on any platform!

For the full documentation, go here.

Importing

While typenum is divided into several modules, they are all re-exported through the crate root, so you can import anything contained herein with use typenum::whatever;, ignoring the crate structure.

You may also find it useful to treat the consts module as a prelude, performing a glob import.

Example

Here is a trivial example of typenum's use:

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

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

type Y = Exp<N2, P3>;
assert_eq!(<Y as Integer>::to_i32(), -8);

For a non-trivial example of its use, see one of the crates that depends on it. The full list is here. Of note are dimensioned which does compile-time type checking for arbitrary unit systems and generic-array which provides arrays whose length you can generically refer to.

Error messages

Typenum's error messages aren't great, and can be difficult to parse. The good news is that the fine folks at Auxon have written a tool to help with it. Please take a look at tnfilt.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–640KB
~13K SLoC