1 | use std::fmt; |
---|---|
2 | use std::io; |
3 | |
4 | pub(crate) struct UTF8Writer<W>(W); |
5 | |
6 | impl<W> UTF8Writer<W> { |
7 | pub(crate) fn new(writer: W) -> Self { |
8 | Self(writer) |
9 | } |
10 | } |
11 | |
12 | impl<W> fmt::Write for UTF8Writer<W> |
13 | where |
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 |