tracing_stackdriver/
writer.rs

1use std::{
2    fmt::{Formatter, Write},
3    io,
4};
5
6/// Utility newtype for converting between fmt::Write and io::Write
7// https://docs.rs/tracing-subscriber/latest/src/tracing_subscriber/fmt/writer.rs.html
8pub(crate) struct WriteAdaptor<'a> {
9    fmt_write: &'a mut dyn Write,
10}
11
12impl<'a> WriteAdaptor<'a> {
13    pub(crate) fn new(fmt_write: &'a mut dyn Write) -> Self {
14        Self { fmt_write }
15    }
16}
17
18impl<'a> io::Write for WriteAdaptor<'a> {
19    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
20        let s =
21            std::str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
22
23        self.fmt_write
24            .write_str(s)
25            .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
26
27        Ok(s.as_bytes().len())
28    }
29
30    fn flush(&mut self) -> io::Result<()> {
31        Ok(())
32    }
33}
34
35impl<'a> std::fmt::Debug for WriteAdaptor<'a> {
36    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
37        formatter.pad("WriteAdaptor { .. }")
38    }
39}