| 1 | use std::io; |
| 2 | use std::fmt; |
| 3 | use std::cmp; |
| 4 | |
| 5 | use std::io::{Error, ErrorKind}; |
| 6 | |
| 7 | use super::*; |
| 8 | |
| 9 | /// Wraps a memory buffer. |
| 10 | /// |
| 11 | /// Although it is possible to use `Generic` to wrap a |
| 12 | /// buffer, this implementation is optimized for a memory buffer, and |
| 13 | /// avoids double buffering. |
| 14 | #[derive(Debug)] |
| 15 | pub struct Memory<'a, C: fmt::Debug + Sync + Send> { |
| 16 | buffer: &'a [u8], |
| 17 | // The next byte to read in the buffer. |
| 18 | cursor: usize, |
| 19 | |
| 20 | // The user settable cookie. |
| 21 | cookie: C, |
| 22 | } |
| 23 | |
| 24 | assert_send_and_sync!(Memory<'_, C> |
| 25 | where C: fmt::Debug); |
| 26 | |
| 27 | impl<'a, C: fmt::Debug + Sync + Send> fmt::Display for Memory<'a, C> { |
| 28 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 29 | write!(f, "Memory ({} of {} bytes read)", |
| 30 | self.cursor, self.buffer.len()) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl<'a> Memory<'a, ()> { |
| 35 | /// Instantiates a new `Memory`. |
| 36 | /// |
| 37 | /// `buffer` contains the `Memory`'s contents. |
| 38 | pub fn new(buffer: &'a [u8]) -> Self { |
| 39 | Self::with_cookie(buffer, ()) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl<'a, C: fmt::Debug + Sync + Send> Memory<'a, C> { |
| 44 | /// Like [`Self::new`], but sets a cookie. |
| 45 | /// |
| 46 | /// The cookie can be retrieved using the [`BufferedReader::cookie_ref`] and |
| 47 | /// [`BufferedReader::cookie_mut`] methods, and set using the [`BufferedReader::cookie_set`] method. |
| 48 | pub fn with_cookie(buffer: &'a [u8], cookie: C) -> Self { |
| 49 | Memory { |
| 50 | buffer, |
| 51 | cursor: 0, |
| 52 | cookie, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// Returns the number of bytes that have been consumed by this |
| 57 | /// reader. |
| 58 | pub fn total_out(&self) -> usize { |
| 59 | self.cursor |
| 60 | } |
| 61 | |
| 62 | #[allow(dead_code)] |
| 63 | pub(crate) fn source_buffer(&self) -> &[u8] { |
| 64 | self.buffer |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl<'a, C: fmt::Debug + Sync + Send> io::Read for Memory<'a, C> { |
| 69 | fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { |
| 70 | let amount = cmp::min(buf.len(), self.buffer.len() - self.cursor); |
| 71 | buf[0..amount].copy_from_slice( |
| 72 | &self.buffer[self.cursor..self.cursor+amount]); |
| 73 | self.consume(amount); |
| 74 | Ok(amount) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl<'a, C: fmt::Debug + Sync + Send> BufferedReader<C> for Memory<'a, C> { |
| 79 | fn buffer(&self) -> &[u8] { |
| 80 | &self.buffer[self.cursor..] |
| 81 | } |
| 82 | |
| 83 | fn data(&mut self, _amount: usize) -> Result<&[u8], io::Error> { |
| 84 | assert!(self.cursor <= self.buffer.len()); |
| 85 | Ok(&self.buffer[self.cursor..]) |
| 86 | } |
| 87 | |
| 88 | fn consume(&mut self, amount: usize) -> &[u8] { |
| 89 | // The caller can't consume more than is buffered! |
| 90 | assert!(amount <= self.buffer.len() - self.cursor, |
| 91 | "Attempt to consume {} bytes, but buffer only has {} bytes!", |
| 92 | amount, self.buffer.len() - self.cursor); |
| 93 | self.cursor += amount; |
| 94 | assert!(self.cursor <= self.buffer.len()); |
| 95 | &self.buffer[self.cursor - amount..] |
| 96 | } |
| 97 | |
| 98 | fn data_consume(&mut self, amount: usize) -> Result<&[u8], io::Error> { |
| 99 | let amount = cmp::min(amount, self.buffer.len() - self.cursor); |
| 100 | return Ok(self.consume(amount)); |
| 101 | } |
| 102 | |
| 103 | fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], io::Error> { |
| 104 | if self.buffer.len() - self.cursor < amount { |
| 105 | return Err(Error::new(ErrorKind::UnexpectedEof, "EOF")); |
| 106 | } |
| 107 | return Ok(self.consume(amount)); |
| 108 | } |
| 109 | |
| 110 | fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<C>> { |
| 111 | None |
| 112 | } |
| 113 | |
| 114 | fn get_ref(&self) -> Option<&dyn BufferedReader<C>> { |
| 115 | None |
| 116 | } |
| 117 | |
| 118 | fn into_inner<'b>(self: Box<Self>) -> Option<Box<dyn BufferedReader<C> + 'b>> |
| 119 | where Self: 'b { |
| 120 | None |
| 121 | } |
| 122 | |
| 123 | fn cookie_set(&mut self, cookie: C) -> C { |
| 124 | use std::mem; |
| 125 | |
| 126 | mem::replace(&mut self.cookie, cookie) |
| 127 | } |
| 128 | |
| 129 | fn cookie_ref(&self) -> &C { |
| 130 | &self.cookie |
| 131 | } |
| 132 | |
| 133 | fn cookie_mut(&mut self) -> &mut C { |
| 134 | &mut self.cookie |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #[cfg(test)] |
| 139 | mod test { |
| 140 | use super::*; |
| 141 | #[test] |
| 142 | fn buffered_reader_memory_test () { |
| 143 | let mut bio = Memory::new(crate::BUFFERED_READER_TEST_DATA); |
| 144 | |
| 145 | buffered_reader_test_data_check(&mut bio); |
| 146 | } |
| 147 | |
| 148 | // Test that buffer() returns the same data as data(). |
| 149 | #[test] |
| 150 | fn buffer_test() { |
| 151 | // Test vector. A Memory returns all unconsumed |
| 152 | // data. So, use a relatively small buffer size. |
| 153 | let size = default_buf_size(); |
| 154 | let mut input = Vec::with_capacity(size); |
| 155 | let mut v = 0u8; |
| 156 | for _ in 0..size { |
| 157 | input.push(v); |
| 158 | if v == std::u8::MAX { |
| 159 | v = 0; |
| 160 | } else { |
| 161 | v += 1; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | let mut reader = Memory::new(&input[..]); |
| 166 | |
| 167 | for i in 0..input.len() { |
| 168 | let data = reader.data(default_buf_size() + 1).unwrap().to_vec(); |
| 169 | assert!(!data.is_empty()); |
| 170 | assert_eq!(data, reader.buffer()); |
| 171 | // And, we may as well check to make sure we read the |
| 172 | // right data. |
| 173 | assert_eq!(data, &input[i..i+data.len()]); |
| 174 | |
| 175 | // Consume one byte and see what happens. |
| 176 | reader.consume(1); |
| 177 | } |
| 178 | } |
| 179 | } |