ckb_rust_std/io/
cursor.rs

1#[cfg(test)]
2mod tests;
3
4use alloc::boxed::Box;
5use alloc::string::String;
6use alloc::vec::Vec;
7
8use crate::io::prelude::*;
9use crate::io::{self, error::ErrorKind, BorrowedCursor, SeekFrom};
10use core::cmp;
11
12/// A `Cursor` wraps an in-memory buffer and provides it with a
13/// [`Seek`] implementation.
14///
15/// `Cursor`s are used with in-memory buffers, anything implementing
16/// <code>[AsRef]<\[u8]></code>, to allow them to implement [`Read`] and/or [`Write`],
17/// allowing these buffers to be used anywhere you might use a reader or writer
18/// that does actual I/O.
19///
20/// The standard library implements some I/O traits on various types which
21/// are commonly used as a buffer, like <code>Cursor<[Vec]\<u8>></code> and
22/// <code>Cursor<[&\[u8\]][bytes]></code>.
23///
24/// # Examples
25///
26/// We may want to write bytes to a [`File`] in our production
27/// code, but use an in-memory buffer in our tests. We can do this with
28/// `Cursor`:
29///
30/// [bytes]: crate::slice "slice"
31/// [`File`]: crate::fs::File
32///
33/// ```no_run
34/// use std::io::prelude::*;
35/// use std::io::{self, SeekFrom};
36/// use std::fs::File;
37///
38/// // a library function we've written
39/// fn write_ten_bytes_at_end<W: Write + Seek>(mut writer: W) -> io::Result<()> {
40///     writer.seek(SeekFrom::End(-10))?;
41///
42///     for i in 0..10 {
43///         writer.write(&[i])?;
44///     }
45///
46///     // all went well
47///     Ok(())
48/// }
49///
50/// # fn foo() -> io::Result<()> {
51/// // Here's some code that uses this library function.
52/// //
53/// // We might want to use a BufReader here for efficiency, but let's
54/// // keep this example focused.
55/// let mut file = File::create("foo.txt")?;
56/// // First, we need to allocate 10 bytes to be able to write into.
57/// file.set_len(10)?;
58///
59/// write_ten_bytes_at_end(&mut file)?;
60/// # Ok(())
61/// # }
62///
63/// // now let's write a test
64/// #[test]
65/// fn test_writes_bytes() {
66///     // setting up a real File is much slower than an in-memory buffer,
67///     // let's use a cursor instead
68///     use std::io::Cursor;
69///     let mut buff = Cursor::new(vec![0; 15]);
70///
71///     write_ten_bytes_at_end(&mut buff).unwrap();
72///
73///     assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
74/// }
75/// ```
76#[derive(Debug, Default, Eq, PartialEq)]
77pub struct Cursor<T> {
78    inner: T,
79    pos: u64,
80}
81
82impl<T> Cursor<T> {
83    /// Creates a new cursor wrapping the provided underlying in-memory buffer.
84    ///
85    /// Cursor initial position is `0` even if underlying buffer (e.g., [`Vec`])
86    /// is not empty. So writing to cursor starts with overwriting [`Vec`]
87    /// content, not with appending to it.
88    ///
89    /// # Examples
90    ///
91    /// ```
92    /// use std::io::Cursor;
93    ///
94    /// let buff = Cursor::new(Vec::new());
95    /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
96    /// # force_inference(&buff);
97    /// ```
98    pub const fn new(inner: T) -> Cursor<T> {
99        Cursor { pos: 0, inner }
100    }
101
102    /// Consumes this cursor, returning the underlying value.
103    ///
104    /// # Examples
105    ///
106    /// ```
107    /// use std::io::Cursor;
108    ///
109    /// let buff = Cursor::new(Vec::new());
110    /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
111    /// # force_inference(&buff);
112    ///
113    /// let vec = buff.into_inner();
114    /// ```
115    pub fn into_inner(self) -> T {
116        self.inner
117    }
118
119    /// Gets a reference to the underlying value in this cursor.
120    ///
121    /// # Examples
122    ///
123    /// ```
124    /// use std::io::Cursor;
125    ///
126    /// let buff = Cursor::new(Vec::new());
127    /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
128    /// # force_inference(&buff);
129    ///
130    /// let reference = buff.get_ref();
131    /// ```
132    pub const fn get_ref(&self) -> &T {
133        &self.inner
134    }
135
136    /// Gets a mutable reference to the underlying value in this cursor.
137    ///
138    /// Care should be taken to avoid modifying the internal I/O state of the
139    /// underlying value as it may corrupt this cursor's position.
140    ///
141    /// # Examples
142    ///
143    /// ```
144    /// use std::io::Cursor;
145    ///
146    /// let mut buff = Cursor::new(Vec::new());
147    /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
148    /// # force_inference(&buff);
149    ///
150    /// let reference = buff.get_mut();
151    /// ```
152    pub fn get_mut(&mut self) -> &mut T {
153        &mut self.inner
154    }
155
156    /// Returns the current position of this cursor.
157    ///
158    /// # Examples
159    ///
160    /// ```
161    /// use std::io::Cursor;
162    /// use std::io::prelude::*;
163    /// use std::io::SeekFrom;
164    ///
165    /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
166    ///
167    /// assert_eq!(buff.position(), 0);
168    ///
169    /// buff.seek(SeekFrom::Current(2)).unwrap();
170    /// assert_eq!(buff.position(), 2);
171    ///
172    /// buff.seek(SeekFrom::Current(-1)).unwrap();
173    /// assert_eq!(buff.position(), 1);
174    /// ```
175    pub const fn position(&self) -> u64 {
176        self.pos
177    }
178
179    /// Sets the position of this cursor.
180    ///
181    /// # Examples
182    ///
183    /// ```
184    /// use std::io::Cursor;
185    ///
186    /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
187    ///
188    /// assert_eq!(buff.position(), 0);
189    ///
190    /// buff.set_position(2);
191    /// assert_eq!(buff.position(), 2);
192    ///
193    /// buff.set_position(4);
194    /// assert_eq!(buff.position(), 4);
195    /// ```
196    pub fn set_position(&mut self, pos: u64) {
197        self.pos = pos;
198    }
199}
200
201impl<T> Cursor<T>
202where
203    T: AsRef<[u8]>,
204{
205    /// Returns the remaining slice.
206    ///
207    /// # Examples
208    ///
209    /// ```
210    /// #![feature(cursor_remaining)]
211    /// use std::io::Cursor;
212    ///
213    /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
214    ///
215    /// assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
216    ///
217    /// buff.set_position(2);
218    /// assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
219    ///
220    /// buff.set_position(4);
221    /// assert_eq!(buff.remaining_slice(), &[5]);
222    ///
223    /// buff.set_position(6);
224    /// assert_eq!(buff.remaining_slice(), &[]);
225    /// ```
226    pub fn remaining_slice(&self) -> &[u8] {
227        let len = self.pos.min(self.inner.as_ref().len() as u64);
228        &self.inner.as_ref()[(len as usize)..]
229    }
230
231    /// Returns `true` if the remaining slice is empty.
232    ///
233    /// # Examples
234    ///
235    /// ```
236    /// #![feature(cursor_remaining)]
237    /// use std::io::Cursor;
238    ///
239    /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
240    ///
241    /// buff.set_position(2);
242    /// assert!(!buff.is_empty());
243    ///
244    /// buff.set_position(5);
245    /// assert!(buff.is_empty());
246    ///
247    /// buff.set_position(10);
248    /// assert!(buff.is_empty());
249    /// ```
250    pub fn is_empty(&self) -> bool {
251        self.pos >= self.inner.as_ref().len() as u64
252    }
253}
254impl<T> Clone for Cursor<T>
255where
256    T: Clone,
257{
258    #[inline]
259    fn clone(&self) -> Self {
260        Cursor {
261            inner: self.inner.clone(),
262            pos: self.pos,
263        }
264    }
265    #[inline]
266    fn clone_from(&mut self, other: &Self) {
267        self.inner.clone_from(&other.inner);
268        self.pos = other.pos;
269    }
270}
271impl<T> io::Seek for Cursor<T>
272where
273    T: AsRef<[u8]>,
274{
275    fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
276        let (base_pos, offset) = match style {
277            SeekFrom::Start(n) => {
278                self.pos = n;
279                return Ok(n);
280            }
281            SeekFrom::End(n) => (self.inner.as_ref().len() as u64, n),
282            SeekFrom::Current(n) => (self.pos, n),
283        };
284        match base_pos.checked_add_signed(offset) {
285            Some(n) => {
286                self.pos = n;
287                Ok(self.pos)
288            }
289            None => Err(io::const_io_error!(
290                ErrorKind::InvalidInput,
291                "invalid seek to a negative or overflowing position",
292            )),
293        }
294    }
295
296    fn stream_len(&mut self) -> io::Result<u64> {
297        Ok(self.inner.as_ref().len() as u64)
298    }
299
300    fn stream_position(&mut self) -> io::Result<u64> {
301        Ok(self.pos)
302    }
303}
304impl<T> Read for Cursor<T>
305where
306    T: AsRef<[u8]>,
307{
308    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
309        let n = Read::read(&mut self.remaining_slice(), buf)?;
310        self.pos += n as u64;
311        Ok(n)
312    }
313
314    fn read_buf(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
315        let prev_written = cursor.written();
316
317        Read::read_buf(&mut self.remaining_slice(), cursor.reborrow())?;
318
319        self.pos += (cursor.written() - prev_written) as u64;
320
321        Ok(())
322    }
323    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
324        let result = Read::read_exact(&mut self.remaining_slice(), buf);
325
326        match result {
327            Ok(_) => self.pos += buf.len() as u64,
328            // The only possible error condition is EOF, so place the cursor at "EOF"
329            Err(_) => self.pos = self.inner.as_ref().len() as u64,
330        }
331
332        result
333    }
334
335    fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
336        let prev_written = cursor.written();
337
338        let result = Read::read_buf_exact(&mut self.remaining_slice(), cursor.reborrow());
339        self.pos += (cursor.written() - prev_written) as u64;
340
341        result
342    }
343
344    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
345        let content = self.remaining_slice();
346        let len = content.len();
347        buf.try_reserve(len)?;
348        buf.extend_from_slice(content);
349        self.pos += len as u64;
350
351        Ok(len)
352    }
353
354    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
355        let content =
356            alloc::str::from_utf8(self.remaining_slice()).map_err(|_| io::Error::INVALID_UTF8)?;
357        let len = content.len();
358        buf.try_reserve(len)?;
359        buf.push_str(content);
360        self.pos += len as u64;
361
362        Ok(len)
363    }
364}
365impl<T> BufRead for Cursor<T>
366where
367    T: AsRef<[u8]>,
368{
369    fn fill_buf(&mut self) -> io::Result<&[u8]> {
370        Ok(self.remaining_slice())
371    }
372    fn consume(&mut self, amt: usize) {
373        self.pos += amt as u64;
374    }
375}
376
377// Non-resizing write implementation
378#[inline]
379fn slice_write(pos_mut: &mut u64, slice: &mut [u8], buf: &[u8]) -> io::Result<usize> {
380    let pos = cmp::min(*pos_mut, slice.len() as u64);
381    let amt = (&mut slice[(pos as usize)..]).write(buf)?;
382    *pos_mut += amt as u64;
383    Ok(amt)
384}
385fn reserve_and_pad(pos_mut: &mut u64, vec: &mut Vec<u8>, buf_len: usize) -> io::Result<usize> {
386    let pos: usize = (*pos_mut).try_into().map_err(|_| {
387        io::const_io_error!(
388            ErrorKind::InvalidInput,
389            "cursor position exceeds maximum possible vector length",
390        )
391    })?;
392
393    // For safety reasons, we don't want these numbers to overflow
394    // otherwise our allocation won't be enough
395    let desired_cap = pos.saturating_add(buf_len);
396    if desired_cap > vec.capacity() {
397        // We want our vec's total capacity
398        // to have room for (pos+buf_len) bytes. Reserve allocates
399        // based on additional elements from the length, so we need to
400        // reserve the difference
401        vec.reserve(desired_cap - vec.len());
402    }
403    // Pad if pos is above the current len.
404    if pos > vec.len() {
405        let diff = pos - vec.len();
406        // Unfortunately, `resize()` would suffice but the optimiser does not
407        // realise the `reserve` it does can be eliminated. So we do it manually
408        // to eliminate that extra branch
409        let spare = vec.spare_capacity_mut();
410        debug_assert!(spare.len() >= diff);
411        // Safety: we have allocated enough capacity for this.
412        // And we are only writing, not reading
413        unsafe {
414            spare
415                .get_unchecked_mut(..diff)
416                .fill(core::mem::MaybeUninit::new(0));
417            vec.set_len(pos);
418        }
419    }
420
421    Ok(pos)
422}
423
424/// Writes the slice to the vec without allocating
425/// # Safety: vec must have buf.len() spare capacity
426unsafe fn vec_write_unchecked(pos: usize, vec: &mut Vec<u8>, buf: &[u8]) -> usize {
427    debug_assert!(vec.capacity() >= pos + buf.len());
428    unsafe { vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()) };
429    pos + buf.len()
430}
431
432/// Resizing write implementation for [`Cursor`]
433///
434/// Cursor is allowed to have a pre-allocated and initialised
435/// vector body, but with a position of 0. This means the [`Write`]
436/// will overwrite the contents of the vec.
437///
438/// This also allows for the vec body to be empty, but with a position of N.
439/// This means that [`Write`] will pad the vec with 0 initially,
440/// before writing anything from that point
441fn vec_write(pos_mut: &mut u64, vec: &mut Vec<u8>, buf: &[u8]) -> io::Result<usize> {
442    let buf_len = buf.len();
443    let mut pos = reserve_and_pad(pos_mut, vec, buf_len)?;
444
445    // Write the buf then progress the vec forward if necessary
446    // Safety: we have ensured that the capacity is available
447    // and that all bytes get written up to pos
448    unsafe {
449        pos = vec_write_unchecked(pos, vec, buf);
450        if pos > vec.len() {
451            vec.set_len(pos);
452        }
453    };
454
455    // Bump us forward
456    *pos_mut += buf_len as u64;
457    Ok(buf_len)
458}
459
460impl Write for Cursor<&mut [u8]> {
461    #[inline]
462    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
463        slice_write(&mut self.pos, self.inner, buf)
464    }
465    #[inline]
466    fn is_write_vectored(&self) -> bool {
467        true
468    }
469    #[inline]
470    fn flush(&mut self) -> io::Result<()> {
471        Ok(())
472    }
473}
474impl Write for Cursor<&mut Vec<u8>> {
475    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
476        vec_write(&mut self.pos, self.inner, buf)
477    }
478    #[inline]
479    fn is_write_vectored(&self) -> bool {
480        true
481    }
482    #[inline]
483    fn flush(&mut self) -> io::Result<()> {
484        Ok(())
485    }
486}
487impl Write for Cursor<Vec<u8>> {
488    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
489        vec_write(&mut self.pos, &mut self.inner, buf)
490    }
491    #[inline]
492    fn is_write_vectored(&self) -> bool {
493        true
494    }
495
496    #[inline]
497    fn flush(&mut self) -> io::Result<()> {
498        Ok(())
499    }
500}
501impl Write for Cursor<Box<[u8]>> {
502    #[inline]
503    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
504        slice_write(&mut self.pos, &mut self.inner, buf)
505    }
506    #[inline]
507    fn is_write_vectored(&self) -> bool {
508        true
509    }
510
511    #[inline]
512    fn flush(&mut self) -> io::Result<()> {
513        Ok(())
514    }
515}
516impl<const N: usize> Write for Cursor<[u8; N]> {
517    #[inline]
518    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
519        slice_write(&mut self.pos, &mut self.inner, buf)
520    }
521    #[inline]
522    fn is_write_vectored(&self) -> bool {
523        true
524    }
525
526    #[inline]
527    fn flush(&mut self) -> io::Result<()> {
528        Ok(())
529    }
530}