Struct futures::io::Cursor

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

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

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

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

Implementations§

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 futures::io::Cursor;

let buff = Cursor::new(Vec::new());

Consumes this cursor, returning the underlying value.

Examples
use futures::io::Cursor;

let buff = Cursor::new(Vec::new());

let vec = buff.into_inner();

Gets a reference to the underlying value in this cursor.

Examples
use futures::io::Cursor;

let buff = Cursor::new(Vec::new());

let reference = buff.get_ref();

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 futures::io::Cursor;

let mut buff = Cursor::new(Vec::new());

let reference = buff.get_mut();

Returns the current position of this cursor.

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

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

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

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

buff.seek(SeekFrom::Current(-1)).await?;
assert_eq!(buff.position(), 1);

Sets the position of this cursor.

Examples
use futures::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);

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 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 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 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
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
Creates a future which will wait for a non-empty buffer to be available from this I/O object or EOF to be reached. Read more
A convenience for calling AsyncBufRead::consume on Unpin IO types. Read more
Creates a future which will read all the bytes associated with this I/O object into buf until the delimiter byte or EOF is reached. This method is the async equivalent to BufRead::read_until. Read more
Creates a future which will read all the bytes associated with this I/O object into buf until a newline (the 0xA byte) or EOF is reached, This method is the async equivalent to BufRead::read_line. Read more
Returns a stream over the lines of this reader. This method is the async equivalent to BufRead::lines. Read more
Creates an adaptor which will chain this stream with another. Read more
Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more
Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more
Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner. Read more
Creates a future which will read all the bytes from this AsyncRead. Read more
Creates a future which will read all the bytes from this AsyncRead. Read more
Helper method for splitting this read/write object into two halves. Read more
Creates an AsyncRead adapter which will read at most limit bytes from the underlying reader. Read more
Creates a future which will seek an IO object, and then yield the new position in the object and the object itself. Read more
Creates a future which will return the current seek position from the start of the stream. Read more
Creates a future which will entirely flush this AsyncWrite. Read more
Creates a future which will entirely close this AsyncWrite.
Creates a future which will write bytes from buf into the object. Read more
Creates a future which will write bytes from bufs into the object using vectored IO operations. Read more
Write data into this object. Read more
Allow using an AsyncWrite as a Sink<Item: AsRef<[u8]>>. 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.