cppstreams 1.1.0

C++ streams in rust
Documentation
use std::io::Write;

use super::as_write::AsWrite;
use super::{IntoOStream, OStream};

macro_rules! impl_struct {
    ($struct_name:ty, $writer:ty, $create_writer:expr) => {
        impl IntoOStream for &mut $struct_name {
            type Writer = $writer;

            #[inline]
            fn into_ostream(self) -> OStream<$writer> {
                OStream::new($create_writer)
            }
        }

        impl From<$struct_name> for OStream<$writer> {
            #[inline]
            fn from(_: $struct_name) -> Self {
                OStream::new($create_writer)
            }
        }

        impl IntoOStream for $struct_name {
            type Writer = $writer;

            #[inline]
            fn into_ostream(self) -> OStream<$writer> {
                OStream::new($create_writer)
            }
        }

        impl<T: std::fmt::Display> std::ops::Shl<T> for $struct_name {
            type Output = OStream<$writer>;

            #[inline]
            fn shl(self, _rhs: T) -> Self::Output {
                self.into_ostream() << _rhs
            }
        }

        impl AsWrite for $struct_name {
            #[inline]
            fn as_write(&mut self) -> impl Write {
                $create_writer
            }
        }

        impl AsWrite for &mut $struct_name {
            #[inline]
            fn as_write(&mut self) -> impl Write {
                $create_writer
            }
        }
    };
}

pub struct Cout;
impl_struct!(Cout, std::io::StdoutLock<'static>, std::io::stdout().lock());
pub struct Cerr;
impl_struct!(Cerr, std::io::StderrLock<'static>, std::io::stderr().lock());