typenum-consts
Procedural macros that take a literal integer (or the result of an evaluation of simple mathematical expressions or an environment variable whose value is a literal integer) and convert it to a typenum::Unsigned / typenum::Integer type-level positive/negative/unsigned integer.
Why?
It saves time.
Assuming you want a type-level positive integer 84938493, tnconst![+84938493] outputs directly typenum::PInt<U84938493> (by the way, U84938493 does not exist in typenum::consts). The alternative is to type PInt<Sum<Prod<Exp<..., ...>, ...>, ...>, ...> (which argubly takes a lot more time).
Example:
# use PhantomData;
# use tnconst;
# use *;
#
#
# type I32OrI64 = i32;
#
# type I32OrI64 = i64;
type ActualPositive84938493Type = tnconst!;
type ExpectedPositive84938493Type = ;
assert_type_eq!;
assert_eq!;
For conditional compilation.
Suppose in different environments you want a different type-level integer, you can either use e.g. #[cfg(production)] type NUMBER = U69; or you can do the following:
use ;
use uconst;
// ``` .env
// ENV_VAR=69
// ```
type E = uconst!;
assert_type_eq!;
All four macros, namely, tnconst![...], pconst![...], uconst![...] and nconst![...], can read literal integers from the environment.
Three ways to use it
Take pconst![...] as the example (the rest of the macros work almost identically).
1. Invoke it with a literal integer
use ;
use pconst;
type A = pconst!;
assert_type_eq!;
2. Invoke using an expression or many simple mathematical expressions
use ;
use pconst;
type D = pconst!;
assert_type_eq!;
3. Invoke by reading from an environment variable
Note: env!(...) is a macro-like invocation. The first parameter is mandatory and is the key of the environment variable that pconst will read. The second parameter is optional and is the file path of the .env.* file to read the environment variable from, e.g. env!("ENV_VAR", "./.env.prod"), "ENV_VAR" is the key to read the value from and "./.env.prod" is the file path relative to CARGO_MANIFEST_DIR.
use ;
use pconst;
// ``` .env
// ENV_VAR=69
// ```
type E = pconst!;
assert_type_eq!;
Roadmap
- Feature gate evaluation of mathematical expressions and reading from environment variables.
- Enable testing for Rust version 1.70.
Vendored Crates
rsc version 3.0.0
Reasons for vendoring rsc.
- As of 28 March 2024, there is a version 3.0.0 of the crate on the GitHub repository but without a corresponding crate on
crates.io. - Easier to implement
Numforisizewith vendoring. - `typenum-const needs a mathematical expression evaluator.
- Its license allows for 'usage without attribution'. Anyway,
src/vendors/rsc/Cargo.toml.vendoredis the originalCargo.tomlfile found in the repository. - Thanks to Luke Wilson.
License
Licensed under either of
-
Apache License, Version 2.0
-
MIT license
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.