pub struct Cursor<T> { /* private fields */ }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]][bytes]>.
Implementations§
Source§impl<T> Cursor<T>
impl<T> Cursor<T>
Sourcepub const fn new(inner: T) -> Cursor<T>
pub const 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.
§Example code
use portable_io::Cursor;
let buff = Cursor::new(Vec::new());Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this cursor, returning the underlying value.
§Example code
use portable_io::Cursor;
let buff = Cursor::new(Vec::new());
let vec = buff.into_inner();Sourcepub const fn get_ref(&self) -> &T
pub const fn get_ref(&self) -> &T
Gets a reference to the underlying value in this cursor.
§Example code
use portable_io::Cursor;
let buff = Cursor::new(Vec::new());
let reference = buff.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 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.
§Example code
use portable_io::Cursor;
let mut buff = Cursor::new(Vec::new());
let reference = buff.get_mut();Sourcepub const fn position(&self) -> u64
pub const fn position(&self) -> u64
Returns the current position of this cursor.
§Example code
use portable_io::prelude::*;
use portable_io::Cursor;
use portable_io::SeekFrom;
let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
assert_eq!(buff.position(), 0);
buff.seek(SeekFrom::Current(2)).unwrap();
assert_eq!(buff.position(), 2);
buff.seek(SeekFrom::Current(-1)).unwrap();
assert_eq!(buff.position(), 1);Sourcepub fn set_position(&mut self, pos: u64)
pub fn set_position(&mut self, pos: u64)
Sets the position of this cursor.
§Example code
use portable_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);Source§impl<T> Cursor<T>
impl<T> Cursor<T>
Sourcepub fn remaining_slice(&self) -> &[u8]
pub fn remaining_slice(&self) -> &[u8]
Returns the remaining slice.
§Example code
use portable_io::Cursor;
let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
buff.set_position(2);
assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
buff.set_position(4);
assert_eq!(buff.remaining_slice(), &[5]);
buff.set_position(6);
assert_eq!(buff.remaining_slice(), &[]);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the remaining slice is empty.
§Example code
use portable_io::Cursor;
let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
buff.set_position(2);
assert!(!buff.is_empty());
buff.set_position(5);
assert!(buff.is_empty());
buff.set_position(10);
assert!(buff.is_empty());Trait Implementations§
Source§impl<T> BufRead for Cursor<T>
impl<T> BufRead for Cursor<T>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
Source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt bytes have been consumed from the buffer,
so they should no longer be returned in calls to read. Read moreSource§fn has_data_left(&mut self) -> Result<bool>
fn has_data_left(&mut self) -> Result<bool>
Read has any data left to be read. Read moreSource§fn read_line(&mut self, buf: &mut String) -> Result<usize>
fn read_line(&mut self, buf: &mut String) -> Result<usize>
0xA byte) is reached, and append
them to the provided buffer. Read moreSource§impl<T> Read for Cursor<T>
impl<T> Read for Cursor<T>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Source§fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<()>
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<()>
Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
buf. Read moreSource§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf. Read moreSource§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
buf. Read moreSource§fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<()>
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<()>
buf. Read moreSource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more