Struct Cursor

pub struct Cursor<T> { /* private fields */ }

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

impl<T> Cursor<T>

fn new(inner: T) -> Self

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());
# fn force_inference(_: &Cursor<Vec<u8>>) {}
# force_inference(&buff);
fn into_inner(self) -> T

Consumes this cursor, returning the underlying value.

Examples

use futures::io::Cursor;

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

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

Gets a reference to the underlying value in this cursor.

Examples

use futures::io::Cursor;

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

let reference = buff.get_ref();
fn get_mut(&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 futures::io::Cursor;

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

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

Returns the current position of this cursor.

Examples

# futures::executor::block_on(async {
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);
# Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
fn set_position(&mut self, pos: u64)

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

impl AsyncWrite for Cursor<&mut Vec<u8>>

fn poll_write(self: Pin<&mut Self>, &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
fn poll_write_vectored(self: Pin<&mut Self>, &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, &mut Context<'_>) -> Poll<Result<()>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

impl AsyncWrite for Cursor<&mut [u8]>

fn poll_write(self: Pin<&mut Self>, &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
fn poll_write_vectored(self: Pin<&mut Self>, &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, &mut Context<'_>) -> Poll<Result<()>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

impl AsyncWrite for Cursor<Box<[u8]>>

fn poll_write(self: Pin<&mut Self>, &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
fn poll_write_vectored(self: Pin<&mut Self>, &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, &mut Context<'_>) -> Poll<Result<()>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

impl AsyncWrite for Cursor<Vec<u8>>

fn poll_write(self: Pin<&mut Self>, &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
fn poll_write_vectored(self: Pin<&mut Self>, &mut Context<'_>, bufs: &[IoSlice<'_>]) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, &mut Context<'_>) -> Poll<Result<()>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

impl<T> AsyncBufRead for Cursor<T> where T: AsRef<[u8]> + Unpin,

fn poll_fill_buf(self: Pin<&mut Self>, &mut Context<'_>) -> Poll<Result<&[u8]>>
fn consume(self: Pin<&mut Self>, amt: usize)

impl<T> AsyncSeek for Cursor<T> where T: AsRef<[u8]> + Unpin,

fn poll_seek(self: Pin<&mut Self>, &mut Context<'_>, pos: SeekFrom) -> Poll<Result<u64>>

impl<T: AsRef<[u8]> + Unpin> AsyncRead for Cursor<T>

fn poll_read(self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<Result<usize>>
fn poll_read_vectored(self: Pin<&mut Self>, &mut Context<'_>, bufs: &mut [IoSliceMut<'_>]) -> Poll<Result<usize>>

impl<T: Clone> Clone for Cursor<T>

fn clone(&self) -> Self

impl<T: Debug> Debug for Cursor<T>

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

impl<T: Default> Default for Cursor<T>

fn default() -> Self

Auto Trait Implementations

impl<T> Freeze for Cursor<T> where Cursor<T>: Freeze,

impl<T> RefUnwindSafe for Cursor<T> where Cursor<T>: RefUnwindSafe,

impl<T> Send for Cursor<T> where Cursor<T>: Send,

impl<T> Sync for Cursor<T> where Cursor<T>: Sync,

impl<T> Unpin for Cursor<T> where Cursor<T>: Unpin,

impl<T> UnsafeUnpin for Cursor<T> where Cursor<T>: UnsafeUnpin,

impl<T> UnwindSafe for Cursor<T> where Cursor<T>: UnwindSafe,

Blanket Implementations

impl<R> AsyncBufReadExt for Cursor<T> where R: AsyncBufRead + ?Sized,

impl<R> AsyncReadExt for Cursor<T> where R: AsyncRead + ?Sized,

impl<S> AsyncSeekExt for Cursor<T> where S: AsyncSeek + ?Sized,

impl<T> Any for Cursor<T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Cursor<T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Cursor<T> where T: ?Sized,

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

impl<T> CloneToUninit for Cursor<T> where T: Clone,

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

impl<T> From<T> for Cursor<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Cursor<T> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Cursor<T> where U: From<T>,

fn into(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<U> for Cursor<T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Cursor<T> where U: TryFrom<T>,

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

impl<W> AsyncWriteExt for Cursor<T> where W: AsyncWrite + ?Sized,