#const #constant #utility #rust

no-std const_stringify_ints

Converts (generic) constant integers to &'static str

1 unstable release

0.1.0 Jul 11, 2022

#70 in #constant

MIT/Apache

18KB
456 lines

const_stringify_ints

Converts (generic) constant integers to &'static str.

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.


lib.rs:

This library solves a very specific problem: You have a (possibly generic) constant integer, and you want it as a &'static str.

All methods are named const_str_ and then the type.

use const_stringify_ints::*;

const SIZE_OF_MY_ARR:usize = 294;
const SIZE_STR:&'static str = const_str_usize::<SIZE_OF_MY_ARR>();

assert_eq!(SIZE_OF_MY_ARR.to_string().as_str(), SIZE_STR);

Support

This library supports no_std environments.

This library should in theory support targets with a 16-bit, however I don't have a way of testing this. Additionaly, these targets are likely very memory-constrained, and this crate always 'allocates' 5 bytes of static memory for a u16 even when only 1 byte is needed.

No runtime deps