cppStream 0.1.1

a wrapper brings `<<` and `>>` operator to rust's writer/reader/stream like what C++ did
l#![allow(unused_must_use)]
use std::fmt::Show;
use std::cell::RefCell;
use std::rc::Rc;
use std::ops::Shl;
use std::io::{ByRefWriter,RefWriter};
use std::io::LineBufferedWriter;
use std::io::stdio::StdWriter;
use std::io::stdout;

pub struct OStream<W> where W: Writer {
    ostream: Rc<RefCell<W>>,
}

impl<W> OStream<W> where W: Writer {
    pub fn new(writer: W) -> OStream<W> {
        OStream {
            ostream: Rc::new(RefCell::new(writer))
        }
    }
}

pub trait ToOStream<'a> where Self: ByRefWriter + Writer + Sized {
    fn to_ostream(&'a mut self) -> OStream<RefWriter<'a,Self>> {
        OStream::new(self.by_ref())
    }
}

impl<'a,B> ToOStream<'a> for B where B: ByRefWriter + Writer {}

pub trait AsOStream where Self: Writer + Sized {
    fn as_ostream(self) -> OStream<Self> {
        OStream::new(self)
    }
}

impl<W> AsOStream for W where W: Writer {}

impl<W,T> Shl<T> for OStream<W> where W: Writer, T: Show {
    type Output = OStream<W>;
    fn shl(self, output: T) -> OStream<W> {
        write!(self.ostream.borrow_mut(), "{}", output);
        self.ostream.borrow_mut().flush();
        OStream {
            ostream: self.ostream.clone()
        }
    }
}

impl<W> Clone for OStream<W> where W: Writer {
    fn clone(&self) -> OStream<W> {
        OStream {
            ostream: self.ostream.clone()
        }
    }
}

#[allow(non_upper_case_globals)]
pub const endl: char = '\n';

pub fn cout() -> OStream<LineBufferedWriter<StdWriter>> {
    OStream::new(stdout())
}

pub fn ostream<R>(writer: R) -> OStream<R> where R: Writer {
    OStream::new(writer)
}

#[test]
fn test_out() {
    let mut vec = Vec::with_capacity(100);
    {
        let vecout = vec.to_ostream();
        vecout << 1i << 2u << 3f64 << "good C++" << endl;
    }
    
    assert_eq!(vec, b"123good C++\n");
}

#[test]
fn test_stdout() {
    use std;
    let cout = std::io::stdout().as_ostream();
    cout << 1i << 2u << 3f64 << endl;
}