| 1 |
[](https://crates.io/crates/typenum) |
| 2 |
[](https://github.com/paholg/typenum/actions/workflows/check.yml) |
| 3 |
|
| 4 |
Typenum |
| 5 |
===== |
| 6 |
|
| 7 |
Typenum is a Rust library for type-level numbers evaluated at compile time. It |
| 8 |
currently supports bits, unsigned integers, and signed integers. |
| 9 |
|
| 10 |
Typenum depends only on libcore, and so is suitable for use on any platform! |
| 11 |
|
| 12 |
For the full documentation, go [here](https://docs.rs/typenum). |
| 13 |
|
| 14 |
### Importing |
| 15 |
|
| 16 |
While `typenum` is divided into several modules, they are all re-exported |
| 17 |
through the crate root, so you can import anything contained herein with `use |
| 18 |
typenum::whatever;`, ignoring the crate structure. |
| 19 |
|
| 20 |
You may also find it useful to treat the `consts` module as a prelude, |
| 21 |
performing a glob import. |
| 22 |
|
| 23 |
### Example |
| 24 |
|
| 25 |
Here is a trivial example of `typenum`'s use: |
| 26 |
|
| 27 |
```rust |
| 28 |
use typenum::{Sum, Exp, Integer, N2, P3, P4}; |
| 29 |
|
| 30 |
type X = Sum<P3, P4>; |
| 31 |
assert_eq!(<X as Integer>::to_i32(), 7); |
| 32 |
|
| 33 |
type Y = Exp<N2, P3>; |
| 34 |
assert_eq!(<Y as Integer>::to_i32(), -8); |
| 35 |
``` |
| 36 |
|
| 37 |
For a non-trivial example of its use, see one of the crates that depends on |
| 38 |
it. The full list is |
| 39 |
[here](https://crates.io/crates/typenum/reverse_dependencies). Of note are |
| 40 |
[dimensioned](https://crates.io/crates/dimensioned/) which does compile-time |
| 41 |
type checking for arbitrary unit systems and |
| 42 |
[generic-array](https://crates.io/crates/generic-array/) which provides arrays |
| 43 |
whose length you can generically refer to. |
| 44 |
|
| 45 |
### Error messages |
| 46 |
|
| 47 |
|
| 48 |
Typenum's error messages aren't great, and can be difficult to parse. The good |
| 49 |
news is that the fine folks at Auxon have written a tool to help with it. Please |
| 50 |
take a look at [tnfilt](https://github.com/auxoncorp/tnfilt). |
| 51 |
|
| 52 |
### License |
| 53 |
|
| 54 |
Licensed under either of |
| 55 |
|
| 56 |
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or |
| 57 |
http://www.apache.org/licenses/LICENSE-2.0) |
| 58 |
* MIT license |
| 59 |
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 60 |
|
| 61 |
at your option. |
| 62 |
|
| 63 |
### Contribution |
| 64 |
|
| 65 |
Unless you explicitly state otherwise, any contribution intentionally submitted |
| 66 |
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 67 |
dual licensed as above, without any additional terms or conditions. |