1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright (c) The buf-list Contributors
// SPDX-License-Identifier: Apache-2.0

#[cfg(feature = "futures03")]
mod futures_imp;
#[cfg(test)]
mod tests;
#[cfg(feature = "tokio1")]
mod tokio_imp;

use crate::{errors::ReadExactError, BufList};
use bytes::Bytes;
use std::{
    cmp::Ordering,
    io::{self, IoSliceMut, SeekFrom},
};

/// A `Cursor` wraps an in-memory `BufList` and provides it with a [`Seek`] implementation.
///
/// `Cursor`s allow `BufList`s to implement [`Read`] and [`BufRead`], allowing a `BufList` to be
/// used anywhere you might use a reader or writer that does actual I/O.
///
/// The cursor may either own or borrow a `BufList`: both `Cursor<BufList>` and `Cursor<&BufList>`
/// are supported.
///
/// # Optional features
///
/// * `tokio1`: With this feature enabled, [`Cursor`] implements the `tokio` crate's
///   [`AsyncSeek`](tokio::io::AsyncSeek), [`AsyncRead`](tokio::io::AsyncRead) and
///   [`AsyncBufRead`](tokio::io::AsyncBufRead).
/// * `futures03`: With this feature enabled, [`Cursor`] implements the `futures` crate's
///   [`AsyncSeek`](futures_io_03::AsyncSeek), [`AsyncRead`](futures_io_03::AsyncRead) and
///   [`AsyncBufRead`](futures_io_03::AsyncBufRead).
///
/// [`Read`]: std::io::Read
/// [`BufRead`]: std::io::BufRead
/// [`Seek`]: std::io::Seek
pub struct Cursor<T> {
    inner: T,

    /// Data associated with the cursor.
    data: CursorData,
}

impl<T: AsRef<BufList>> Cursor<T> {
    /// Creates a new cursor wrapping the provided `BufList`.
    ///
    /// # Examples
    ///
    /// ```
    /// use buf_list::{BufList, Cursor};
    ///
    /// let cursor = Cursor::new(BufList::new());
    /// ```
    pub fn new(inner: T) -> Cursor<T> {
        let data = CursorData::new();
        Cursor { inner, data }
    }

    /// Consumes this cursor, returning the underlying value.
    ///
    /// # Examples
    ///
    /// ```
    /// use buf_list::{BufList, Cursor};
    ///
    /// let cursor = Cursor::new(BufList::new());
    ///
    /// let vec = cursor.into_inner();
    /// ```
    pub fn into_inner(self) -> T {
        self.inner
    }

    /// Gets a reference to the underlying value in this cursor.
    ///
    /// # Examples
    ///
    /// ```
    /// use buf_list::{BufList, Cursor};
    ///
    /// let cursor = Cursor::new(BufList::new());
    ///
    /// let reference = cursor.get_ref();
    /// ```
    #[cfg(const_fn_trait_bounds)]
    pub const fn get_ref(&self) -> &T {
        &self.inner
    }

    /// Gets a reference to the underlying value in this cursor.
    ///
    /// # Examples
    ///
    /// ```
    /// use buf_list::{BufList, Cursor};
    ///
    /// let cursor = Cursor::new(BufList::new());
    ///
    /// let reference = cursor.get_ref();
    /// ```
    #[cfg(not(const_fn_trait_bounds))]
    pub fn get_ref(&self) -> &T {
        &self.inner
    }

    /// Returns the current position of this cursor.
    ///
    /// # Examples
    ///
    /// ```
    /// use buf_list::{BufList, Cursor};
    /// use std::io::prelude::*;
    /// use std::io::SeekFrom;
    ///
    /// let mut cursor = Cursor::new(BufList::from(&[1, 2, 3, 4, 5][..]));
    ///
    /// assert_eq!(cursor.position(), 0);
    ///
    /// cursor.seek(SeekFrom::Current(2)).unwrap();
    /// assert_eq!(cursor.position(), 2);
    ///
    /// cursor.seek(SeekFrom::Current(-1)).unwrap();
    /// assert_eq!(cursor.position(), 1);
    /// ```
    #[cfg(const_fn_trait_bounds)]
    pub const fn position(&self) -> u64 {
        self.data.pos
    }

    /// Returns the current position of this cursor.
    ///
    /// # Examples
    ///
    /// ```
    /// use buf_list::{BufList, Cursor};
    /// use std::io::prelude::*;
    /// use std::io::SeekFrom;
    ///
    /// let mut cursor = Cursor::new(BufList::from(&[1, 2, 3, 4, 5][..]));
    ///
    /// assert_eq!(cursor.position(), 0);
    ///
    /// cursor.seek(SeekFrom::Current(2)).unwrap();
    /// assert_eq!(cursor.position(), 2);
    ///
    /// cursor.seek(SeekFrom::Current(-1)).unwrap();
    /// assert_eq!(cursor.position(), 1);
    /// ```
    #[cfg(not(const_fn_trait_bounds))]
    pub fn position(&self) -> u64 {
        self.data.pos
    }

