Struct Cursor

struct Cursor<T> { ... }

A Cursor wraps an in-memory buffer and provides it with a Seek implementation.

Cursors are used with in-memory buffers, anything implementing [AsRef]<[u8]>, to allow them to implement Read and/or Write, allowing these buffers to be used anywhere you might use a reader or writer that does actual I/O.

The standard library implements some I/O traits on various types which are commonly used as a buffer, like Cursor<[Vec]<u8>> and Cursor<&[u8]>.

Examples

We may want to write bytes to a File in our production code, but use an in-memory buffer in our tests. We can do this with Cursor:

use std::io::prelude::*;
use std::io::{self, SeekFrom};
use std::fs::File;

// a library function we've written
fn write_ten_bytes_at_end<W: Write + Seek>(mut writer: W) -> io::Result<()> {
    writer.seek(SeekFrom::End(-10))?;

    for i in 0..10 {
        writer.write(&[i])?;
    }

    // all went well
    Ok(())
}

# fn foo() -> io::Result<()> {
// Here's some code that uses this library function.
//
// We might want to use a BufReader here for efficiency, but let's
// keep this example focused.
let mut file = File::create("foo.txt")?;
// First, we need to allocate 10 bytes to be able to write into.
file.set_len(10)?;

write_ten_bytes_at_end(&mut file)?;
# Ok(())
# }

// now let's write a test
#[test]
fn test_writes_bytes() {
    // setting up a real File is much slower than an in-memory buffer,
    // let's use a cursor instead
    use std::io::Cursor;
    let mut buff = Cursor::new(vec![0; 15]);

    write_ten_bytes_at_end(&mut buff).unwrap();

    assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}

Implementations

impl<T> Cursor<T>

fn split(self: &Self) -> (&[u8], &[u8])

Splits the underlying slice at the cursor position and returns them.

Examples

#![feature(cursor_split)]
use std::io::Cursor;

let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);

assert_eq!(buff.split(), ([].as_slice(), [1, 2, 3, 4, 5].as_slice()));

buff.set_position(2);
assert_eq!(buff.split(), ([1, 2].as_slice(), [3, 4, 5].as_slice()));

buff.set_position(6);
assert_eq!(buff.split(), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()));

impl<T> Cursor<T>

const fn new(inner: T) -> Cursor<T>

Creates a new cursor wrapping the provided underlying in-memory buffer.

Cursor initial position is 0 even if underlying buffer (e.g., Vec) is not empty. So writing to cursor starts with overwriting Vec content, not with appending to it.

Examples

use std::io::Cursor;

let buff = Cursor::new(Vec::new());
# fn force_inference(_: &Cursor<Vec<u8>>) {}
# force_inference(&buff);
fn into_inner(self: Self) -> T

Consumes this cursor, returning the underlying value.

Examples

use std::io::Cursor;

let buff = Cursor::new(Vec::new());
# fn force_inference(_: &Cursor<Vec<u8>>) {}
# force_inference(&buff);

let vec = buff.into_inner();
const fn get_ref(self: &Self) -> &T

Gets a reference to the underlying value in this cursor.

Examples

use std::io::Cursor;

let buff = Cursor::new(Vec::new());
# fn force_inference(_: &Cursor<Vec<u8>>) {}
# force_inference(&buff);

let reference = buff.get_ref();
const fn get_mut(self: &mut Self) -> &mut T

Gets a mutable reference to the underlying value in this cursor.

Care should be taken to avoid modifying the internal I/O state of the underlying value as it may corrupt this cursor's position.

Examples

use std::io::Cursor;

let mut buff = Cursor::new(Vec::new());
# fn force_inference(_: &Cursor<Vec<u8>>) {}
# force_inference(&buff);

let reference = buff.get_mut();
const fn position(self: &Self) -> u64

Returns the current position of this cursor.

Examples

use std::io::Cursor;
use std::io::prelude::*;
use std::io::SeekFrom;

let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);

assert_eq!(buff.position(), 0);

buff.seek(SeekFrom::Current(2)).unwrap();
assert_eq!(buff.position(), 2);

buff.seek(SeekFrom::Current(-1)).unwrap();
assert_eq!(buff.position(), 1);
const fn set_position(self: &mut Self, pos: u64)

Sets the position of this cursor.

Examples

use std::io::Cursor;

let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);

assert_eq!(buff.position(), 0);

buff.set_position(2);
assert_eq!(buff.position(), 2);

buff.set_position(4);
assert_eq!(buff.position(), 4);

impl<T> Cursor<T>

fn split_mut(self: &mut Self) -> (&mut [u8], &mut [u8])

Splits the underlying slice at the cursor position and returns them mutably.

Examples

#![feature(cursor_split)]
use std::io::Cursor;

