use core::fmt; pub struct FmtWrapper<'a> { buf: &'a mut [u8], offset: usize, } impl<'a> FmtWrapper<'a> { pub fn new(buf: &'a mut [u8]) -> Self { FmtWrapper { buf: buf, offset: 0, } } pub fn contents(&self) -> &[u8] { &self.buf[..self.offset] } } impl<'a> fmt::Write for FmtWrapper<'a> { fn write_str(&mut self, s: &str) -> fmt::Result { let bytes = s.as_bytes(); let remainder = &mut self.buf[self.offset..]; let remainder = &mut remainder[..bytes.len()]; remainder.copy_from_slice(bytes); self.offset += bytes.len(); Ok(()) } }