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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)