use crate::{fmt, io};
pub struct IoFmt<W> {
pub writer: W,
pub written: usize,
pub error: Option<io::Error>,
}
impl<W> IoFmt<W> {
pub fn new(writer: W) -> Self {
Self { writer, written: 0, error: None }
}
}
impl<W: io::Write> fmt::Write for IoFmt<W> {
fn write_str(&mut self, data: &str) -> fmt::Result {
if self.error.is_some() {
return Err(fmt::Error);
}
if let Err(e) = self.writer
.write(data.as_bytes())
.and_then(|written| {
self.written += written;
self.writer.flush()
})
{
self.error = Some(e);
}
Ok(())
}
}