core/net/
display_buffer.rsuse crate::mem::MaybeUninit;
use crate::{fmt, str};
pub struct DisplayBuffer<const SIZE: usize> {
buf: [MaybeUninit<u8>; SIZE],
len: usize,
}
impl<const SIZE: usize> DisplayBuffer<SIZE> {
#[inline]
pub const fn new() -> Self {
Self { buf: [MaybeUninit::uninit(); SIZE], len: 0 }
}
#[inline]
pub fn as_str(&self) -> &str {
unsafe {
let s = MaybeUninit::slice_assume_init_ref(&self.buf[..self.len]);
str::from_utf8_unchecked(s)
}
}
}
impl<const SIZE: usize> fmt::Write for DisplayBuffer<SIZE> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let bytes = s.as_bytes();
if let Some(buf) = self.buf.get_mut(self.len..(self.len + bytes.len())) {
MaybeUninit::copy_from_slice(buf, bytes);
self.len += bytes.len();
Ok(())
} else {
Err(fmt::Error)
}
}
}