1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::io::{Result as IoResult, Write};

pub struct WriteString {
    buf: Vec<u8>,
}

pub struct ChildWriter<'a> {
    parent: &'a mut WriteString,
}

impl WriteString {
    pub fn new() -> Self {
        Self { buf: vec![] }
    }
    pub fn generate_child(&mut self) -> ChildWriter {
        ChildWriter { parent: self }
    }
    pub fn get_string(&self) -> Result<String, ::std::string::FromUtf8Error> {
        String::from_utf8(self.buf.clone())
    }

    pub fn get_string_lossy(&self) -> String {
        String::from_utf8_lossy(&self.buf).to_owned().to_string()
    }
}

impl Write for WriteString {
    fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
        self.buf.extend(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> IoResult<()> {
        Ok(())
    }
}

impl<'a> Write for ChildWriter<'a> {
    fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
        self.parent.write(buf)
    }

    fn flush(&mut self) -> IoResult<()> {
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn write_test() {
        let mut w = WriteString::new();
        for _ in 0..100 {
            let ct = w.write(b"asdf").unwrap();
            assert_eq!(ct, 4);
        }
        let s = w.get_string().unwrap();
        assert_eq!(s, "asdf".repeat(100));
    }
}