|
| 1 | +//! Defines types which can be lazily converted into `Bytes` chunks |
| 2 | +
|
| 3 | +use bytes::Bytes; |
| 4 | + |
| 5 | +/// A source of one or more buffers which can be converted into `Bytes` buffers on demand |
| 6 | +/// |
| 7 | +/// The purpose of this data type is to defer conversion as long as possible, |
| 8 | +/// so that no heap allocation is required in case no data is writable. |
| 9 | +pub trait BytesSource { |
| 10 | + /// Returns the next chunk from the source of owned chunks. |
| 11 | + /// |
| 12 | + /// This method will consume parts of the source. |
| 13 | + /// Calling it will yield `Bytes` elements up to the configured `limit`. |
| 14 | + /// |
| 15 | + /// The method returns a tuple: |
| 16 | + /// - The first item is the yielded `Bytes` element. The element will be |
| 17 | + /// empty if the limit is zero or no more data is available. |
| 18 | + /// - The second item returns how many complete chunks inside the source had |
| 19 | + /// had been consumed. This can be less than 1, if a chunk inside the |
| 20 | + /// source had been truncated in order to adhere to the limit. It can also |
| 21 | + /// be more than 1, if zero-length chunks had been skipped. |
| 22 | + fn pop_chunk(&mut self, limit: usize) -> (Bytes, usize); |
| 23 | +} |
| 24 | + |
| 25 | +/// Indicates how many bytes and chunks had been transferred in a write operation |
| 26 | +#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)] |
| 27 | +pub struct Written { |
| 28 | + /// The amount of bytes which had been written |
| 29 | + pub bytes: usize, |
| 30 | + /// The amount of full chunks which had been written |
| 31 | + /// |
| 32 | + /// If a chunk was only partially written, it will not be counted by this field. |
| 33 | + pub chunks: usize, |
| 34 | +} |
| 35 | + |
| 36 | +/// A [`BytesSource`] implementation for `&'a mut [Bytes]` |
| 37 | +/// |
| 38 | +/// The type allows to dequeue [`Bytes`] chunks from an array of chunks, up to |
| 39 | +/// a configured limit. |
| 40 | +pub struct BytesArray<'a> { |
| 41 | + /// The wrapped slice of `Bytes` |
| 42 | + chunks: &'a mut [Bytes], |
| 43 | + /// The amount of chunks consumed from this source |
| 44 | + consumed: usize, |
| 45 | +} |
| 46 | + |
| 47 | +impl<'a> BytesArray<'a> { |
| 48 | + pub fn from_chunks(chunks: &'a mut [Bytes]) -> Self { |
| 49 | + Self { |
| 50 | + chunks, |
| 51 | + consumed: 0, |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<'a> BytesSource for BytesArray<'a> { |
| 57 | + fn pop_chunk(&mut self, limit: usize) -> (Bytes, usize) { |
| 58 | + // The loop exists to skip empty chunks while still marking them as |
| 59 | + // consumed |
| 60 | + let mut chunks_consumed = 0; |
| 61 | + |
| 62 | + while self.consumed < self.chunks.len() { |
| 63 | + let chunk = &mut self.chunks[self.consumed]; |
| 64 | + |
| 65 | + if chunk.len() <= limit { |
| 66 | + let chunk = std::mem::take(chunk); |
| 67 | + self.consumed += 1; |
| 68 | + chunks_consumed += 1; |
| 69 | + if chunk.is_empty() { |
| 70 | + continue; |
| 71 | + } |
| 72 | + return (chunk, chunks_consumed); |
| 73 | + } else if limit > 0 { |
| 74 | + let chunk = chunk.split_to(limit); |
| 75 | + return (chunk, chunks_consumed); |
| 76 | + } else { |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + (Bytes::new(), chunks_consumed) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// A [`BytesSource`] implementation for `&[u8]` |
| 86 | +/// |
| 87 | +/// The type allows to dequeue a single [`Bytes`] chunk, which will be lazily |
| 88 | +/// created from a reference. This allows to defer the allocation until it is |
| 89 | +/// known how much data needs to be copied. |
| 90 | +pub struct ByteSlice<'a> { |
| 91 | + /// The wrapped byte slice |
| 92 | + data: &'a [u8], |
| 93 | +} |
| 94 | + |
| 95 | +impl<'a> ByteSlice<'a> { |
| 96 | + pub fn from_slice(data: &'a [u8]) -> Self { |
| 97 | + Self { data } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl<'a> BytesSource for ByteSlice<'a> { |
| 102 | + fn pop_chunk(&mut self, limit: usize) -> (Bytes, usize) { |
| 103 | + let limit = limit.min(self.data.len()); |
| 104 | + if limit == 0 { |
| 105 | + return (Bytes::new(), 0); |
| 106 | + } |
| 107 | + |
| 108 | + let chunk = Bytes::from(self.data[..limit].to_owned()); |
| 109 | + self.data = &self.data[chunk.len()..]; |
| 110 | + |
| 111 | + let chunks_consumed = if self.data.is_empty() { 1 } else { 0 }; |
| 112 | + (chunk, chunks_consumed) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(test)] |
| 117 | +mod tests { |
| 118 | + use super::*; |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn bytes_array() { |
| 122 | + let full = b"Hello World 123456789 ABCDEFGHJIJKLMNOPQRSTUVWXYZ".to_owned(); |
| 123 | + for limit in 0..full.len() { |
| 124 | + let mut chunks = [ |
| 125 | + Bytes::from_static(b""), |
| 126 | + Bytes::from_static(b"Hello "), |
| 127 | + Bytes::from_static(b"Wo"), |
| 128 | + Bytes::from_static(b""), |
| 129 | + Bytes::from_static(b"r"), |
| 130 | + Bytes::from_static(b"ld"), |
| 131 | + Bytes::from_static(b""), |
| 132 | + Bytes::from_static(b" 12345678"), |
| 133 | + Bytes::from_static(b"9 ABCDE"), |
| 134 | + Bytes::from_static(b"F"), |
| 135 | + Bytes::from_static(b"GHJIJKLMNOPQRSTUVWXYZ"), |
| 136 | + ]; |
| 137 | + let num_chunks = chunks.len(); |
| 138 | + let last_chunk_len = chunks[chunks.len() - 1].len(); |
| 139 | + |
| 140 | + let mut array = BytesArray::from_chunks(&mut chunks); |
| 141 | + |
| 142 | + let mut buf = Vec::new(); |
| 143 | + let mut chunks_popped = 0; |
| 144 | + let mut chunks_consumed = 0; |
| 145 | + let mut remaining = limit; |
| 146 | + loop { |
| 147 | + let (chunk, consumed) = array.pop_chunk(remaining); |
| 148 | + chunks_consumed += consumed; |
| 149 | + |
| 150 | + if !chunk.is_empty() { |
| 151 | + buf.extend_from_slice(&chunk); |
| 152 | + remaining -= chunk.len(); |
| 153 | + chunks_popped += 1; |
| 154 | + } else { |
| 155 | + break; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + assert_eq!(&buf[..], &full[..limit]); |
| 160 | + |
| 161 | + if limit == full.len() { |
| 162 | + // Full consumption of the last chunk |
| 163 | + assert_eq!(chunks_consumed, num_chunks); |
| 164 | + // Since there are empty chunks, we consume more than there are popped |
| 165 | + assert_eq!(chunks_consumed, chunks_popped + 3); |
| 166 | + } else if limit > full.len() - last_chunk_len { |
| 167 | + // Partial consumption of the last chunk |
| 168 | + assert_eq!(chunks_consumed, num_chunks - 1); |
| 169 | + assert_eq!(chunks_consumed, chunks_popped + 2); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + #[test] |
| 175 | + fn byte_slice() { |
| 176 | + let full = b"Hello World 123456789 ABCDEFGHJIJKLMNOPQRSTUVWXYZ".to_owned(); |
| 177 | + for limit in 0..full.len() { |
| 178 | + let mut array = ByteSlice::from_slice(&full[..]); |
| 179 | + |
| 180 | + let mut buf = Vec::new(); |
| 181 | + let mut chunks_popped = 0; |
| 182 | + let mut chunks_consumed = 0; |
| 183 | + let mut remaining = limit; |
| 184 | + loop { |
| 185 | + let (chunk, consumed) = array.pop_chunk(remaining); |
| 186 | + chunks_consumed += consumed; |
| 187 | + |
| 188 | + if !chunk.is_empty() { |
| 189 | + buf.extend_from_slice(&chunk); |
| 190 | + remaining -= chunk.len(); |
| 191 | + chunks_popped += 1; |
| 192 | + } else { |
| 193 | + break; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + assert_eq!(&buf[..], &full[..limit]); |
| 198 | + if limit != 0 { |
| 199 | + assert_eq!(chunks_popped, 1); |
| 200 | + } else { |
| 201 | + assert_eq!(chunks_popped, 0); |
| 202 | + } |
| 203 | + |
| 204 | + if limit == full.len() { |
| 205 | + assert_eq!(chunks_consumed, 1); |
| 206 | + } else { |
| 207 | + assert_eq!(chunks_consumed, 0); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments