2022-08-16 17:48:55 +02:00
|
|
|
use crate::fmt;
|
2022-08-16 18:12:06 +02:00
|
|
|
use crate::mem::MaybeUninit;
|
2022-08-16 17:48:55 +02:00
|
|
|
use crate::str;
|
|
|
|
|
|
|
|
/// Used for slow path in `Display` implementations when alignment is required.
|
2022-08-16 20:23:32 +02:00
|
|
|
pub struct DisplayBuffer<const SIZE: usize> {
|
2022-08-16 18:12:06 +02:00
|
|
|
buf: [MaybeUninit<u8>; SIZE],
|
2022-08-16 17:48:55 +02:00
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
2022-08-16 20:23:32 +02:00
|
|
|
impl<const SIZE: usize> DisplayBuffer<SIZE> {
|
2022-08-16 19:32:00 +02:00
|
|
|
#[inline]
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self { buf: MaybeUninit::uninit_array(), len: 0 }
|
2022-08-16 17:48:55 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 19:32:00 +02:00
|
|
|
#[inline]
|
2022-08-16 17:48:55 +02:00
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
// SAFETY: `buf` is only written to by the `fmt::Write::write_str` implementation
|
|
|
|
// which writes a valid UTF-8 string to `buf` and correctly sets `len`.
|
2022-08-16 18:12:06 +02:00
|
|
|
unsafe {
|
|
|
|
let s = MaybeUninit::slice_assume_init_ref(&self.buf[..self.len]);
|
|
|
|
str::from_utf8_unchecked(s)
|
|
|
|
}
|
2022-08-16 17:48:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 20:23:32 +02:00
|
|
|
impl<const SIZE: usize> fmt::Write for DisplayBuffer<SIZE> {
|
2022-08-16 17:48:55 +02:00
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
2022-08-16 18:12:06 +02:00
|
|
|
let bytes = s.as_bytes();
|
|
|
|
|
|
|
|
if let Some(buf) = self.buf.get_mut(self.len..(self.len + bytes.len())) {
|
|
|
|
MaybeUninit::write_slice(buf, bytes);
|
|
|
|
self.len += bytes.len();
|
2022-08-16 17:48:55 +02:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(fmt::Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|