#typenum #substrate #unsigned-integer #signed-integer #numbers #compile #bits

no-std substrate-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. This substrate-typenum fork just adds SCALE codec derive for use with substrate

1 stable release

1.16.0 Oct 25, 2023

#14 in #typenum

Download history 798/week @ 2024-04-08 901/week @ 2024-04-15 532/week @ 2024-04-22 560/week @ 2024-04-29 474/week @ 2024-05-06 574/week @ 2024-05-13 724/week @ 2024-05-20 834/week @ 2024-05-27 924/week @ 2024-06-03 826/week @ 2024-06-10 676/week @ 2024-06-17 1253/week @ 2024-06-24 833/week @ 2024-07-01 678/week @ 2024-07-08 1220/week @ 2024-07-15 1037/week @ 2024-07-22

3,800 downloads per month
Used in 23 crates (via substrate-fixed)

MIT/Apache

195KB
5.5K 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, perfoming 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–520KB
~11K SLoC