Skip to main content

Cursor

Struct Cursor 

Source
pub struct Cursor<T> { /* private fields */ }
Available on crate feature std only.
Expand description

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]>.

Implementations§

Source§

impl<T> Cursor<T>

Source

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

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

pub fn into_inner(self) -> T

Consumes this cursor, returning the underlying value.

§Examples
use async_std::io::Cursor;

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

let vec = buff.into_inner();
Source

pub fn get_ref(&self) -> &T

Gets a reference to the underlying value in this cursor.

§Examples
use async_std::io::Cursor;

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

let reference = buff.get_ref();
Source

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

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

let reference = buff.get_mut();
Source

pub fn position(&self) -> u64

Returns the current position of this cursor.

§Examples
use async_std::io::Cursor;
use async_std::io::prelude::*;
use async_std::io::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);
Source

pub fn set_position(&mut self, pos: u64)

Sets the position of this cursor.

§Examples
use async_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);

Trait Implementations§

Source§

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

Source§

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

Available on crate feature docs only.
Returns the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
Source§

fn consume(self: Pin<&mut Self>, amt: usize)

Available on crate feature docs only.
Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to read.
Source§

fn read_until<'a>( &'a mut self, byte: u8, buf: &'a mut Vec<u8>, ) -> ImplFuture<usize>
where Self: Unpin,

Available on crate feature docs only.
Reads all bytes into buf until the delimiter byte or EOF is reached. Read more
Source§

fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Reads all bytes and appends them into buf until a newline (the 0xA byte) is reached. Read more
Source§

fn lines(self) -> Lines<Self>
where Self: Unpin + Sized,

Available on crate feature docs only.
Returns a stream over the lines of this byte stream. Read more
Source§

fn split(self, byte: u8) -> Split<Self>
where Self: Sized,

Available on crate feature docs only.
Returns a stream over the contents of this reader split on the byte byte. Read more
Source§

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

Source§

fn clone(&self) -> Cursor<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Source§

fn default() -> Cursor<T>

Returns the “default value” for a type. Read more
Source§

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

Source§

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

Available on crate feature docs only.
Attempt to read from the AsyncRead into buf.
Source§

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

Available on crate feature docs only.
Attempt to read from the AsyncRead into bufs using vectored IO operations.
Source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Reads some bytes from the byte stream. Read more
Source§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>], ) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Like read, except that it reads into a slice of buffers. Read more
Source§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8>, ) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Reads all bytes from the byte stream. Read more
Source§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String, ) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Reads all bytes from the byte stream and appends them into a string. Read more
Source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Reads the exact number of bytes required to fill buf. Read more
Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Available on crate feature docs only.
Creates an adaptor which will read at most limit bytes from it. Read more
Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Available on crate feature docs only.
Creates a “by reference” adaptor for this instance of Read. Read more
Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Available on crate feature docs only.
Transforms this Read instance to a Stream over its bytes. Read more
Source§

fn chain<R: Read>(self, next: R) -> Chain<Self, R>
where Self: Sized,

Available on crate feature docs only.
Creates an adaptor which will chain this stream with another. Read more
Source§

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

Source§

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

Available on crate feature docs only.
Attempt to seek to an offset, in bytes, in a stream.
Source§

fn seek(&mut self, pos: SeekFrom) -> ImplFuture<Result<u64>>
where Self: Unpin,

Available on crate feature docs only.
Seeks to a new position in a byte stream. Read more
Source§

impl Write for Cursor<&mut [u8]>

Source§

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

Available on crate feature docs only.
Attempt to write bytes from buf into the object.
Source§

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

Available on crate feature docs only.
Attempt to write bytes from bufs into the object using vectored IO operations.
Source§

fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>>

Available on crate feature docs only.
Attempt to flush the object, ensuring that any buffered data reach their destination.
Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Available on crate feature docs only.
Attempt to close the object.
Source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Writes some bytes into the byte stream. Read more
Source§

fn flush(&mut self) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Flushes the stream to ensure that all buffered contents reach their destination. Read more
Source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>], ) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Like write, except that it writes from a slice of buffers. Read more
Source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Writes an entire buffer into the byte stream. Read more
Source§

fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Writes a formatted string into this writer, returning any error encountered. Read more
Source§

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

Source§

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

