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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use crate::stream::{Stream, StreamError};

///Reader for inmemory stream
pub struct Reader<'a> {
    stream: &'a mut Stream,
}

///Writer for inmemory stream
pub struct Writer<'a> {
    stream: &'a mut Stream,
}

impl<'a> Reader<'a> {
    /// Create new Reader based on stream.
    pub fn new(stream: &mut Stream) -> Reader {
        Reader {
            stream
        }
    }

    /// Peek one byte from stream, position don't shift.
    ///
    /// Can return EOF error.
    pub fn peek_byte(&self) -> Result<u8, StreamError> {
        self.stream.peek_byte()
    }

    /// Peek bytes from stream, position don't shift.
    ///
    /// Can return EOF, ZeroLength errors.
    pub fn peek(&self, buffer: &mut [u8]) -> Result<usize, StreamError> {
        self.stream.peek(buffer)
    }

    /// Read one byte from stream, position  shifted.
    ///
    /// Can return EOF error.
    pub fn read_byte(&mut self) -> Result<u8, StreamError> {
        self.stream.read_byte()
    }

    /// Read bytes from stream, position shifted.
    ///
    /// Can return EOF, ZeroLength errors.
    pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, StreamError> {
        self.stream.read(buffer)
    }

    /// Skip "count" bytes in stream. Return skipped bytes count.
    pub fn skip(&mut self, count: usize) -> usize {
        self.stream.skip(count)
    }

    /// Skip all bytes in stream. Return skipped bytes count.
    pub fn skip_all(&mut self) -> usize {
        self.stream.skip_all()
    }

    /// View of available bytes in stream.
    pub fn view(&self) -> &[u8] {
        self.stream.view()
    }

    /// Return available to read bytes.
    pub fn available(&self) -> usize {
        self.stream.available()
    }

    /// Clear bytes that have been read. Has no effect on the allocated capacity.
    pub fn truncate_readied(&mut self) {
        self.stream.truncate_readied();
    }

    /// Truncate the capacity of the stream as much as possible.
    pub fn truncate_capacity(&mut self) {
        self.stream.truncate_capacity();
    }
}

impl<'a> Writer<'a> {
    /// Create new Writer based on stream.
    pub fn new(stream: &mut Stream) -> Writer {
        Writer {
            stream
        }
    }

    /// Write one byte to stream.
    pub fn write_byte(&mut self, byte: u8) {
        self.stream.write_byte(byte);
    }

    /// Write bytes to stream.
    pub fn write(&mut self, buffer: &[u8]) -> usize {
        self.stream.write(buffer)
    }
}