    /// Sets the position of this cursor.
    ///
    /// # Examples
    ///
    /// ```
    /// use buf_list::{BufList, Cursor};
    ///
    /// let mut cursor = Cursor::new(BufList::from(&[1, 2, 3, 4, 5][..]));
    ///
    /// assert_eq!(cursor.position(), 0);
    ///
    /// cursor.set_position(2);
    /// assert_eq!(cursor.position(), 2);
    ///
    /// cursor.set_position(4);
    /// assert_eq!(cursor.position(), 4);
    /// ```
    pub fn set_position(&mut self, pos: u64) {
        self.data.set_pos(self.inner.as_ref(), pos);
    }

    // ---
    // Helper methods
    // ---
    #[cfg(test)]
    fn assert_invariants(&self) -> anyhow::Result<()> {
        self.data.assert_invariants(self.inner.as_ref())
    }
}

impl<T> Clone for Cursor<T>
where
    T: Clone,
{
    #[inline]
    fn clone(&self) -> Self {
        Cursor {
            inner: self.inner.clone(),
            data: self.data.clone(),
        }
    }

    #[inline]
    fn clone_from(&mut self, other: &Self) {
        self.inner.clone_from(&other.inner);
        self.data = other.data.clone();
    }
}

impl<T: AsRef<BufList>> io::Seek for Cursor<T> {
    fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
        self.data.seek_impl(self.inner.as_ref(), style)
    }

    #[cfg(seek_convenience)]
    fn stream_position(&mut self) -> io::Result<u64> {
        Ok(self.data.pos)
    }
}

impl<T: AsRef<BufList>> io::Read for Cursor<T> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        Ok(self.data.read_impl(self.inner.as_ref(), buf))
    }

    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
        Ok(self.data.read_vectored_impl(self.inner.as_ref(), bufs))
    }

    // TODO: is_read_vectored once that's available on stable Rust.

    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
        self.data.read_exact_impl(self.inner.as_ref(), buf)
    }
}

impl<T: AsRef<BufList>> io::BufRead for Cursor<T> {
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        Ok(self.data.fill_buf_impl(self.inner.as_ref()))
    }

    fn consume(&mut self, amt: usize) {
        self.data.consume_impl(self.inner.as_ref(), amt);
    }
}

#[derive(Clone, Debug)]
struct CursorData {
    /// The chunk number the cursor is pointing to. Kept in sync with pos.
    ///
    /// This is within the range [0, self.start_pos.len()). It is self.start_pos.len() - 1 iff pos
    /// is greater than list.num_bytes().
    chunk: usize,

    /// The overall position in the stream. Kept in sync with chunk.
    pos: u64,
}

impl CursorData {
    fn new() -> Self {
        Self { chunk: 0, pos: 0 }
    }

    #[cfg(test)]
    fn assert_invariants(&self, list: &BufList) -> anyhow::Result<()> {
        use anyhow::ensure;

        ensure!(
            self.pos >= list.get_start_pos()[self.chunk],
            "invariant failed: current position {} >= start position {} (chunk = {})",
            self.pos,
            list.get_start_pos()[self.chunk],
            self.chunk
        );

        let next_pos = list.get_start_pos().get(self.chunk + 1).copied().into();
        ensure!(
            Offset::Value(self.pos) < next_pos,
            "invariant failed: next start position {:?} > current position {} (chunk = {})",
            next_pos,
            self.pos,
            self.chunk
        );

        Ok(())
    }

