Add sized encode buffer a la itoa

This commit is contained in:
Michael Pfaff 2023-04-10 20:09:50 -04:00
parent 8d37e7c549
commit 9a878ff83d
Signed by: michael
GPG Key ID: CF402C4A012AA9D4
1 changed files with 23 additions and 0 deletions

View File

@ -450,6 +450,29 @@ pub trait Encode {
pub struct Encoder<const UPPER: bool = false>;
pub struct Buffer<const N: usize>(MaybeUninit<[u8; N * 2]>) where [u8; N * 2]:;
impl<const N: usize> Buffer<N> where [u8; N * 2]: {
#[inline]
pub fn new() -> Self {
Self(MaybeUninit::uninit())
}
#[inline]
pub fn format_exact<const UPPER: bool>(&mut self, bytes: &[u8; N]) -> &str {
self.0 = MaybeUninit::new(Encoder::<UPPER>::enc_sized(bytes));
unsafe { std::str::from_utf8_unchecked(self.0.assume_init_ref()) }
}
// TODO: support using only part of the buffer.
/*pub fn format<const UPPER: bool>(&mut self, bytes: &[u8]) -> &str {
assert!(bytes.len() <= N);
self.0 = MaybeUninit::new(Encoder::<UPPER>::enc_slice(bytes));
unsafe { std::str::from_utf8_unchecked(self.0.assume_init_ref()) }
}*/
}
#[repr(align(32))]
struct Aligned32<T>(T);