1use std::fmt;
2use std::io;
3
4pub(crate) struct UTF8Writer<W>(W);
5
6impl<W> UTF8Writer<W> {
7 pub(crate) fn new(writer: W) -> Self {
8 Self(writer)
9 }
10}
11
12impl<W> fmt::Write for UTF8Writer<W>
13where
14 W: io::Write,
15{
16 fn write_str(&mut self, s: &str) -> fmt::Result {
17 let mut buf: &[u8] = s.as_bytes();
18 loop {
19 let n: usize = self.0.write(buf).map_err(|_| fmt::Error)?;
20 if n == buf.len() {
21 break;
22 }
23
24 buf = &buf[n..];
25 }
26
27 Ok(())
28 }
29}
30