Struct bin_layout::Cursor
source · [−]pub struct Cursor<T> {
pub data: T,
pub offset: usize,
}Fields
data: Toffset: usizeImplementations
sourceimpl<T: Bytes> Cursor<T>
impl<T: Bytes> Cursor<T>
sourcepub fn write_slice(&mut self, slice: impl AsRef<[u8]>)
pub fn write_slice(&mut self, slice: impl AsRef<[u8]>)
Writes a slice into the data view.
Examples
use bin_layout::{Cursor, ErrorKind};
let mut view = Cursor::new(vec![0; 3]);
view.write_slice([4, 2]);
assert_eq!(view.offset, 2);Panics
This function may panic, if the data is slice &mut [u8], and has not enough capacity.
But If the data is vector Vec<u8>, then it may reserve extra capacity if necessary.
sourceimpl<'de> Cursor<&'de [u8]>
impl<'de> Cursor<&'de [u8]>
sourcepub fn remaining_slice(&self) -> &'de [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn remaining_slice(&self) -> &'de [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Returns remaining slice from the current offset. It doesn’t change the offset.
Examples
use bin_layout::Cursor;
let mut view = Cursor::new([1, 2].as_ref());
assert_eq!(view.remaining_slice(), &[1, 2]);
view.offset = 42;
assert!(view.remaining_slice().is_empty());sourcepub fn read_slice<E: Error>(&mut self, len: usize) -> Result<&'de [u8], E>
pub fn read_slice<E: Error>(&mut self, len: usize) -> Result<&'de [u8], E>
Read slice from the current offset.
Example
use bin_layout::{Cursor, ErrorKind};
let mut view = Cursor::new([1, 2, 3].as_ref());
let slice: Result<_, ()> = view.read_slice(2);
assert_eq!(slice, Ok([1, 2].as_ref()));
let slice: Result<_, ErrorKind> = view.read_slice(3);
assert_eq!(slice, Err(ErrorKind::InsufficientBytes));Trait Implementations
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more