| 1 | #![forbid(unsafe_code)] |
| 2 | |
| 3 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::collectionsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/::VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#; |
| 4 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#; |
| 5 | use bytesrust-analyzer cargo bytes 1.12.1 crate/::{Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#, BufMutrust-analyzer cargo bytes 1.12.1 buf/buf_mut/BufMut#, Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#, BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#}; |
| 6 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/; |
| 7 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#; |
| 8 | |
| 9 | #[cfg(feature = "std")] |
| 10 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#; |
| 11 | |
| 12 | #[cfg(feature = "std")] |
| 13 | fn chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/chunks_vectored().<'s, B, I>(bufs: I, dst: &mut [IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#<'s>]) -> usize |
| 14 | where |
| 15 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = &'s B>, |
| 16 | B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf# + 's, |
| 17 | { |
| 18 | let mut filled = 0; |
| 19 | for buf in bufs { |
| 20 | if filled rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().dst.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 21 | break; |
| 22 | } |
| 23 | filled rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().buf.chunks_vectoredrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#chunks_vectored().(&mut dst[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().filled..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().); |
| 24 | } |
| 25 | filled |
| 26 | } |
| 27 | |
| 28 | /// A consumable view of a sequence of buffers. |
| 29 | /// |
| 30 | /// This allows viewing a sequence of buffers as one buffer, without copying the bytes over. Unlike |
| 31 | /// the [SegmentedBuf], this doesn't allow for appending more buffers and doesn't drop the buffers |
| 32 | /// as they are exhausted (though they all get exhausted, no leftovers are kept in them as the |
| 33 | /// caller advances through it). On the other hand, it doesn't require an internal allocation in |
| 34 | /// the form of VecDeque and can be based on any kind of slice. |
| 35 | /// |
| 36 | /// # Example |
| 37 | /// |
| 38 | /// ```rust |
| 39 | /// # use bytes_utils::SegmentedSlice; |
| 40 | /// # use bytes::Buf; |
| 41 | /// # use std::io::Read; |
| 42 | /// let mut buffers = [b"Hello" as &[_], b"", b" ", b"", b"World"]; |
| 43 | /// let buf = SegmentedSlice::new(&mut buffers); |
| 44 | /// |
| 45 | /// assert_eq!(11, buf.remaining()); |
| 46 | /// assert_eq!(b"Hello", buf.chunk()); |
| 47 | /// |
| 48 | /// let mut out = String::new(); |
| 49 | /// buf.reader().read_to_string(&mut out).expect("Doesn't cause IO errors"); |
| 50 | /// assert_eq!("Hello World", out); |
| 51 | /// ``` |
| 52 | /// |
| 53 | /// # Optimizations |
| 54 | /// |
| 55 | /// The [copy_to_bytes][SegmentedSlice::copy_to_bytes] method tries to avoid copies by delegating |
| 56 | /// into the underlying buffer if possible (if the whole request can be fulfilled using only a |
| 57 | /// single buffer). If that one is optimized (for example, the [Bytes] returns a shared instance |
| 58 | /// instead of making a copy), the copying is avoided. If the request is across a buffer boundary, |
| 59 | /// a copy is made. |
| 60 | /// |
| 61 | /// The [chunks_vectored][SegmentedSlice::chunks_vectored] will properly output as many slices as |
| 62 | /// possible, not just 1 as the default implementation does. |
| 63 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 64 | pub struct SegmentedSlicerust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#<'a, B> { |
| 65 | remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#remaining.: usize, |
| 66 | idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx.: usize, |
| 67 | bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#bufs.: &'a mut [B], |
| 68 | } |
| 69 | |
| 70 | impl<'a, B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#> SegmentedSlicerust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#<'a, B> { |
| 71 | /// Creates a new buffer out of a slice of buffers. |
| 72 | /// |
| 73 | /// The buffers will then be taken in order to form one bigger buffer. |
| 74 | /// |
| 75 | /// Each of the buffers in turn will be exhausted using its [advance][Buf::advance] before |
| 76 | /// proceeding to the next one. Note that the buffers are not dropped (unlike with |
| 77 | /// [SegmentedBuf]). |
| 78 | pub fn newrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`]new().(bufs: &'a mut [B]) -> Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`] { |
| 79 | let remaining = bufs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#::remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#remaining().).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().(); |
| 80 | let mut me = Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`] { |
| 81 | remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#remaining., |
| 82 | idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx.: 0, |
| 83 | bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#bufs., |
| 84 | }; |
| 85 | me.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`]clean_empty().(); |
| 86 | me |
| 87 | } |
| 88 | |
| 89 | fn clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`]clean_empty().(&mut self) { |
| 90 | while self.idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#bufs..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#bufs.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..has_remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#has_remaining().() { |
| 91 | self.idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl<'a, B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#> Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf# for SegmentedSlicerust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#<'a, B> { |
| 97 | fn remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]remaining().(&self) -> usize { |
| 98 | self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#remaining. |
| 99 | } |
| 100 | |
| 101 | fn chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]chunk().(&self) -> &[u8] { |
| 102 | self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#bufs..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(self.idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx.).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#::chunkrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#chunk().).unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().() |
| 103 | } |
| 104 | |
| 105 | fn advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]advance().(&mut self, mut cnt: usize) { |
| 106 | self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#remaining. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().cnt; |
| 107 | while cnt rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 108 | let first = &mut self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#bufs.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().self.idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().; |
| 109 | let rem = first.remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/impl#[`&mut T`][Buf]remaining().(); |
| 110 | let segment = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(rem, cnt); |
| 111 | first.advancerust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#advance().(segment); |
| 112 | cnt rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().segment; |
| 113 | self.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`]clean_empty().(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn copy_to_bytesrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]copy_to_bytes().(&mut self, len: usize) -> Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes# { |
| 118 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(len <= self.remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/impl#[`&mut T`][Buf]remaining().(), "`len` greater than remaining"); |
| 119 | match self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#bufs..get_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_mut().(self.idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx.) { |
| 120 | // Special optimized case. The whole request comes from the front buffer. That one may |
| 121 | // be optimized to do something more efficient, like slice the Bytes (if B == Bytes) |
| 122 | // instead of copying, so we take the opportunity if it offers itself. |
| 123 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(front) if front.remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/impl#[`&mut T`][Buf]remaining().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().len => { |
| 124 | self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#remaining. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().len; |
| 125 | let res = front.copy_to_bytesrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#copy_to_bytes().(len); |
| 126 | self.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`]clean_empty().(); |
| 127 | res |
| 128 | } |
| 129 | // The general case, borrowed from the default implementation (there's no way to |
| 130 | // delegate to it, is there?) |
| 131 | _ => { |
| 132 | let mut res = BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]with_capacity().(len); |
| 133 | res.putrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][BufMut]put().(self.takerust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#take().(len)); |
| 134 | res.freezerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]freeze().() |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[cfg(feature = "std")] |
| 140 | fn chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]chunks_vectored().<'s>(&'s self, dst: &mut [IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#<'s>]) -> usize { |
| 141 | let bufs = self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#bufs..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(self.idxrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#idx...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#).unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().(); |
| 142 | chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/chunks_vectored().(bufs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().(), dst) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /// A concatenation of multiple buffers into a large one, without copying the bytes over. |
| 147 | /// |
| 148 | /// Note that this doesn't provide a continuous slice view into them, it is split into the segments |
| 149 | /// of the original smaller buffers. |
| 150 | /// |
| 151 | /// This variants drop the inner buffers as they are exhausted and new ones can be added. But it |
| 152 | /// internally keeps a [VecDeque], therefore needs a heap allocation. If you don't need the |
| 153 | /// extending behaviour, but want to avoid the allocation, the [SegmentedSlice] can be used instead. |
| 154 | /// |
| 155 | /// # Why |
| 156 | /// |
| 157 | /// This can be used, for example, if data of unknown length is coming over the network (for |
| 158 | /// example, the bodies in [hyper] act a bit like this, it returns a stream of [Bytes] buffers). |
| 159 | /// One might want to accumulate the whole body before acting on it, possibly by parsing it through |
| 160 | /// [serde] or [prost]. Options would include: |
| 161 | /// |
| 162 | /// * Have a `Vec<u8>` and extend it with each chunk. This needlessly copy the bytes every time and |
| 163 | /// reallocates if the vector grows too large. |
| 164 | /// * Repeatedly use [chain][Buf::chain], but this changes the type of the whole buffer, therefore |
| 165 | /// needs to be boxed. |
| 166 | /// * Use [hyper::body::aggregate] to create a [Buf] implementation that concatenates all of them |
| 167 | /// together, but lacks any kind of flexibility (like protecting against loading too much data |
| 168 | /// into memory). |
| 169 | /// |
| 170 | /// This type allows for concatenating multiple buffers, either all at once, or by incrementally |
| 171 | /// pushing more buffers to the end. |
| 172 | /// |
| 173 | /// # Heterogeneous buffers |
| 174 | /// |
| 175 | /// This expects all the buffers are of the same type. If different-typed buffers are needed, one |
| 176 | /// needs to use dynamic dispatch, either something like `SegmentedBuf<Box<Buf>>` or |
| 177 | /// `SegmentedBuf<&mut Buf>`. |
| 178 | /// |
| 179 | /// # Example |
| 180 | /// |
| 181 | /// ```rust |
| 182 | /// # use std::io::Read; |
| 183 | /// # use bytes::{Bytes, Buf}; |
| 184 | /// # use bytes_utils::SegmentedBuf; |
| 185 | /// let mut buf = SegmentedBuf::new(); |
| 186 | /// buf.push(Bytes::from("Hello")); |
| 187 | /// buf.push(Bytes::from(" ")); |
| 188 | /// buf.push(Bytes::from("World")); |
| 189 | /// |
| 190 | /// assert_eq!(3, buf.segments()); |
| 191 | /// assert_eq!(11, buf.remaining()); |
| 192 | /// assert_eq!(b"Hello", buf.chunk()); |
| 193 | /// |
| 194 | /// let mut out = String::new(); |
| 195 | /// buf.reader().read_to_string(&mut out).expect("Doesn't cause IO errors"); |
| 196 | /// assert_eq!("Hello World", out); |
| 197 | /// ``` |
| 198 | /// |
| 199 | /// # FIFO behaviour |
| 200 | /// |
| 201 | /// The buffers are dropped once their data are completely consumed. Additionally, it is possible |
| 202 | /// to add more buffers to the end, even while some of the previous buffers were partially or fully |
| 203 | /// consumed. That makes it usable as kind of a queue (that operates on the buffers, not individual |
| 204 | /// bytes). |
| 205 | /// |
| 206 | /// ```rust |
| 207 | /// # use bytes::{Bytes, Buf}; |
| 208 | /// # use bytes_utils::SegmentedBuf; |
| 209 | /// let mut buf = SegmentedBuf::new(); |
| 210 | /// buf.push(Bytes::from("Hello")); |
| 211 | /// assert_eq!(1, buf.segments()); |
| 212 | /// |
| 213 | /// let mut out = [0; 3]; |
| 214 | /// buf.copy_to_slice(&mut out); |
| 215 | /// assert_eq!(&out, b"Hel"); |
| 216 | /// assert_eq!(2, buf.remaining()); |
| 217 | /// assert_eq!(1, buf.segments()); |
| 218 | /// |
| 219 | /// buf.push(Bytes::from("World")); |
| 220 | /// assert_eq!(7, buf.remaining()); |
| 221 | /// assert_eq!(2, buf.segments()); |
| 222 | /// |
| 223 | /// buf.copy_to_slice(&mut out); |
| 224 | /// assert_eq!(&out, b"loW"); |
| 225 | /// assert_eq!(4, buf.remaining()); |
| 226 | /// assert_eq!(1, buf.segments()); |
| 227 | /// ``` |
| 228 | /// |
| 229 | /// # Optimizations |
| 230 | /// |
| 231 | /// The [copy_to_bytes][SegmentedBuf::copy_to_bytes] method tries to avoid copies by delegating |
| 232 | /// into the underlying buffer if possible (if the whole request can be fulfilled using only a |
| 233 | /// single buffer). If that one is optimized (for example, the [Bytes] returns a shared instance |
| 234 | /// instead of making a copy), the copying is avoided. If the request is across a buffer boundary, |
| 235 | /// a copy is made. |
| 236 | /// |
| 237 | /// The [chunks_vectored][SegmentedBuf::chunks_vectored] will properly output as many slices as |
| 238 | /// possible, not just 1 as the default implementation does. |
| 239 | /// |
| 240 | /// [hyper]: https://docs.rs/hyper |
| 241 | /// [serde]: https://docs.rs/serde |
| 242 | /// [prost]: https://docs.rs/prost |
| 243 | /// [hyper::body::aggregate]: https://docs.rs/hyper/0.14.2/hyper/body/fn.aggregate.html |
| 244 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 245 | pub struct SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 246 | bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs.: VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#<B>, |
| 247 | // Pre-computed sum of the total remaning |
| 248 | remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining.: usize, |
| 249 | } |
| 250 | |
| 251 | impl<B> SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 252 | /// Creates a new empty instance. |
| 253 | /// |
| 254 | /// The instance can be [pushed][SegmentedBuf::push] or [extended][Extend] later. |
| 255 | /// |
| 256 | /// Alternatively, one may create it directly from an iterator, a [Vec] or a [VecDeque] of |
| 257 | /// buffers. |
| 258 | pub fn newrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]new().() -> Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`] { |
| 259 | Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]::defaultrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Default]default().() |
| 260 | } |
| 261 | |
| 262 | /// Returns the yet unconsumed sequence of buffers. |
| 263 | pub fn into_innerrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]into_inner().(self) -> VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#<B> { |
| 264 | self.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 265 | } |
| 266 | |
| 267 | /// Returns the number of segments (buffers) this contains. |
| 268 | pub fn segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().(&self) -> usize { |
| 269 | self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]len().() |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | impl<B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#> SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 274 | /// Extends the buffer by another segment. |
| 275 | /// |
| 276 | /// The newly added segment is added to the end of the buffer (the buffer works as a FIFO). |
| 277 | pub fn pushrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]push().(&mut self, buf: B) { |
| 278 | self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().buf.remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#remaining().(); |
| 279 | self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..push_backrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]push_back().(buf); |
| 280 | self.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]clean_empty().(); |
| 281 | } |
| 282 | fn update_remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]update_remaining().(&mut self) { |
| 283 | self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining. = self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#::remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#remaining().).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().(); |
| 284 | } |
| 285 | fn clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]clean_empty().(&mut self) { |
| 286 | loop { |
| 287 | match self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..frontrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]front().() { |
| 288 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b) if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().b.has_remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#has_remaining().() => { |
| 289 | self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..pop_frontrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]pop_front().(); |
| 290 | } |
| 291 | _ => break, |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | impl<B> Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 298 | fn defaultrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Default]default().() -> Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Default] { |
| 299 | Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Default] { |
| 300 | bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs.: VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T>`]new().(), |
| 301 | remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining.: 0, |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | impl<B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<B>> for SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 307 | fn fromrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`From<Vec<B>>`]from().(bufs: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<B>) -> Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`From<Vec<B>>`] { |
| 308 | Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`From<Vec<B>>`]::fromrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`From<VecDeque<B>>`]from().(VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`][`From<Vec<T, A>>`]from().(bufs)) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | impl<B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#<B>> for SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 313 | fn fromrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`From<VecDeque<B>>`]from().(bufs: VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#<B>) -> Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`From<VecDeque<B>>`] { |
| 314 | let mut me = Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`From<VecDeque<B>>`] { bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs., remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining.: 0 }; |
| 315 | me.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]clean_empty().(); |
| 316 | me.update_remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]update_remaining().(); |
| 317 | me |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | impl<B> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B>> for VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#<B> { |
| 322 | fn fromrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`VecDeque<B>`][`From<SegmentedBuf<B>>`]from().(me: SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B>) -> Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`VecDeque<B>`][`From<SegmentedBuf<B>>`] { |
| 323 | me.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs. |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | impl<B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#> Extendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/Extend#<B> for SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 328 | fn extendrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`Extend<B>`]extend().<T: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = B>>(&mut self, iter: T) { |
| 329 | self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`][`Extend<T>`]extend().(iter); |
| 330 | self.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]clean_empty().(); |
| 331 | self.update_remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]update_remaining().(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | impl<B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#> FromIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/FromIterator#<B> for SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 336 | fn from_iterrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`FromIterator<B>`]from_iter().<T: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = B>>(iter: T) -> Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`FromIterator<B>`] { |
| 337 | let mut me = Selfrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`FromIterator<B>`] { |
| 338 | bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs.: VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#::from_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T>`][`FromIterator<T>`]from_iter().(iter), |
| 339 | remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining.: 0, |
| 340 | }; |
| 341 | me.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]clean_empty().(); |
| 342 | me.update_remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]update_remaining().(); |
| 343 | me |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | impl<B: Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#> Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf# for SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<B> { |
| 348 | fn remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().(&self) -> usize { |
| 349 | self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining. |
| 350 | } |
| 351 | |
| 352 | fn chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().(&self) -> &[u8] { |
| 353 | self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..frontrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]front().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(Bufrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#::chunkrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#chunk().).unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().() |
| 354 | } |
| 355 | |
| 356 | fn advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]advance().(&mut self, mut cnt: usize) { |
| 357 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(cnt <= self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining., "Advance past the end of buffer"); |
| 358 | self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().cnt; |
| 359 | while cnt rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 360 | let front = self |
| 361 | .bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs. |
| 362 | .front_mutrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]front_mut().() |
| 363 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Missing buffers to provide remaining"); |
| 364 | let front_remaining = front.remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/impl#[`&mut T`][Buf]remaining().(); |
| 365 | if front_remaining rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().cnt { |
| 366 | front.advancerust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#advance().(cnt); |
| 367 | break; |
| 368 | } else { |
| 369 | // We advance past the whole front buffer |
| 370 | cnt rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().front_remaining; |
| 371 | self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..pop_frontrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]pop_front().(); |
| 372 | } |
| 373 | } |
| 374 | self.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]clean_empty().(); |
| 375 | } |
| 376 | |
| 377 | fn copy_to_bytesrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]copy_to_bytes().(&mut self, len: usize) -> Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes# { |
| 378 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(len <= self.remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/impl#[`&mut T`][Buf]remaining().(), "`len` greater than remaining"); |
| 379 | match self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..front_mutrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]front_mut().() { |
| 380 | // Special optimized case. The whole request comes from the front buffer. That one may |
| 381 | // be optimized to do something more efficient, like slice the Bytes (if B == Bytes) |
| 382 | // instead of copying, so we take the opportunity if it offers itself. |
| 383 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(front) if front.remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/impl#[`&mut T`][Buf]remaining().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().len => { |
| 384 | self.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#remaining. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`SubAssign<Self>`]sub_assign().len; |
| 385 | let res = front.copy_to_bytesrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#copy_to_bytes().(len); |
| 386 | self.clean_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]clean_empty().(); |
| 387 | res |
| 388 | } |
| 389 | // The general case, borrowed from the default implementation (there's no way to |
| 390 | // delegate to it, is there?) |
| 391 | _ => { |
| 392 | let mut res = BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]with_capacity().(len); |
| 393 | res.putrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][BufMut]put().(self.takerust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#take().(len)); |
| 394 | res.freezerust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]freeze().() |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | #[cfg(feature = "std")] |
| 400 | fn chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunks_vectored().<'a>(&'a self, dst: &mut [IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#<'a>]) -> usize { |
| 401 | chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/chunks_vectored().(self.bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#bufs..iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/impl#[`VecDeque<T, A>`]iter().(), dst) |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | #[cfg(test)] |
| 406 | mod testsrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/ { |
| 407 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#; |
| 408 | |
| 409 | use superrust-analyzer cargo bytes-utils 0.1.4 segmented/::*; |
| 410 | |
| 411 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 412 | fn emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/empty().() { |
| 413 | let mut b = SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#::<Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#>::newrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]new().(); |
| 414 | |
| 415 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().b.has_remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#has_remaining().()); |
| 416 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().()); |
| 417 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().().is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().()); |
| 418 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().()); |
| 419 | |
| 420 | b.copy_to_slicerust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#copy_to_slice().(&mut []); |
| 421 | b.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]advance().(0); |
| 422 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, b.readerrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#reader().().readrust-analyzer cargo bytes 1.12.1 buf/reader/impl#[`Reader<B>`][Read]read().(&mut [0; 10]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 423 | } |
| 424 | |
| 425 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 426 | fn empty_slicesrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/empty_slices().() { |
| 427 | let mut b = SegmentedSlicerust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#::<&[u8]>::defaultrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Default]default().(); |
| 428 | |
| 429 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().b.has_remainingrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#has_remaining().()); |
| 430 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]remaining().()); |
| 431 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]chunk().().is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().()); |
| 432 | |
| 433 | b.copy_to_slicerust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#copy_to_slice().(&mut []); |
| 434 | b.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]advance().(0); |
| 435 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, b.readerrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#reader().().readrust-analyzer cargo bytes 1.12.1 buf/reader/impl#[`Reader<B>`][Read]read().(&mut [0; 10]).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()); |
| 436 | } |
| 437 | |
| 438 | fn segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().() -> SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#> { |
| 439 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 440 | Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::fromrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes][`From<&'static str>`]from().("Hello"), |
| 441 | Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::fromrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes][`From<&'static str>`]from().(" "), |
| 442 | Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().(), |
| 443 | Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::fromrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes][`From<&'static str>`]from().("World"), |
| 444 | ] |
| 445 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 446 | } |
| 447 | |
| 448 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 449 | fn segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segments().() { |
| 450 | let mut b = segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().(); |
| 451 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(11, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().()); |
| 452 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"Hello", b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().()); |
| 453 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(4, b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().()); |
| 454 | b.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]advance().(3); |
| 455 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(8, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().()); |
| 456 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"lo", b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().()); |
| 457 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(4, b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().()); |
| 458 | } |
| 459 | |
| 460 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 461 | fn to_bytes_allrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/to_bytes_all().() { |
| 462 | let mut b = segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().(); |
| 463 | let bytes = b.copy_to_bytesrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]copy_to_bytes().(11); |
| 464 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("Hello World", &bytes); |
| 465 | } |
| 466 | |
| 467 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 468 | fn advance_withinrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/advance_within().() { |
| 469 | let mut b = segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().(); |
| 470 | b.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]advance().(2); |
| 471 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(4, b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().()); |
| 472 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(9, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().()); |
| 473 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"llo", b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().()); |
| 474 | } |
| 475 | |
| 476 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 477 | fn advance_borderrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/advance_border().() { |
| 478 | let mut b = segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().(); |
| 479 | b.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]advance().(5); |
| 480 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(3, b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().()); |
| 481 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(6, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().()); |
| 482 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b" ", b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().()); |
| 483 | } |
| 484 | |
| 485 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 486 | fn advance_acrossrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/advance_across().() { |
| 487 | let mut b = segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().(); |
| 488 | b.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]advance().(7); |
| 489 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(1, b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().()); |
| 490 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(4, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().()); |
| 491 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"orld", b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().()); |
| 492 | } |
| 493 | |
| 494 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 495 | fn empty_at_borderrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/empty_at_border().() { |
| 496 | let mut b = segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().(); |
| 497 | b.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]advance().(6); |
| 498 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(1, b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().()); |
| 499 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(5, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().()); |
| 500 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"World", b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().()); |
| 501 | } |
| 502 | |
| 503 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 504 | fn empty_bufsrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/empty_bufs().() { |
| 505 | fn is_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/is_empty().(b: &SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#<Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#>) { |
| 506 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().()); |
| 507 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(0, b.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]remaining().()); |
| 508 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"", b.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunk().()); |
| 509 | } |
| 510 | |
| 511 | is_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/is_empty().(&vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![].intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 512 | is_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/is_empty().(&vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().(), Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().()].intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 513 | is_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/is_empty().(&vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().(), Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().()].into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()); |
| 514 | |
| 515 | let mut b = SegmentedBufrust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedBuf#::newrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]new().(); |
| 516 | is_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/is_empty().(&b); |
| 517 | b.pushrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]push().(Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().()); |
| 518 | is_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/is_empty().(&b); |
| 519 | b.extendrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][`Extend<B>`]extend().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().(), Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::newrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]new().()]); |
| 520 | is_emptyrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/is_empty().(&b); |
| 521 | } |
| 522 | |
| 523 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 524 | fn sliced_hellorust-analyzer cargo bytes-utils 0.1.4 segmented/tests/sliced_hello().() { |
| 525 | let mut buffers = [b"Hello" as &[_], b"", b" ", b"", b"World"]; |
| 526 | let buf = SegmentedSlicerust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#::newrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`]new().(&mut buffers); |
| 527 | |
| 528 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(11, buf.remainingrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]remaining().()); |
| 529 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"Hello", buf.chunkrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]chunk().()); |
| 530 | |
| 531 | let mut out = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 532 | buf.readerrust-analyzer cargo bytes 1.12.1 buf/buf_impl/Buf#reader().() |
| 533 | .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_string().(&mut out) |
| 534 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Doesn't cause IO errors"); |
| 535 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("Hello World", out); |
| 536 | } |
| 537 | |
| 538 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 539 | fn chunk_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/chunk_vectored().() { |
| 540 | let mut b = segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().(); |
| 541 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b.chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunks_vectored().(&mut []), 0); |
| 542 | let mut slices = [IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`]new().(&[]); 5]; |
| 543 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b.segmentsrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`]segments().(), 4); |
| 544 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b.chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunks_vectored().(&mut slices), 3); |
| 545 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"Hello"); |
| 546 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b" "); |
| 547 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"World"); |
| 548 | b.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]advance().(2); |
| 549 | let mut slices = [IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`]new().(&[]); 1]; |
| 550 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b.chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedBuf<B>`][Buf]chunks_vectored().(&mut slices), 1); |
| 551 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"llo"); |
| 552 | } |
| 553 | |
| 554 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 555 | fn chunk_vectored_nestedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/chunk_vectored_nested().() { |
| 556 | let mut bufs = [segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().(), segmentedrust-analyzer cargo bytes-utils 0.1.4 segmented/tests/segmented().()]; |
| 557 | let mut bufs = SegmentedSlicerust-analyzer cargo bytes-utils 0.1.4 segmented/SegmentedSlice#::newrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`]new().(&mut bufs); |
| 558 | let mut slices = [IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`]new().(&[]); 10]; |
| 559 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bufs.chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]chunks_vectored().(&mut slices), 6); |
| 560 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"Hello"); |
| 561 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b" "); |
| 562 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"World"); |
| 563 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"Hello"); |
| 564 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b" "); |
| 565 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().5]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"World"); |
| 566 | bufs.advancerust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]advance().(2); |
| 567 | let mut slices = [IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`]new().(&[]); 1]; |
| 568 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(bufs.chunks_vectoredrust-analyzer cargo bytes-utils 0.1.4 segmented/impl#[`SegmentedSlice<'a, B>`][Buf]chunks_vectored().(&mut slices), 1); |
| 569 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`IoSlice<'a>`][Deref]deref().slices[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"llo"); |
| 570 | } |
| 571 | |
| 572 | #[cfg(not(miri))] |
| 573 | mod proptests { |
| 574 | |
| 575 | use superrust-analyzer cargo bytes-utils 0.1.4 segmented/::*; |
| 576 | use proptestrust-analyzer cargo proptest 1.11.0 crate/::preluderust-analyzer cargo proptest 1.11.0 prelude/::*; |
| 577 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#; |
| 578 | |
| 579 | proptestrust-analyzer cargo proptest 1.11.0 crate/! { |
| 580 | #[test] |
| 581 | fn random(bufs: Vec<Vec<u8>>, splits in proptest::collection::vec(0..10usize, 1..10)) { |
| 582 | let concat: Vec<u8> = bufs.iter().flat_map(|b| b.iter()).copied().collect(); |
| 583 | let mut segmented = bufs.iter() |
| 584 | .map(|b| &b[..]) |
| 585 | .collect::<SegmentedBuf<_>>(); |
| 586 | assert_eq!(concat.len(), segmented.remaining()); |
| 587 | assert!(segmented.segments() <= bufs.len()); |
| 588 | assert!(concat.starts_with(segmented.chunk())); |
| 589 | let mut bytes = segmented.clone().copy_to_bytes(segmented.remaining()); |
| 590 | assert_eq!(&concat[..], &bytes[..]); |
| 591 | let mut sliced = bufs.iter().map(Deref::deref).collect::<Vec<&[u8]>>(); |
| 592 | let mut sliced = SegmentedSlice::new(&mut sliced); |
| 593 | |
| 594 | let mut fifo = SegmentedBuf::new(); |
| 595 | let mut buf_pos = bufs.iter(); |
| 596 | |
| 597 | for split in splits { |
| 598 | if !bytes.has_remaining() { |
| 599 | break; |
| 600 | } |
| 601 | let split = cmp::min(bytes.remaining(), split); |
| 602 | while fifo.remaining() < split { |
| 603 | fifo.push(&buf_pos.next().unwrap()[..]); |
| 604 | } |
| 605 | let c1 = bytes.copy_to_bytes(split); |
| 606 | let c2 = segmented.copy_to_bytes(split); |
| 607 | let c3 = sliced.copy_to_bytes(split); |
| 608 | assert_eq!(c1, c2); |
| 609 | assert_eq!(c1, c3); |
| 610 | assert_eq!(bytes.remaining(), segmented.remaining()); |
| 611 | assert_eq!(bytes.remaining(), sliced.remaining()); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | } |