Struct futures_lite::io::Cursor

source ·
pub struct Cursor<T> { /* private fields */ }
Expand description

Gives an in-memory buffer a cursor for reading and writing.

Examples

use futures_lite::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt, Cursor, SeekFrom};

let mut bytes = b"hello".to_vec();
let mut cursor = Cursor::new(&mut bytes);

// Overwrite 'h' with 'H'.
cursor.write_all(b"H").await?;

// Move the cursor one byte forward.
cursor.seek(SeekFrom::Current(1)).await?;

// Read a byte.
let mut byte = [0];
cursor.read_exact(&mut byte).await?;
assert_eq!(&byte, b"l");

// Check the final buffer.
assert_eq!(bytes, b"Hello");

Implementations§

Creates a cursor for an in-memory buffer.

Cursor’s initial position is 0 even if the underlying buffer is not empty. Writing using Cursor will overwrite the existing contents unless the cursor is moved to the end of the buffer using set_position() or [seek()][AsyncSeekExt::seek()].

Examples
use futures_lite::io::Cursor;

let cursor = Cursor::new(Vec::<u8>::new());

Gets a reference to the underlying buffer.

Examples
use futures_lite::io::Cursor;

let cursor = Cursor::new(Vec::<u8>::new());
let r = cursor.get_ref();

Gets a mutable reference to the underlying buffer.

Examples
use futures_lite::io::Cursor;

let mut cursor = Cursor::new(Vec::<u8>::new());
let r = cursor.get_mut();

Unwraps the cursor, returning the underlying buffer.

Examples
use futures_lite::io::Cursor;

let cursor = Cursor::new(vec![1, 2, 3]);
assert_eq!(cursor.into_inner(), [1, 2, 3]);

Returns the current position of this cursor.

Examples
use futures_lite::io::{AsyncSeekExt, Cursor, SeekFrom};

let mut cursor = Cursor::new(b"hello");
assert_eq!(cursor.position(), 0);

cursor.seek(SeekFrom::Start(2)).await?;
assert_eq!(cursor.position(), 2);

Sets the position of this cursor.

Examples
use futures_lite::io::Cursor;

let mut cursor = Cursor::new(b"hello");
assert_eq!(cursor.position(), 0);

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

Trait Implementations§

Attempt to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to poll_read. Read more
Attempt to read from the AsyncRead into buf. Read more
Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
Attempt to seek to an offset, in bytes, in a stream. Read more
Attempt to write bytes from buf into the object. Read more
Attempt to write bytes from bufs into the object using vectored IO operations. Read more
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Attempt to close the object. Read more
Attempt to write bytes from buf into the object. Read more
Attempt to close the object. Read more
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Attempt to write bytes from bufs into the object using vectored IO operations. Read more
Attempt to write bytes from buf into the object. Read more
Attempt to close the object. Read more
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Attempt to write bytes from bufs into the object using vectored IO operations. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Returns the contents of the internal buffer, filling it with more data if empty. Read more
Consumes amt buffered bytes. Read more
Reads all bytes and appends them into buf until the delimiter byte or EOF is found. Read more
Reads all bytes and appends them into buf until a newline (the 0xA byte) or EOF is found. Read more
Returns a stream over the lines of this byte stream. Read more
Returns a stream over the contents of this reader split on the specified byte. Read more
Reads some bytes from the byte stream. Read more
Like read(), except it reads into a slice of buffers. Read more
Reads the entire contents and appends them to a Vec. Read more
Reads the entire contents and appends them to a String. Read more
Reads the exact number of bytes required to fill buf. Read more
Creates an adapter which will read at most limit bytes from it. Read more
Converts this AsyncRead into a Stream of bytes. Read more
Creates an adapter which will chain this stream with another. Read more
Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. Read more
Seeks to a new position in a byte stream. Read more
Writes some bytes into the byte stream. Read more
Like write(), except that it writes a slice of buffers. Read more
Writes an entire buffer into the byte stream. Read more
Flushes the stream to ensure that all buffered contents reach their destination. Read more
Closes the writer. Read more
Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.