support compile-time number conversions#23
Merged
mmstick merged 10 commits intommstick:masterfrom Jun 1, 2025
Merged
Conversation
Contributor
Author
|
cc @mmstick when you get a chance |
mmstick
approved these changes
Jun 1, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
NumToAtrait is convenient, but currently cannot be used in const contextswe cannot define const functions on the existing
NumToAtrait (for now), so instead, we can defineconstlibrary 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
constfunctions of the formatnumtoa_u16,numtoa_str_u16, etc. and is fully backwards compatible, with the following notable changes:copy_from_slicewas replaced with a new macrocopy_2_dec_lut_bytes&string[x..]was replaced withstring.split_at(x).1here is a usage example of the new const functions:
the number
12345is 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 thestringstool 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.