Available on crate feature docs only.
Attempt to write bytes from buf into the object.
Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Available on crate feature docs only.
Attempt to close the object.
Source§

fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>>

Available on crate feature docs only.
Attempt to flush the object, ensuring that any buffered data reach their destination.
Source§

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

Available on crate feature docs only.
Attempt to write bytes from bufs into the object using vectored IO operations.
Source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Writes some bytes into the byte stream. Read more
Source§

fn flush(&mut self) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Flushes the stream to ensure that all buffered contents reach their destination. Read more
Source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>], ) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Like write, except that it writes from a slice of buffers. Read more
Source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Writes an entire buffer into the byte stream. Read more
Source§

fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Writes a formatted string into this writer, returning any error encountered. Read more
Source§

impl Write for Cursor<Vec<u8>>

Source§

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

Available on crate feature docs only.
Attempt to write bytes from buf into the object.
Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Available on crate feature docs only.
Attempt to close the object.
Source§

fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<()>>

Available on crate feature docs only.
Attempt to flush the object, ensuring that any buffered data reach their destination.
Source§

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

Available on crate feature docs only.
Attempt to write bytes from bufs into the object using vectored IO operations.
Source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Writes some bytes into the byte stream. Read more
Source§

fn flush(&mut self) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Flushes the stream to ensure that all buffered contents reach their destination. Read more
Source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>], ) -> ImplFuture<Result<usize>>
where Self: Unpin,

Available on crate feature docs only.
Like write, except that it writes from a slice of buffers. Read more
Source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Writes an entire buffer into the byte stream. Read more
Source§

fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> ImplFuture<Result<()>>
where Self: Unpin,

Available on crate feature docs only.
Writes a formatted string into this writer, returning any error encountered. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> BufReadExt for T
where T: BufRead + ?Sized,

Source§

fn read_until<'a>( &'a mut self, byte: u8, buf: &'a mut Vec<u8>, ) -> ReadUntilFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Reads all bytes into buf until the delimiter byte or EOF is reached. Read more
Source§

fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Reads all bytes and appends them into buf until a newline (the 0xA byte) is reached. Read more
Source§

fn lines(self) -> Lines<Self>
where Self: Unpin + Sized,

Available on crate feature std only.
Returns a stream over the lines of this byte stream. Read more
Source§

fn split(self, byte: u8) -> Split<Self>
where Self: Sized,

Available on crate feature std only.
Returns a stream over the contents of this reader split on the byte byte. Read more
Source§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ReadExt for T
where T: Read + ?Sized,

Source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Reads some bytes from the byte stream. Read more
Source§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>], ) -> ReadVectoredFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Like read, except that it reads into a slice of buffers. Read more
Source§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8>, ) -> ReadToEndFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Reads all bytes from the byte stream. Read more
Source§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String, ) -> ReadToStringFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Reads all bytes from the byte stream and appends them into a string. Read more
Source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Reads the exact number of bytes required to fill buf. Read more
Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Available on crate feature std only.
Creates an adaptor which will read at most limit bytes from it. Read more
Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Available on crate feature std only.
Creates a “by reference” adaptor for this instance of Read. Read more
Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Available on crate feature std only.
Transforms this Read instance to a Stream over its bytes. Read more
Source§

fn chain<R: Read>(self, next: R) -> Chain<Self, R>
where Self: Sized,

Available on crate feature std only.
Creates an adaptor which will chain this stream with another. Read more
Source§

impl<T> SeekExt for T
where T: Seek + ?Sized,

Source§

fn seek(&mut self, pos: SeekFrom) -> SeekFuture<'_, Self>
where Self: Unpin,

Available on crate feature std only.
Seeks to a new position in a byte stream. Read more
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T> WriteExt for T
where T: Write + ?Sized,

Source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Writes some bytes into the byte stream. Read more
Source§

fn flush(&mut self) -> FlushFuture<'_, Self>
where Self: Unpin,

Available on crate feature std only.
Flushes the stream to ensure that all buffered contents reach their destination. Read more
Source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>], ) -> WriteVectoredFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Like write, except that it writes from a slice of buffers. Read more
Source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Writes an entire buffer into the byte stream. Read more
Source§

fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> WriteFmtFuture<'a, Self>
where Self: Unpin,

Available on crate feature std only.
Writes a formatted string into this writer, returning any error encountered. Read more