|
| 1 | +use bytes::BufMut; |
| 2 | +use std::ops::Deref; |
| 3 | +use std::io; |
| 4 | + |
| 5 | +/// Safe wrapper around Vec<u8> with custom `bytes::BufMut` and `std::io::Write` |
| 6 | +/// implementations that ensure the buffer never exceeds maximum capacity. |
| 7 | +pub struct CappedBuffer { |
| 8 | + buf: Vec<u8>, |
| 9 | + max: usize, |
| 10 | +} |
| 11 | + |
| 12 | +impl CappedBuffer { |
| 13 | + /// Create a new `CappedBuffer` with initial `capacity`, and a limit |
| 14 | + /// capacity set to `max`. |
| 15 | + pub fn new(mut capacity: usize, max: usize) -> Self { |
| 16 | + if capacity > max { |
| 17 | + capacity = max; |
| 18 | + } |
| 19 | + |
| 20 | + Self { |
| 21 | + buf: Vec::with_capacity(capacity), |
| 22 | + max, |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /// Remaining amount of bytes that can be written to the buffer |
| 27 | + /// before reaching max capacity |
| 28 | + #[inline] |
| 29 | + pub fn remaining(&self) -> usize { |
| 30 | + self.max - self.buf.len() |
| 31 | + } |
| 32 | + |
| 33 | + /// Shift the content of the buffer to the left by `shift`, |
| 34 | + /// effectively forgetting the shifted out bytes. |
| 35 | + /// New length of the buffer will be adjusted accordingly. |
| 36 | + pub fn shift(&mut self, shift: usize) { |
| 37 | + if shift >= self.buf.len() { |
| 38 | + self.buf.clear(); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + let src = self.buf[shift..].as_ptr(); |
| 43 | + let dst = self.buf.as_mut_ptr(); |
| 44 | + let new_len = self.buf.len() - shift; |
| 45 | + |
| 46 | + // This is a simple, potentially overlapping memcpy within |
| 47 | + // the buffer, shifting `new_len` bytes at offset `shift` (`src`) |
| 48 | + // to the beginning of the buffer (`dst`) |
| 49 | + unsafe { |
| 50 | + std::ptr::copy(src, dst, new_len); |
| 51 | + self.buf.set_len(new_len); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl AsRef<[u8]> for CappedBuffer { |
| 57 | + fn as_ref(&self) -> &[u8] { |
| 58 | + &self.buf |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl AsMut<[u8]> for CappedBuffer { |
| 63 | + fn as_mut(&mut self) -> &mut [u8] { |
| 64 | + &mut self.buf |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl Deref for CappedBuffer { |
| 69 | + type Target = Vec<u8>; |
| 70 | + |
| 71 | + fn deref(&self) -> &Vec<u8> { |
| 72 | + &self.buf |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl io::Write for CappedBuffer { |
| 77 | + fn write(&mut self, mut buf: &[u8]) -> io::Result<usize> { |
| 78 | + if buf.len() > self.remaining() { |
| 79 | + buf = &buf[..self.remaining()]; |
| 80 | + } |
| 81 | + self.buf.extend_from_slice(buf); |
| 82 | + Ok(buf.len()) |
| 83 | + } |
| 84 | + |
| 85 | + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { |
| 86 | + if buf.len() <= self.remaining() { |
| 87 | + self.buf.extend_from_slice(buf); |
| 88 | + Ok(()) |
| 89 | + } else { |
| 90 | + Err(io::Error::new(io::ErrorKind::InvalidInput, "Exceeded maximum buffer capacity")) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + fn flush(&mut self) -> io::Result<()> { |
| 95 | + self.buf.flush() |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl BufMut for CappedBuffer { |
| 100 | + fn remaining_mut(&self) -> usize { |
| 101 | + self.remaining() |
| 102 | + } |
| 103 | + |
| 104 | + unsafe fn advance_mut(&mut self, cnt: usize) { |
| 105 | + assert!(cnt <= self.remaining(), "Exceeded buffer capacity"); |
| 106 | + |
| 107 | + self.buf.advance_mut(cnt); |
| 108 | + } |
| 109 | + |
| 110 | + unsafe fn bytes_mut(&mut self) -> &mut [u8] { |
| 111 | + let remaining = self.remaining(); |
| 112 | + |
| 113 | + // `self.buf.bytes_mut` does an implicit allocation |
| 114 | + if remaining == 0 { |
| 115 | + return &mut []; |
| 116 | + } |
| 117 | + |
| 118 | + let mut bytes = self.buf.bytes_mut(); |
| 119 | + |
| 120 | + if bytes.len() > remaining { |
| 121 | + bytes = &mut bytes[..remaining]; |
| 122 | + } |
| 123 | + |
| 124 | + bytes |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(test)] |
| 129 | +mod test { |
| 130 | + use std::io::Write; |
| 131 | + use super::*; |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn shift() { |
| 135 | + let mut buffer = CappedBuffer::new(10, 20); |
| 136 | + |
| 137 | + buffer.write_all(b"Hello World").unwrap(); |
| 138 | + buffer.shift(6); |
| 139 | + |
| 140 | + assert_eq!(&*buffer, b"World"); |
| 141 | + assert_eq!(buffer.remaining(), 15); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn shift_zero() { |
| 146 | + let mut buffer = CappedBuffer::new(10, 20); |
| 147 | + |
| 148 | + buffer.write_all(b"Hello World").unwrap(); |
| 149 | + buffer.shift(0); |
| 150 | + |
| 151 | + assert_eq!(&*buffer, b"Hello World"); |
| 152 | + assert_eq!(buffer.remaining(), 9); |
| 153 | + } |
| 154 | + |
| 155 | + #[test] |
| 156 | + fn shift_all() { |
| 157 | + let mut buffer = CappedBuffer::new(10, 20); |
| 158 | + |
| 159 | + buffer.write_all(b"Hello World").unwrap(); |
| 160 | + buffer.shift(11); |
| 161 | + |
| 162 | + assert_eq!(&*buffer, b""); |
| 163 | + assert_eq!(buffer.remaining(), 20); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn shift_capacity() { |
| 168 | + let mut buffer = CappedBuffer::new(10, 20); |
| 169 | + |
| 170 | + buffer.write_all(b"Hello World").unwrap(); |
| 171 | + buffer.shift(20); |
| 172 | + |
| 173 | + assert_eq!(&*buffer, b""); |
| 174 | + assert_eq!(buffer.remaining(), 20); |
| 175 | + } |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn shift_over_capacity() { |
| 179 | + let mut buffer = CappedBuffer::new(10, 20); |
| 180 | + |
| 181 | + buffer.write_all(b"Hello World").unwrap(); |
| 182 | + buffer.shift(50); |
| 183 | + |
| 184 | + assert_eq!(&*buffer, b""); |
| 185 | + assert_eq!(buffer.remaining(), 20); |
| 186 | + } |
| 187 | +} |
0 commit comments