| 1 | // Copyright (c) The buf-list Contributors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | #[cfg(feature = "futures03")] |
| 5 | mod futures_imprust-analyzer cargo buf-list 1.1.2 cursor/futures_imp/; |
| 6 | #[cfg(test)] |
| 7 | mod testsrust-analyzer cargo buf-list 1.1.2 cursor/tests/; |
| 8 | #[cfg(feature = "tokio1")] |
| 9 | mod tokio_imprust-analyzer cargo buf-list 1.1.2 cursor/tokio_imp/; |
| 10 | |
| 11 | use craterust-analyzer cargo buf-list 1.1.2 crate/::{BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#, errorsrust-analyzer cargo buf-list 1.1.2 errors/::ReadExactErrorrust-analyzer cargo buf-list 1.1.2 errors/ReadExactError#}; |
| 12 | use bytesrust-analyzer cargo bytes 1.3.0 crate/::{Bufrust-analyzer cargo bytes 1.3.0 buf/buf_impl/Buf#, Bytesrust-analyzer cargo bytes 1.3.0 bytes/Bytes#}; |
| 13 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 14 | cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#, |
| 15 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-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#, IoSliceMutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSliceMut#, SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#}, |
| 16 | }; |
| 17 | |
| 18 | /// A `Cursor` wraps an in-memory `BufList` and provides it with a [`Seek`] implementation. |
| 19 | /// |
| 20 | /// `Cursor`s allow `BufList`s to implement [`Read`] and [`BufRead`], allowing a `BufList` to be |
| 21 | /// used anywhere you might use a reader or writer that does actual I/O. |
| 22 | /// |
| 23 | /// The cursor may either own or borrow a `BufList`: both `Cursor<BufList>` and `Cursor<&BufList>` |
| 24 | /// are supported. |
| 25 | /// |
| 26 | /// # Optional features |
| 27 | /// |
| 28 | /// * `tokio1`: With this feature enabled, [`Cursor`] implements the `tokio` crate's |
| 29 | /// [`AsyncSeek`](tokio::io::AsyncSeek), [`AsyncRead`](tokio::io::AsyncRead) and |
| 30 | /// [`AsyncBufRead`](tokio::io::AsyncBufRead). |
| 31 | /// * `futures03`: With this feature enabled, [`Cursor`] implements the `futures` crate's |
| 32 | /// [`AsyncSeek`](futures_io_03::AsyncSeek), [`AsyncRead`](futures_io_03::AsyncRead) and |
| 33 | /// [`AsyncBufRead`](futures_io_03::AsyncBufRead). |
| 34 | /// |
| 35 | /// [`Read`]: std::io::Read |
| 36 | /// [`BufRead`]: std::io::BufRead |
| 37 | /// [`Seek`]: std::io::Seek |
| 38 | pub struct Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#<T> { |
| 39 | innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner.: T, |
| 40 | |
| 41 | /// Data associated with the cursor. |
| 42 | datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data.: CursorDatarust-analyzer cargo buf-list 1.1.2 cursor/CursorData#, |
| 43 | } |
| 44 | |
| 45 | impl<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#>> Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#<T> { |
| 46 | /// Creates a new cursor wrapping the provided `BufList`. |
| 47 | /// |
| 48 | /// # Examples |
| 49 | /// |
| 50 | /// ``` |
| 51 | /// use buf_list::{BufList, Cursor}; |
| 52 | /// |
| 53 | /// let cursor = Cursor::new(BufList::new()); |
| 54 | /// ``` |
| 55 | pub fn newrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`]new().(inner: T) -> Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#<T> { |
| 56 | let data = CursorDatarust-analyzer cargo buf-list 1.1.2 cursor/CursorData#::newrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]new().(); |
| 57 | Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor# { innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner., datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data. } |
| 58 | } |
| 59 | |
| 60 | /// Consumes this cursor, returning the underlying value. |
| 61 | /// |
| 62 | /// # Examples |
| 63 | /// |
| 64 | /// ``` |
| 65 | /// use buf_list::{BufList, Cursor}; |
| 66 | /// |
| 67 | /// let cursor = Cursor::new(BufList::new()); |
| 68 | /// |
| 69 | /// let vec = cursor.into_inner(); |
| 70 | /// ``` |
| 71 | pub fn into_innerrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`]into_inner().(self) -> T { |
| 72 | self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner. |
| 73 | } |
| 74 | |
| 75 | /// Gets a reference to the underlying value in this cursor. |
| 76 | /// |
| 77 | /// # Examples |
| 78 | /// |
| 79 | /// ``` |
| 80 | /// use buf_list::{BufList, Cursor}; |
| 81 | /// |
| 82 | /// let cursor = Cursor::new(BufList::new()); |
| 83 | /// |
| 84 | /// let reference = cursor.get_ref(); |
| 85 | /// ``` |
| 86 | pub const fn get_refrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`]get_ref().(&self) -> &T { |
| 87 | &self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner. |
| 88 | } |
| 89 | |
| 90 | /// Returns the current position of this cursor. |
| 91 | /// |
| 92 | /// # Examples |
| 93 | /// |
| 94 | /// ``` |
| 95 | /// use buf_list::{BufList, Cursor}; |
| 96 | /// use std::io::prelude::*; |
| 97 | /// use std::io::SeekFrom; |
| 98 | /// |
| 99 | /// let mut cursor = Cursor::new(BufList::from(&[1, 2, 3, 4, 5][..])); |
| 100 | /// |
| 101 | /// assert_eq!(cursor.position(), 0); |
| 102 | /// |
| 103 | /// cursor.seek(SeekFrom::Current(2)).unwrap(); |
| 104 | /// assert_eq!(cursor.position(), 2); |
| 105 | /// |
| 106 | /// cursor.seek(SeekFrom::Current(-1)).unwrap(); |
| 107 | /// assert_eq!(cursor.position(), 1); |
| 108 | /// ``` |
| 109 | pub const fn positionrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`]position().(&self) -> u64 { |
| 110 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. |
| 111 | } |
| 112 | |
| 113 | /// Sets the position of this cursor. |
| 114 | /// |
| 115 | /// # Examples |
| 116 | /// |
| 117 | /// ``` |
| 118 | /// use buf_list::{BufList, Cursor}; |
| 119 | /// |
| 120 | /// let mut cursor = Cursor::new(BufList::from(&[1, 2, 3, 4, 5][..])); |
| 121 | /// |
| 122 | /// assert_eq!(cursor.position(), 0); |
| 123 | /// |
| 124 | /// cursor.set_position(2); |
| 125 | /// assert_eq!(cursor.position(), 2); |
| 126 | /// |
| 127 | /// cursor.set_position(4); |
| 128 | /// assert_eq!(cursor.position(), 4); |
| 129 | /// ``` |
| 130 | pub fn set_positionrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`]set_position().(&mut self, pos: u64) { |
| 131 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..set_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]set_pos().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), pos); |
| 132 | } |
| 133 | |
| 134 | // --- |
| 135 | // Helper methods |
| 136 | // --- |
| 137 | #[cfg(test)] |
| 138 | fn assert_invariantsrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`]assert_invariants().(&self) -> anyhowrust-analyzer cargo anyhow 1.0.70 crate/::Resultrust-analyzer cargo anyhow 1.0.70 Result#<()> { |
| 139 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..assert_invariantsrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]assert_invariants().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | impl<T> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#<T> |
| 144 | where |
| 145 | T: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 146 | { |
| 147 | #[inline] |
| 148 | fn clonerust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Clone]clone().(&self) -> Selfrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Clone] { |
| 149 | Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor# { |
| 150 | innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner.: self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().(), |
| 151 | datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data.: self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..clonerust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData][Clone]clone().(), |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[inline] |
| 156 | fn clone_fromrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Clone]clone_from().(&mut self, other: &Selfrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Clone]) { |
| 157 | self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..clone_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone_from().(&other.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner.); |
| 158 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data. = other.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..clonerust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData][Clone]clone().(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | impl<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#>> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek# for Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#<T> { |
| 163 | fn seekrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Seek]seek().(&mut self, style: SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u64> { |
| 164 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..seek_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]seek_impl().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), style) |
| 165 | } |
| 166 | |
| 167 | fn stream_positionrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Seek]stream_position().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u64> { |
| 168 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos.) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | impl<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#>> 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# for Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#<T> { |
| 173 | fn readrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Read]read().(&mut self, buf: &mut [u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 174 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..read_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]read_impl().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), buf)) |
| 175 | } |
| 176 | |
| 177 | fn read_vectoredrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Read]read_vectored().(&mut self, bufs: &mut [IoSliceMutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSliceMut#<'_>]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 178 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..read_vectored_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]read_vectored_impl().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), bufs)) |
| 179 | } |
| 180 | |
| 181 | // TODO: is_read_vectored once that's available on stable Rust. |
| 182 | |
| 183 | fn read_exactrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Read]read_exact().(&mut self, buf: &mut [u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 184 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..read_exact_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]read_exact_impl().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), buf) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | impl<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#>> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead# for Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#<T> { |
| 189 | fn fill_bufrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][BufRead]fill_buf().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<&[u8]> { |
| 190 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..fill_buf_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]fill_buf_impl().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().())) |
| 191 | } |
| 192 | |
| 193 | fn consumerust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][BufRead]consume().(&mut self, amt: usize) { |
| 194 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..consume_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]consume_impl().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), amt); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | impl<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#>> Bufrust-analyzer cargo bytes 1.3.0 buf/buf_impl/Buf# for Cursorrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#<T> { |
| 199 | fn remainingrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Buf]remaining().(&self) -> usize { |
| 200 | let total = self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..num_bytesrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]num_bytes().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()); |
| 201 | total.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().(self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos.) as usize |
| 202 | } |
| 203 | |
| 204 | fn has_remainingrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Buf]has_remaining().(&self) -> bool { |
| 205 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..num_bytesrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]num_bytes().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]gt().self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. |
| 206 | } |
| 207 | |
| 208 | fn chunkrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Buf]chunk().(&self) -> &[u8] { |
| 209 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..fill_buf_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]fill_buf_impl().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 210 | } |
| 211 | |
| 212 | fn advancerust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Buf]advance().(&mut self, amt: usize) { |
| 213 | self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..consume_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]consume_impl().(self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), amt); |
| 214 | } |
| 215 | |
| 216 | fn chunks_vectoredrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Buf]chunks_vectored().<'iovs>(&'iovs self, iovs: &mut [IoSlicerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSlice#<'iovs>]) -> usize { |
| 217 | let list = self.innerrust-analyzer cargo buf-list 1.1.2 cursor/Cursor#inner..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(); |
| 218 | |
| 219 | if iovs.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() || !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.has_remainingrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Cursor<T>`][Buf]has_remaining().() { |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | let current_chunk = self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.; |
| 224 | let chunk_start_pos = list.get_start_posrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_start_pos().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().current_chunk]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 225 | let offset_in_chunk = (self.datarust-analyzer cargo buf-list 1.1.2 cursor/Cursor#data..posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().chunk_start_pos) as usize; |
| 226 | |
| 227 | iovs[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut(). = 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().( |
| 228 | &list.get_chunkrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_chunk().(current_chunk).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("chunk is in range")[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().offset_in_chunk..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/Index#index()., |
| 229 | ); |
| 230 | // Fill up the remaining iovs with as many slices as possible. |
| 231 | let to_fill = (iovs.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(list.num_chunksrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]num_chunks().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().current_chunk); |
| 232 | for (i, iov) in iovs.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(to_fill).skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(1) { |
| 233 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().iov = 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().( |
| 234 | list.get_chunkrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_chunk().(current_chunk rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().i) |
| 235 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("chunk is in range"), |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | to_fill |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | #[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:)] |
| 244 | struct CursorDatarust-analyzer cargo buf-list 1.1.2 cursor/CursorData# { |
| 245 | /// The chunk number the cursor is pointing to. Kept in sync with pos. |
| 246 | /// |
| 247 | /// This is within the range [0, self.start_pos.len()). It is self.start_pos.len() - 1 iff pos |
| 248 | /// is greater than list.num_bytes(). |
| 249 | chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.: usize, |
| 250 | |
| 251 | /// The overall position in the stream. Kept in sync with chunk. |
| 252 | posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos.: u64, |
| 253 | } |
| 254 | |
| 255 | impl CursorDatarust-analyzer cargo buf-list 1.1.2 cursor/CursorData# { |
| 256 | fn newrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]new().() -> Selfrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData] { |
| 257 | Selfrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData] { chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.: 0, posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos.: 0 } |
| 258 | } |
| 259 | |
| 260 | #[cfg(test)] |
| 261 | fn assert_invariantsrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]assert_invariants().(&self, list: &BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#) -> anyhowrust-analyzer cargo anyhow 1.0.70 crate/::Resultrust-analyzer cargo anyhow 1.0.70 Result#<()> { |
| 262 | use anyhowrust-analyzer cargo anyhow 1.0.70 crate/::ensurerust-analyzer cargo anyhow 1.0.70 ensure/; |
| 263 | |
| 264 | ensurerust-analyzer cargo anyhow 1.0.70 macros/ensure!!( |
| 265 | self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. >= list.get_start_posrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_start_pos().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., |
| 266 | "invariant failed: current position {} >= start position {} (chunk = {})", |
| 267 | self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos., |
| 268 | list.get_start_posrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_start_pos().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., |
| 269 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. |
| 270 | ); |
| 271 | |
| 272 | let next_pos = list.get_start_posrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_start_pos().().getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1).copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]copied().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 273 | ensurerust-analyzer cargo anyhow 1.0.70 macros/ensure!!( |
| 274 | Offsetrust-analyzer cargo buf-list 1.1.2 cursor/Offset#::Valuerust-analyzer cargo buf-list 1.1.2 cursor/Offset#Value#(self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos.) <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). next_pos, |
| 275 | "invariant failed: next start position {:?} > current position {} (chunk = {})", |
| 276 | next_pos, |
| 277 | self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos., |
| 278 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. |
| 279 | ); |
| 280 | |
| 281 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 282 | } |
| 283 | |
| 284 | fn seek_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]seek_impl().(&mut self, list: &BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#, style: SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u64> { |
| 285 | let (base_pos, offset) = match style { |
| 286 | SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(n) => { |
| 287 | self.set_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]set_pos().(list, n); |
| 288 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n); |
| 289 | } |
| 290 | SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#End#(n) => (self.num_bytesrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]num_bytes().(list), n), |
| 291 | SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(n) => (self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos., n), |
| 292 | }; |
| 293 | // Can't use checked_add_signed since it was only stabilized in Rust 1.66. This is adapted |
| 294 | // from |
| 295 | // https://github.com/rust-lang/rust/blame/ed937594d3/library/std/src/io/cursor.rs#L295-L299. |
| 296 | let new_pos = if offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]ge().0 { |
| 297 | base_pos.checked_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]checked_add().(offset as u64) |
| 298 | } else { |
| 299 | base_pos.checked_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]checked_sub().(offset.wrapping_negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]wrapping_neg().() as u64) |
| 300 | }; |
| 301 | match new_pos { |
| 302 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(n) => { |
| 303 | self.set_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]set_pos().(list, n); |
| 304 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos.) |
| 305 | } |
| 306 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 307 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#, |
| 308 | "invalid seek to a negative or overflowing position", |
| 309 | )), |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | fn read_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]read_impl().(&mut self, list: &BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#, buf: &mut [u8]) -> usize { |
| 314 | // Read as much as possible until we fill up the buffer. |
| 315 | let mut buf_pos = 0; |
| 316 | while buf_pos 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().buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 317 | let (chunk, chunk_pos) = match self.get_chunk_and_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]get_chunk_and_pos().(list) { |
| 318 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) => value, |
| 319 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => break, |
| 320 | }; |
| 321 | // The number of bytes to copy is the smaller of the two: |
| 322 | // - the length of the chunk - the position in it. |
| 323 | // - the number of bytes remaining, which is buf.len() - buf_pos. |
| 324 | let n_to_copy = (chunk.lenrust-analyzer cargo bytes 1.3.0 bytes/impl#[Bytes]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().chunk_pos).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(buf.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/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().buf_pos); |
| 325 | let chunk_bytes = chunk.as_refrust-analyzer cargo bytes 1.3.0 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().(); |
| 326 | |
| 327 | let bytes_to_copy = &chunk_bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().chunk_pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#(chunk_pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().n_to_copy)]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 328 | let dest = &mut buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().buf_pos..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#(buf_pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().n_to_copy)]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().; |
| 329 | dest.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(bytes_to_copy); |
| 330 | buf_pos 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().n_to_copy; |
| 331 | |
| 332 | // Increment the position. |
| 333 | self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().n_to_copy as u64; |
| 334 | // If we've finished reading through the chunk, move to the next chunk. |
| 335 | if n_to_copy 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().chunk.lenrust-analyzer cargo bytes 1.3.0 bytes/impl#[Bytes]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().chunk_pos { |
| 336 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. 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; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | buf_pos |
| 341 | } |
| 342 | |
| 343 | fn read_vectored_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]read_vectored_impl().(&mut self, list: &BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#, bufs: &mut [IoSliceMutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/IoSliceMut#<'_>]) -> usize { |
| 344 | let mut nread = 0; |
| 345 | for buf in bufs { |
| 346 | // Copy data from the buffer until we run out of bytes to copy. |
| 347 | let n = self.read_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]read_impl().(list, buf); |
| 348 | nread 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().n; |
| 349 | if n 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().buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | nread |
| 354 | } |
| 355 | |
| 356 | fn read_exact_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]read_exact_impl().(&mut self, list: &BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#, buf: &mut [u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 357 | // This is the same as read_impl as long as there's enough space. |
| 358 | let total = self.num_bytesrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]num_bytes().(list); |
| 359 | let remaining = total.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]saturating_sub().(self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos.); |
| 360 | let buf_len = buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 361 | if remaining rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt().buf_len as u64 { |
| 362 | // Rust 1.80 and above will cause the position to be set to the end |
| 363 | // of the buffer, due to (apparently) |
| 364 | // https://github.com/rust-lang/rust/pull/125404. Follow that |
| 365 | // behavior. |
| 366 | self.set_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]set_pos().(list, total); |
| 367 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 368 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::UnexpectedEofrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#UnexpectedEof#, |
| 369 | ReadExactErrorrust-analyzer cargo buf-list 1.1.2 errors/ReadExactError# { remainingrust-analyzer cargo buf-list 1.1.2 errors/ReadExactError#remaining., buf_lenrust-analyzer cargo buf-list 1.1.2 errors/ReadExactError#buf_len. }, |
| 370 | )); |
| 371 | } |
| 372 | |
| 373 | self.read_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]read_impl().(list, buf); |
| 374 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 375 | } |
| 376 | |
| 377 | fn fill_buf_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]fill_buf_impl().<'a>(&'a self, list: &'a BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#) -> &'a [u8] { |
| 378 | const EMPTY_SLICErust-analyzer cargo buf-list 1.1.2 cursor/EMPTY_SLICE.: &[u8] = &[]; |
| 379 | match self.get_chunk_and_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]get_chunk_and_pos().(list) { |
| 380 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((chunk, chunk_pos)) => &chunk.as_refrust-analyzer cargo bytes 1.3.0 bytes/impl#[Bytes][`AsRef<[u8]>`]as_ref().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().chunk_pos..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/Index#index()., |
| 381 | // An empty return value means the end of the buffer has been reached. |
| 382 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => EMPTY_SLICErust-analyzer cargo buf-list 1.1.2 cursor/EMPTY_SLICE., |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | fn consume_implrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]consume_impl().(&mut self, list: &BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#, amt: usize) { |
| 387 | self.set_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]set_pos().(list, self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().amt as u64); |
| 388 | } |
| 389 | |
| 390 | fn set_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]set_pos().(&mut self, list: &BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#, new_pos: u64) { |
| 391 | match new_pos.cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][Ord]cmp().(&self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos.) { |
| 392 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Greaterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Greater# => { |
| 393 | let start_pos = list.get_start_posrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_start_pos().(); |
| 394 | let next_start = start_pos.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1).copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]copied().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(); |
| 395 | if Offsetrust-analyzer cargo buf-list 1.1.2 cursor/Offset#::Valuerust-analyzer cargo buf-list 1.1.2 cursor/Offset#Value#(new_pos) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().next_start { |
| 396 | // Within the same chunk. |
| 397 | } else { |
| 398 | // The above check ensures that we're not currently pointing to the last index |
| 399 | // (since it would have returned Eof, which is greater than Offset(n) for any |
| 400 | // n). |
| 401 | // |
| 402 | // Do a binary search for this element. |
| 403 | match start_pos[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..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/Index#index()..binary_searchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]binary_search().(&new_pos) { |
| 404 | // We're starting the search from self.chunk + 1, which means that the value |
| 405 | // returned from binary_search is 1 less than the actual delta. |
| 406 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(delta_minus_one) => { |
| 407 | // Exactly at the start point of a chunk. |
| 408 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. 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 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().delta_minus_one; |
| 409 | } |
| 410 | // The value returned in the error case (not at the start point of a chunk) |
| 411 | // is (delta - 1) + 1, so just delta. |
| 412 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(delta) => { |
| 413 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!( |
| 414 | delta >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). 0, |
| 415 | "delta must be at least 1 since we already \ |
| 416 | checked the same chunk (self.chunk = {})", |
| 417 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk., |
| 418 | ); |
| 419 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. 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().delta; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal# => {} |
| 425 | Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less# => { |
| 426 | let start_pos = list.get_start_posrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_start_pos().(); |
| 427 | if start_pos.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.).copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]copied().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(new_pos) { |
| 428 | // Within the same chunk. |
| 429 | } else { |
| 430 | match start_pos[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..binary_searchrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]binary_search().(&new_pos) { |
| 431 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(chunk) => { |
| 432 | // Exactly at the start point of a chunk. |
| 433 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. = chunk; |
| 434 | } |
| 435 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(chunk_plus_1) => { |
| 436 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!( |
| 437 | chunk_plus_1 >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). 0, |
| 438 | "chunk_plus_1 must be at least 1 since self.start_pos[0] is 0 \ |
| 439 | (self.chunk = {})", |
| 440 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk., |
| 441 | ); |
| 442 | self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk. = chunk_plus_1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. = new_pos; |
| 449 | } |
| 450 | |
| 451 | #[inline] |
| 452 | fn get_chunk_and_posrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]get_chunk_and_pos().<'b>(&self, list: &'b BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(&'b Bytesrust-analyzer cargo bytes 1.3.0 bytes/Bytes#, usize)> { |
| 453 | match list.get_chunkrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_chunk().(self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.) { |
| 454 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(chunk) => { |
| 455 | // This guarantees that pos is not past the end of the list. |
| 456 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!( |
| 457 | self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialOrd<Self>`]lt(). self.num_bytesrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]num_bytes().(list), |
| 458 | "self.pos ({}) is less than num_bytes ({})", |
| 459 | self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos., |
| 460 | self.num_bytesrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]num_bytes().(list) |
| 461 | ); |
| 462 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(( |
| 463 | chunk, |
| 464 | (self.posrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#pos. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().list.get_start_posrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_start_pos().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.chunkrust-analyzer cargo buf-list 1.1.2 cursor/CursorData#chunk.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) as usize, |
| 465 | )) |
| 466 | } |
| 467 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 468 | // pos is past the end of the list. |
| 469 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | fn num_bytesrust-analyzer cargo buf-list 1.1.2 cursor/impl#[CursorData]num_bytes().(&self, list: &BufListrust-analyzer cargo buf-list 1.1.2 imp/BufList#) -> u64 { |
| 475 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().list |
| 476 | .get_start_posrust-analyzer cargo buf-list 1.1.2 imp/impl#[BufList]get_start_pos().() |
| 477 | .lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().() |
| 478 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("start_pos always has at least one element") |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /// This is the same as Option<T> except Offset and Eof are reversed in ordering, i.e. Eof > |
| 483 | /// Offset(T) for any T. |
| 484 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, 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:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 485 | enum Offsetrust-analyzer cargo buf-list 1.1.2 cursor/Offset#<T> { |
| 486 | Valuerust-analyzer cargo buf-list 1.1.2 cursor/Offset#Value#(T), |
| 487 | Eofrust-analyzer cargo buf-list 1.1.2 cursor/Offset#Eof#, |
| 488 | } |
| 489 | |
| 490 | impl<T> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>> for Offsetrust-analyzer cargo buf-list 1.1.2 cursor/Offset#<T> { |
| 491 | fn fromrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Offset<T>`][`From<Option<T>>`]from().(value: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>) -> Selfrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Offset<T>`][`From<Option<T>>`] { |
| 492 | match value { |
| 493 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(v) => Selfrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Offset<T>`][`From<Option<T>>`]::Valuerust-analyzer cargo buf-list 1.1.2 cursor/Offset#Value#(v), |
| 494 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Selfrust-analyzer cargo buf-list 1.1.2 cursor/impl#[`Offset<T>`][`From<Option<T>>`]::Eofrust-analyzer cargo buf-list 1.1.2 cursor/Offset#Eof#, |
| 495 | } |
| 496 | } |
| 497 | } |