Debian Codegraph @

src/helper.rs

raw symbols SCIP
1 use core::cmp;
2 use core::fmt::{self, Write};
3 use core::mem;
4
5 /// A partial reimplementation of `impl std::io::Write for &mut [u8]`.
6 ///
7 /// There are probably simpler ways to do this, this was the first thing that
8 /// came to mind.
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 }