pub struct Cursor<T> { /* private fields */ }
Expand description
A Cursor
wraps an in-memory buffer and provides it with a
Seek
implementation.
This trait is similar to std::io::cursor::Cursor, but it is based on the embedded-io library, so there may be some differences compared to the std platform’s implementation. It is recommended to refer to the embedded-io documentation for more details.
Cursor
s 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.
§Examples
use embedded_io_extras::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.
§Examples
use embedded_io_extras::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.
§Examples
use embedded_io_extras::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.
§Examples
use embedded_io_extras::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.
§Examples
use embedded_io_extras::Cursor;
use embedded_io::{Seek, 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.
§Examples
use embedded_io_extras::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.
§Examples
use embedded_io_extras::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.
§Examples
use embedded_io_extras::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§impl<T> Read for Cursor<T>
impl<T> Read for Cursor<T>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
Source§fn read_exact(
&mut self,
buf: &mut [u8],
) -> Result<(), ReadExactError<Self::Error>>
fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
buf
. Read more