Skip to content

Conversation

@master-hax
Copy link
Contributor

@master-hax master-hax commented Jul 28, 2025

background

the core numtoa_ functions (and by extension, the entire library) never reads any data from the output buffer that it has not already written. this should allow us to safely operate on uninitialized slices because the underlying data that we read is always overwritten i.e. we can operate on [MaybeUninit<u8>] instead of [u8]

in this PR, i update the code to introduce a new set of API with a set of core functions numtoa_uninit_ e,g, numtoa_uninit_u8 & numtoa_uninit_u8_str that allows operating on uninitialized data. though this does add some extra usage of unsafe, i think the usage is directly analogous to the existing usage of core::str::from_utf8_unchecked & only used when necessary.

note that i had to add a bunch of ugly = MaybeUninit::new() into the codebase because MaybeUninit::write doesn't work in const-contexts, meaning we would have had to otherwise remove compile-time conversion support. i think we can refactor this into a macro in a future PR.

tl;dr: this PR provides a new more optimized way to use the classic reused buffer API while also optimizing existing usage of the streamlined API

changes

  • refactor numtoa_ functions
  • add new numtoa_uninit_ const functions e.g. numtoa_uninit_i16_str
  • add new numtoa_uinit_X_str const functions e.g. numtoa_uninit_u128_str
  • add new numtoa_uinit & numtoa_uinit_str functions to NumToA trait
  • update the code to handle MaybeUninit<u8> instead of u8
  • add unsafe helper function assume_slice_init
  • add what i believe to be an always safe helper function assume_mut_slice_uninit
  • update AsciiNumber to use MaybeUninit<u8>
  • add constructor with debug assertion to AsciiNumber
  • add unit tests

@master-hax master-hax changed the title allow operating on uninitialized data [WIP] allow operating on uninitialized data Jul 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant