pub struct Cursor<'a> { /* private fields */ }Expand description
A no-std compatible Cursor for reading from byte slices.
Wraps a &[u8] and tracks a read position, implementing both
sync::Read and sync::Seek (when the sync feature is enabled).
§Example
use hadris_io::{Cursor, Read, Seek, SeekFrom};
let data = [1u8, 2, 3, 4, 5];
let mut cursor = Cursor::new(&data);
let mut buf = [0u8; 2];
cursor.read_exact(&mut buf).unwrap();
assert_eq!(buf, [1, 2]);
cursor.seek(SeekFrom::Start(0)).unwrap();
cursor.read_exact(&mut buf).unwrap();
assert_eq!(buf, [1, 2]);Implementations§
Source§impl<'a> Cursor<'a>
impl<'a> Cursor<'a>
Sourcepub fn new(data: &'a [u8]) -> Self
pub fn new(data: &'a [u8]) -> Self
Creates a new cursor wrapping the given byte slice, starting at position 0.
use hadris_io::Cursor;
let data = [1, 2, 3];
let cursor = Cursor::new(&data);
assert_eq!(cursor.position(), 0);
assert_eq!(cursor.get_ref().len(), 3);Sourcepub fn position(&self) -> usize
pub fn position(&self) -> usize
Returns the current byte offset within the underlying data.
use hadris_io::Cursor;
let mut cursor = Cursor::new(&[0u8; 10]);
assert_eq!(cursor.position(), 0);
cursor.set_position(5);
assert_eq!(cursor.position(), 5);Sourcepub fn set_position(&mut self, pos: usize)
pub fn set_position(&mut self, pos: usize)
Sets the cursor position to the given byte offset.
use hadris_io::Cursor;
let mut cursor = Cursor::new(&[0u8; 10]);
cursor.set_position(7);
assert_eq!(cursor.position(), 7);Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Cursor<'a>
impl<'a> RefUnwindSafe for Cursor<'a>
impl<'a> Send for Cursor<'a>
impl<'a> Sync for Cursor<'a>
impl<'a> Unpin for Cursor<'a>
impl<'a> UnsafeUnpin for Cursor<'a>
impl<'a> UnwindSafe for Cursor<'a>
Blanket Implementations§
Source§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