support compile-time number conversions #23
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
background
the
NumToA
trait is convenient, but currently cannot be used in const contextswe cannot define const functions on the existing
NumToA
trait (for now), so instead, we can defineconst
library functions. this change will allow numtoa to convert numbers into strings 100% at compile time!the PR contains mostly simple refactors to move the core logic into
const
functions of the formatnumtoa_u16
,numtoa_str_u16
, etc. and is fully backwards compatible, with the following notable changes:copy_from_slice
was replaced with a new macrocopy_2_dec_lut_bytes
&string[x..]
was replaced withstring.split_at(x).1
here is a usage example of the new const functions:
the number
12345
is converted to ascii form purely at compile time. if we inspect the binary, we can see that numtoa code is not present in the final binary. using thestrings
tool additionally shows that the decimal lookup table is not present, whereas it is easily noticeable in a binary that was compiled with e.g.println!("hello world: {}", 12345.numtoa_str(10, &mut [0_u8; 20]));
making the code much more efficient.the boilerplate code is a bit unfortunate & unwieldy but i have a future PR that will make compile-time conversion much easier & more ergonomic as a one-liner.