Add sized encode buffer a la itoa
This commit is contained in:
parent
8d37e7c549
commit
9a878ff83d
23
src/enc.rs
23
src/enc.rs
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue