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§
source§impl<T> Cursor<T>
impl<T> Cursor<T>
sourcepub fn new(inner: T) -> Cursor<T>
pub fn new(inner: T) -> Cursor<T>
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()
.
§Examples
use futures_lite::io::Cursor;
let cursor = Cursor::new(Vec::<u8>::new());
sourcepub fn get_ref(&self) -> &T
pub fn get_ref(&self) -> &T
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();
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
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();
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
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]);
sourcepub fn position(&self) -> u64
pub fn position(&self) -> u64
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);
sourcepub fn set_position(&mut self, pos: u64)
pub fn set_position(&mut self, pos: u64)
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§
source§impl<T> AsyncBufRead for Cursor<T>
impl<T> AsyncBufRead for Cursor<T>
source§impl<T> AsyncRead for Cursor<T>
impl<T> AsyncRead for Cursor<T>
source§impl AsyncWrite for Cursor<&mut [u8]>
impl AsyncWrite for Cursor<&mut [u8]>
source§fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, _: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
Attempt to write bytes from
buf
into the object. Read moresource§fn poll_write_vectored(
self: Pin<&mut Self>,
_: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize>>
fn poll_write_vectored( self: Pin<&mut Self>, _: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>
Attempt to write bytes from
bufs
into the object using vectored
IO operations. Read moresource§impl AsyncWrite for Cursor<&mut Vec<u8>>
impl AsyncWrite for Cursor<&mut Vec<u8>>
source§fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, _: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
Attempt to write bytes from
buf
into the object. Read moresource§fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Attempt to close the object. Read more
source§impl AsyncWrite for Cursor<Vec<u8>>
impl AsyncWrite for Cursor<Vec<u8>>
source§fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, _: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
Attempt to write bytes from
buf
into the object. Read moresource§fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
Attempt to close the object. 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> UnwindSafe for Cursor<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
source§fn fill_buf(&mut self) -> FillBuf<'_, Self> ⓘwhere
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self> ⓘwhere
Self: Unpin,
Returns the contents of the internal buffer, filling it with more data if empty. Read more
source§fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'_, Self> ⓘwhere
Self: Unpin,
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntilFuture<'_, Self> ⓘwhere
Self: Unpin,
source§fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'_, Self> ⓘwhere
Self: Unpin,
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'_, Self> ⓘwhere
Self: Unpin,
Reads all bytes and appends them into
buf
until a newline (the 0xA byte) or EOF is found. Read moresource§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> ⓘwhere
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> ⓘwhere
Self: Unpin,
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,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> ⓘwhere
Self: Unpin,
source§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>
) -> ReadToEndFuture<'a, Self> ⓘwhere
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>
) -> ReadToEndFuture<'a, Self> ⓘwhere
Self: Unpin,
source§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> ⓘwhere
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> ⓘwhere
Self: Unpin,
source§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> ⓘwhere
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> ⓘwhere
Self: Unpin,
Reads the exact number of bytes required to fill
buf
. Read moresource§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
Creates an adapter which will read at most
limit
bytes from it. Read moresource§impl<S> AsyncSeekExt for S
impl<S> AsyncSeekExt for S
source§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
source§fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self> ⓘwhere
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self> ⓘwhere
Self: Unpin,
Writes some bytes into the byte stream. Read more
source§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self> ⓘwhere
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self> ⓘwhere
Self: Unpin,
source§fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self> ⓘwhere
Self: Unpin,
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self> ⓘwhere
Self: Unpin,
Writes an entire buffer into the byte stream. Read more
source§fn flush(&mut self) -> FlushFuture<'_, Self> ⓘwhere
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self> ⓘwhere
Self: Unpin,
Flushes the stream to ensure that all buffered contents reach their destination. Read more
source§fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
Boxes the writer and changes its type to
dyn AsyncWrite + Send + 'a
. Read moresource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more