| 1 | use std::{cmp, mem::MaybeUninit}; |
| 2 | |
| 3 | use self::impl_::RawBuf; |
| 4 | |
| 5 | pub struct StdBuf { |
| 6 | buf: RawBuf, |
| 7 | pos: usize, |
| 8 | end: usize, |
| 9 | } |
| 10 | |
| 11 | impl StdBuf { |
| 12 | pub fn with_capacity(cap: usize) -> Self { |
| 13 | StdBuf { |
| 14 | buf: RawBuf::with_capacity(cap), |
| 15 | pos: 0, |
| 16 | end: 0, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | pub fn capacity(&self) -> usize { |
| 21 | self.buf.capacity() |
| 22 | } |
| 23 | |
| 24 | pub fn len(&self) -> usize { |
| 25 | self.end - self.pos |
| 26 | } |
| 27 | |
| 28 | pub fn usable_space(&self) -> usize { |
| 29 | self.capacity() - self.end |
| 30 | } |
| 31 | |
| 32 | pub fn reserve(&mut self, additional: usize) -> bool { |
| 33 | self.check_cursors(); |
| 34 | let usable_space = self.usable_space(); |
| 35 | |
| 36 | // there's already enough space |
| 37 | if usable_space >= additional { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | // attempt to reserve additional capacity in-place |
| 42 | if self.buf.reserve_in_place(additional - usable_space) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // don't copy the contents of the buffer as they're irrelevant now |
| 47 | if self.pos == self.end { |
| 48 | let capacity = self.buf.capacity(); |
| 49 | // free the existing memory |
| 50 | self.buf = RawBuf::with_capacity(0); |
| 51 | self.buf = RawBuf::with_capacity(capacity + additional); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | self.buf.reserve(additional - usable_space) |
| 56 | } |
| 57 | |
| 58 | pub fn make_room(&mut self) { |
| 59 | self.check_cursors(); |
| 60 | |
| 61 | // no room at the head of the buffer |
| 62 | if self.pos == 0 { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // simply move the bytes down to the beginning |
| 67 | let len = self.len(); |
| 68 | |
| 69 | let buf_slice = unsafe { self.buf.as_mut_slice() }; |
| 70 | buf_slice.copy_within(self.pos..self.pos + len, 0); |
| 71 | |
| 72 | self.pos = 0; |
| 73 | self.end = len; |
| 74 | } |
| 75 | |
| 76 | #[inline] |
| 77 | pub fn buf(&self) -> &[u8] { |
| 78 | unsafe { &self.buf.as_slice()[self.pos..self.end] } |
| 79 | } |
| 80 | |
| 81 | pub fn buf_mut(&mut self) -> &mut [u8] { |
| 82 | unsafe { &mut self.buf.as_mut_slice()[self.pos..self.end] } |
| 83 | } |
| 84 | |
| 85 | pub unsafe fn write_buf(&mut self) -> &mut [MaybeUninit<u8>] { |
| 86 | let slice: &mut [u8] = &mut self.buf.as_mut_slice()[self.end..]; |
| 87 | // SAFETY: &[T] and &[MaybeUninit<T>] have the same layout |
| 88 | std::mem::transmute(slice) |
| 89 | } |
| 90 | |
| 91 | pub unsafe fn bytes_written(&mut self, amt: usize) { |
| 92 | self.end = cmp::min(self.end + amt, self.capacity()); |
| 93 | } |
| 94 | |
| 95 | pub fn consume(&mut self, amt: usize) { |
| 96 | self.pos = cmp::min(self.pos + amt, self.end); |
| 97 | self.check_cursors(); |
| 98 | } |
| 99 | |
| 100 | pub fn check_cursors(&mut self) -> bool { |
| 101 | if self.pos == self.end { |
| 102 | self.pos = 0; |
| 103 | self.end = 0; |
| 104 | true |
| 105 | } else { |
| 106 | false |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | mod impl_ { |
| 112 | use std::mem; |
| 113 | |
| 114 | pub struct RawBuf { |
| 115 | buf: Box<[u8]>, |
| 116 | } |
| 117 | |
| 118 | impl RawBuf { |
| 119 | pub fn with_capacity(capacity: usize) -> Self { |
| 120 | let mut buf = Vec::with_capacity(capacity); |
| 121 | let true_cap = buf.capacity(); |
| 122 | |
| 123 | unsafe { |
| 124 | buf.set_len(true_cap); |
| 125 | } |
| 126 | |
| 127 | RawBuf { |
| 128 | buf: buf.into_boxed_slice(), |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | pub fn capacity(&self) -> usize { |
| 133 | self.buf.len() |
| 134 | } |
| 135 | |
| 136 | pub fn reserve(&mut self, additional: usize) -> bool { |
| 137 | let mut buf = mem::replace(&mut self.buf, Box::new([])).into_vec(); |
| 138 | |
| 139 | let old_ptr = self.buf.as_ptr(); |
| 140 | |
| 141 | buf.reserve_exact(additional); |
| 142 | |
| 143 | unsafe { |
| 144 | let new_cap = buf.capacity(); |
| 145 | buf.set_len(new_cap); |
| 146 | } |
| 147 | |
| 148 | self.buf = buf.into_boxed_slice(); |
| 149 | |
| 150 | old_ptr == self.buf.as_ptr() |
| 151 | } |
| 152 | |
| 153 | pub fn reserve_in_place(&mut self, _additional: usize) -> bool { |
| 154 | // `Vec` does not support this |
| 155 | false |
| 156 | } |
| 157 | |
| 158 | #[inline] |
| 159 | pub unsafe fn as_slice(&self) -> &[u8] { |
| 160 | &self.buf |
| 161 | } |
| 162 | |
| 163 | pub unsafe fn as_mut_slice(&mut self) -> &mut [u8] { |
| 164 | &mut self.buf |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn read_into_full() { |
| 171 | use crate::Buffer; |
| 172 | |
| 173 | let mut buffer = Buffer::with_capacity(1); |
| 174 | |
| 175 | assert_eq!(buffer.capacity(), 1); |
| 176 | |
| 177 | let mut bytes = &[1u8, 2][..]; |
| 178 | |
| 179 | // Result<usize, io::Error> does not impl PartialEq |
| 180 | assert_eq!(buffer.read_from(&mut bytes).unwrap(), 1); |
| 181 | assert_eq!(buffer.read_from(&mut bytes).unwrap(), 0); |
| 182 | } |