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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::io::{Read as IoRead, Result as IoResult, Error as IoError, ErrorKind as IoErrorKind};
use ::Result;
use serde_crate as serde;

/// A byte-oriented reading trait that is specialized for
/// slices and generic readers.
pub trait BincodeRead<'storage>: IoRead {
    #[doc(hidden)]
    fn forward_read_str<V>(&mut self, length: usize, visitor: V) ->  Result<V::Value>
    where V: serde::de::Visitor<'storage>;

    #[doc(hidden)]
    fn get_byte_buffer(&mut self, length: usize) -> Result<Vec<u8>>;

    #[doc(hidden)]
    fn forward_read_bytes<V>(&mut self, length: usize, visitor: V) ->  Result<V::Value>
    where V: serde::de::Visitor<'storage>;
}

/// A BincodeRead implementation for byte slices
pub struct SliceReader<'storage> {
    slice: &'storage [u8]
}

/// A BincodeRead implementation for io::Readers
pub struct IoReadReader<R> {
    reader: R,
    temp_buffer: Vec<u8>,
}

impl <'storage> SliceReader<'storage> {
    /// Constructs a slice reader
    pub fn new(bytes: &'storage [u8]) -> SliceReader<'storage> {
        SliceReader {
            slice: bytes,
        }
    }
}

impl <R> IoReadReader<R> {
    /// Constructs an IoReadReader
    pub fn new(r: R) -> IoReadReader<R> {
        IoReadReader {
            reader: r,
            temp_buffer: vec![],
        }
    }
}

impl <'storage> IoRead for SliceReader<'storage> {
    fn read(&mut self, out: & mut [u8]) -> IoResult<usize> {
        (&mut self.slice).read(out)
    }
}

impl <R: IoRead> IoRead for IoReadReader<R> {
    fn read(&mut self, out: & mut [u8]) -> IoResult<usize> {
        self.reader.read(out)
    }
}

impl <'storage> SliceReader<'storage> {
    fn unexpected_eof() -> Box<::ErrorKind> {
        return Box::new(::ErrorKind::IoError(IoError::new(IoErrorKind::UnexpectedEof, "")));
    }
}

impl <'storage> BincodeRead<'storage> for SliceReader<'storage> {
    fn forward_read_str<V>(&mut self, length: usize, visitor: V) ->  Result<V::Value>
    where V: serde::de::Visitor<'storage> {
        use ::ErrorKind;
        if length > self.slice.len() {
            return Err(SliceReader::unexpected_eof());
        }

        let string = match ::std::str::from_utf8(&self.slice[..length]) {
            Ok(s) => s,
            Err(_) => return Err(Box::new(ErrorKind::InvalidEncoding {
                desc: "string was not valid utf8",
                detail: None,
            })),
        };
        let r = visitor.visit_borrowed_str(string);
        self.slice = &self.slice[length..];
        r
    }

    fn get_byte_buffer(&mut self, length: usize) -> Result<Vec<u8>> {
        if length > self.slice.len() {
            return Err(SliceReader::unexpected_eof());
        }

        let r = &self.slice[..length];
        self.slice = &self.slice[length..];
        Ok(r.to_vec())
    }

    fn forward_read_bytes<V>(&mut self, length: usize, visitor: V) ->  Result<V::Value>
    where V: serde::de::Visitor<'storage> {
        if length > self.slice.len() {
            return Err(SliceReader::unexpected_eof());
        }

        let r = visitor.visit_borrowed_bytes(&self.slice[..length]);
        self.slice = &self.slice[length..];
        r
    }
}

impl <R> IoReadReader<R> where R: IoRead {
    fn fill_buffer(&mut self, length: usize) -> Result<()> {
        let current_length = self.temp_buffer.len();
        if length > current_length{
            self.temp_buffer.reserve_exact(length - current_length);
            unsafe { self.temp_buffer.set_len(length); }
        }

        self.reader.read_exact(&mut self.temp_buffer[..length])?;
        Ok(())
    }
}

impl <R> BincodeRead<'static> for IoReadReader<R> where R: IoRead {
    fn forward_read_str<V>(&mut self, length: usize, visitor: V) ->  Result<V::Value>
    where V: serde::de::Visitor<'static> {
        self.fill_buffer(length)?;

        let string = match ::std::str::from_utf8(&self.temp_buffer[..length]) {
            Ok(s) => s,
            Err(_) => return Err(Box::new(::ErrorKind::InvalidEncoding {
                desc: "string was not valid utf8",
                detail: None,
            })),
        };

        let r = visitor.visit_str(string);
        r
    }

    fn get_byte_buffer(&mut self, length: usize) -> Result<Vec<u8>> {
        self.fill_buffer(length)?;
        Ok(self.temp_buffer[..length].to_vec())
    }

    fn forward_read_bytes<V>(&mut self, length: usize, visitor: V) ->  Result<V::Value>
    where V: serde::de::Visitor<'static> {
        self.fill_buffer(length)?;
        let r = visitor.visit_bytes(&self.temp_buffer[..length]);
        r
    }
}