restd 0.3.0

A re-implementation of various std features
Documentation
use crate::{fmt, io};

/// An adapter from [`io::Write`] to [`fmt::Write`].
///
/// Once an error is encountered, refuses to write again until `self.error` is
/// cleared. Flushes on every write.
pub struct IoFmt<W> {
    /// The underlying writer.
    pub writer: W,
    /// The number of bytes written.
    pub written: usize,
    /// The last error encountered, if any.
    ///
    /// While this is `Some`, all writes will error and return before even
    /// attempting to write.
    pub error: Option<io::Error>,
}

impl<W> IoFmt<W> {
    /// Creates a new `IoFmt` with 0 bytes written and no error.
    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(())
    }
}