let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);

assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));

buff.set_position(2);
assert_eq!(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice()));

buff.set_position(6);
assert_eq!(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice()));

impl Write for Cursor<&mut [u8]>

fn write(self: &mut Self, buf: &[u8]) -> Result<usize>
fn write_vectored(self: &mut Self, bufs: &[IoSlice<'_>]) -> Result<usize>
fn is_write_vectored(self: &Self) -> bool
fn write_all(self: &mut Self, buf: &[u8]) -> Result<()>
fn write_all_vectored(self: &mut Self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn flush(self: &mut Self) -> Result<()>

impl<A> Write for Cursor<&mut Vec<u8, A>>

fn write(self: &mut Self, buf: &[u8]) -> Result<usize>
fn write_vectored(self: &mut Self, bufs: &[IoSlice<'_>]) -> Result<usize>
fn is_write_vectored(self: &Self) -> bool
fn write_all(self: &mut Self, buf: &[u8]) -> Result<()>
fn write_all_vectored(self: &mut Self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn flush(self: &mut Self) -> Result<()>

impl<A> Write for Cursor<Box<[u8], A>>

fn write(self: &mut Self, buf: &[u8]) -> Result<usize>
fn write_vectored(self: &mut Self, bufs: &[IoSlice<'_>]) -> Result<usize>
fn is_write_vectored(self: &Self) -> bool
fn write_all(self: &mut Self, buf: &[u8]) -> Result<()>
fn write_all_vectored(self: &mut Self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn flush(self: &mut Self) -> Result<()>

impl<A> Write for Cursor<Vec<u8, A>>

fn write(self: &mut Self, buf: &[u8]) -> Result<usize>
fn write_vectored(self: &mut Self, bufs: &[IoSlice<'_>]) -> Result<usize>
fn is_write_vectored(self: &Self) -> bool
fn write_all(self: &mut Self, buf: &[u8]) -> Result<()>
fn write_all_vectored(self: &mut Self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn flush(self: &mut Self) -> Result<()>

impl<N: usize> Write for Cursor<[u8; N]>

fn write(self: &mut Self, buf: &[u8]) -> Result<usize>
fn write_vectored(self: &mut Self, bufs: &[IoSlice<'_>]) -> Result<usize>
fn is_write_vectored(self: &Self) -> bool
fn write_all(self: &mut Self, buf: &[u8]) -> Result<()>
fn write_all_vectored(self: &mut Self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn flush(self: &mut Self) -> Result<()>

impl<T> Any for Cursor<T>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Cursor<T>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Cursor<T>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> BufRead for Cursor<T>

fn fill_buf(self: &mut Self) -> Result<&[u8]>
fn consume(self: &mut Self, amt: usize)

impl<T> Clone for Cursor<T>

fn clone(self: &Self) -> Self
fn clone_from(self: &mut Self, other: &Self)

impl<T> CloneToUninit for Cursor<T>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> Freeze for Cursor<T>

impl<T> From for Cursor<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Read for Cursor<T>

fn read(self: &mut Self, buf: &mut [u8]) -> Result<usize>
fn read_buf(self: &mut Self, cursor: BorrowedCursor<'_>) -> Result<()>
fn read_vectored(self: &mut Self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn is_read_vectored(self: &Self) -> bool
fn read_exact(self: &mut Self, buf: &mut [u8]) -> Result<()>
fn read_buf_exact(self: &mut Self, cursor: BorrowedCursor<'_>) -> Result<()>
fn read_to_end(self: &mut Self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_string(self: &mut Self, buf: &mut String) -> Result<usize>

impl<T> RefUnwindSafe for Cursor<T>

impl<T> Seek for Cursor<T>

fn seek(self: &mut Self, style: SeekFrom) -> Result<u64>
fn stream_len(self: &mut Self) -> Result<u64>
fn stream_position(self: &mut Self) -> Result<u64>

impl<T> Send for Cursor<T>

impl<T> StructuralPartialEq for Cursor<T>

impl<T> Sync for Cursor<T>

impl<T> ToOwned for Cursor<T>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> Unpin for Cursor<T>

impl<T> UnsafeUnpin for Cursor<T>

impl<T> UnwindSafe for Cursor<T>

impl<T, U> Into for Cursor<T>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Cursor<T>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Cursor<T>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>

impl<T: $crate::cmp::Eq> Eq for Cursor<T>

impl<T: $crate::cmp::PartialEq> PartialEq for Cursor<T>

fn eq(self: &Self, other: &Cursor<T>) -> bool

impl<T: $crate::default::Default> Default for Cursor<T>

fn default() -> Cursor<T>

impl<T: $crate::fmt::Debug> Debug for Cursor<T>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result