    fn seek_impl(&mut self, list: &BufList, style: SeekFrom) -> io::Result<u64> {
        let (base_pos, offset) = match style {
            SeekFrom::Start(n) => {
                self.set_pos(list, n);
                return Ok(n);
            }
            SeekFrom::End(n) => (self.num_bytes(list), n),
            SeekFrom::Current(n) => (self.pos, n),
        };
        // Can't use checked_add_signed since it was only stabilized in Rust 1.66. This is adapted
        // from
        // https://github.com/rust-lang/rust/blame/ed937594d3/library/std/src/io/cursor.rs#L295-L299.
        let new_pos = if offset >= 0 {
            base_pos.checked_add(offset as u64)
        } else {
            base_pos.checked_sub(offset.wrapping_neg() as u64)
        };
        match new_pos {
            Some(n) => {
                self.set_pos(list, n);
                Ok(self.pos)
            }
            None => Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "invalid seek to a negative or overflowing position",
            )),
        }
    }

    fn read_impl(&mut self, list: &BufList, buf: &mut [u8]) -> usize {
        // Read as much as possible until we fill up the buffer.
        let mut buf_pos = 0;
        while buf_pos < buf.len() {
            let (chunk, chunk_pos) = match self.get_chunk_and_pos(list) {
                Some(value) => value,
                None => break,
            };
            // The number of bytes to copy is the smaller of the two:
            // - the length of the chunk - the position in it.
            // - the number of bytes remaining, which is buf.len() - buf_pos.
            let n_to_copy = (chunk.len() - chunk_pos).min(buf.len() - buf_pos);
            let chunk_bytes = chunk.as_ref();

            let bytes_to_copy = &chunk_bytes[chunk_pos..(chunk_pos + n_to_copy)];
            let dest = &mut buf[buf_pos..(buf_pos + n_to_copy)];
            dest.copy_from_slice(bytes_to_copy);
            buf_pos += n_to_copy;

            // Increment the position.
            self.pos += n_to_copy as u64;
            // If we've finished reading through the chunk, move to the next chunk.
            if n_to_copy == chunk.len() - chunk_pos {
                self.chunk += 1;
            }
        }

        buf_pos
    }

    fn read_vectored_impl(&mut self, list: &BufList, bufs: &mut [IoSliceMut<'_>]) -> usize {
        let mut nread = 0;
        for buf in bufs {
            // Copy data from the buffer until we run out of bytes to copy.
            let n = self.read_impl(list, buf);
            nread += n;
            if n < buf.len() {
                break;
            }
        }
        nread
    }

    fn read_exact_impl(&mut self, list: &BufList, buf: &mut [u8]) -> io::Result<()> {
        // This is the same as read_impl as long as there's enough space.
        let remaining = self.num_bytes(list).saturating_sub(self.pos);
        let buf_len = buf.len();
        if remaining < buf_len as u64 {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                ReadExactError { remaining, buf_len },
            ));
        }

        self.read_impl(list, buf);
        Ok(())
    }

    fn fill_buf_impl<'a>(&'a self, list: &'a BufList) -> &[u8] {
        const EMPTY_SLICE: &[u8] = &[];
        match self.get_chunk_and_pos(list) {
            Some((chunk, chunk_pos)) => &chunk.as_ref()[chunk_pos..],
            // An empty return value means the end of the buffer has been reached.
            None => EMPTY_SLICE,
        }
    }

    fn consume_impl(&mut self, list: &BufList, amt: usize) {
        self.set_pos(list, self.pos + amt as u64);
    }

    fn set_pos(&mut self, list: &BufList, new_pos: u64) {
        match new_pos.cmp(&self.pos) {
            Ordering::Greater => {
                let start_pos = list.get_start_pos();
                let next_start = start_pos.get(self.chunk + 1).copied().into();
                if Offset::Value(new_pos) < next_start {
                    // Within the same chunk.
                } else {
                    // The above check ensures that we're not currently pointing to the last index
                    // (since it would have returned Eof, which is greater than Offset(n) for any
                    // n).
                    //
                    // Do a binary search for this element.
                    match start_pos[self.chunk + 1..].binary_search(&new_pos) {
                        // We're starting the search from self.chunk + 1, which means that the value
                        // returned from binary_search is 1 less than the actual delta.
                        Ok(delta_minus_one) => {
                            // Exactly at the start point of a chunk.
                            self.chunk += 1 + delta_minus_one;
                        }
                        // The value returned in the error case (not at the start point of a chunk)
                        // is (delta - 1) + 1, so just delta.
                        Err(delta) => {
                            debug_assert!(
                                delta > 0,
                                "delta must be at least 1 since we already \
                                checked the same chunk (self.chunk = {})",
                                self.chunk,
                            );
                            self.chunk += delta;
                        }
                    }
                }
            }
            Ordering::Equal => {}
            Ordering::Less => {
                let start_pos = list.get_start_pos();
                if start_pos.get(self.chunk).copied() <= Some(new_pos) {
                    // Within the same chunk.
                } else {
                    match start_pos[..self.chunk].binary_search(&new_pos) {
                        Ok(chunk) => {
                            // Exactly at the start point of a chunk.
                            self.chunk = chunk;
                        }
                        Err(chunk_plus_1) => {
                            debug_assert!(
                                chunk_plus_1 > 0,
                                "chunk_plus_1 must be at least 1 since self.start_pos[0] is 0 \
                                 (self.chunk = {})",
                                self.chunk,
                            );
                            self.chunk = chunk_plus_1 - 1;
                        }
                    }
                }
            }
        }
        self.pos = new_pos;
    }

    #[inline]
    fn get_chunk_and_pos<'b>(&self, list: &'b BufList) -> Option<(&'b Bytes, usize)> {
        match list.get_chunk(self.chunk) {
            Some(chunk) => {
                // This guarantees that pos is not past the end of the list.
                debug_assert!(
                    self.pos < self.num_bytes(list),
                    "self.pos ({}) is less than num_bytes ({})",
                    self.pos,
                    self.num_bytes(list)
                );
                Some((
                    chunk,
                    (self.pos - list.get_start_pos()[self.chunk]) as usize,
                ))
            }
            None => {
                // pos is past the end of the list.
                None
            }
        }
    }

    fn num_bytes(&self, list: &BufList) -> u64 {
        *list
            .get_start_pos()
            .last()
            .expect("start_pos always has at least one element")
    }
}

/// This is the same as Option<T> except Offset and Eof are reversed in ordering, i.e. Eof >
/// Offset(T) for any T.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
enum Offset<T> {
    Value(T),
    Eof,
}

impl<T> From<Option<T>> for Offset<T> {
    fn from(value: Option<T>) -> Self {
        match value {
            Some(v) => Self::Value(v),
            None => Self::Eof,
        }
    }
}