| 1 |
use core::cmp; |
| 2 |
use core::fmt::{self, Write}; |
| 3 |
use core::mem; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
pub(crate) struct WriteHelper<'a>(&'a mut [u8]); |
| 10 |
|
| 11 |
impl<'a> WriteHelper<'a> { |
| 12 |
pub(crate) fn new(inner: &'a mut [u8]) -> Self { |
| 13 |
Self(inner) |
| 14 |
} |
| 15 |
|
| 16 |
pub(crate) fn into_raw(self) -> &'a mut [u8] { |
| 17 |
self.0 |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl<'a> Write for WriteHelper<'a> { |
| 22 |
fn write_str(&mut self, data: &str) -> fmt::Result { |
| 23 |
let amt = cmp::min(data.len(), self.0.len()); |
| 24 |
let (a, b) = mem::replace(&mut self.0, &mut []).split_at_mut(amt); |
| 25 |
a.copy_from_slice(&data[..amt].as_bytes()); |
| 26 |
self.0 = b; |
| 27 |
Ok(()) |
| 28 |
} |
| 29 |
} |