pub struct Stream<'a>(pub &'a [u8]);
Expand description
A stream of bytes from which values can be extracted.
Tuple Fields
0: &'a [u8]
Implementations
sourceimpl<'a> Stream<'a>
impl<'a> Stream<'a>
sourcepub fn extract<T, P>(&mut self, parameter: P) -> ExtractResult<'a, T>where
T: Extract<'a, P>,
pub fn extract<T, P>(&mut self, parameter: P) -> ExtractResult<'a, T>where
T: Extract<'a, P>,
Extracts bytes from this stream as a T
.
Example
let mut stream = Stream(&[1, 2]);
assert_eq!(stream.extract::<u8, _>(()), Ok(0x01));
assert_eq!(stream.extract::<u8, _>(()), Ok(0x02));
assert_eq!(stream.extract::<u8, _>(()), Err(ExtractError::Insufficient(1)));
sourcepub fn peek<T, P>(&mut self, parameter: P) -> ExtractResult<'a, T>where
T: Extract<'a, P>,
pub fn peek<T, P>(&mut self, parameter: P) -> ExtractResult<'a, T>where
T: Extract<'a, P>,
Returns bytes from this stream as a T
without extracting the bytes.
Example
let mut stream = Stream(&[1, 2, 3, 4]);
assert_eq!(stream.peek::<u16, _>(()), Ok(0x0102));
assert_eq!(stream.extract::<u16, _>(()), Ok(0x0102));
assert_eq!(stream.peek::<u16, _>(()), Ok(0x0304));
assert_eq!(stream.extract::<u16, _>(()), Ok(0x0304));
assert_eq!(stream.peek::<u16, _>(()), Err(ExtractError::Insufficient(2)));
assert_eq!(stream.extract::<u16, _>(()), Err(ExtractError::Insufficient(2)));
sourcepub fn skip(&mut self, n: usize)
pub fn skip(&mut self, n: usize)
Skips the supplied number of bytes in this stream.
Example
let mut stream = Stream(&[1, 2, 3, 4]);
stream.skip(2);
assert_eq!(stream.extract::<u16, _>(()), Ok(0x0304));
assert_eq!(stream.extract::<u16, _>(()), Err(ExtractError::Insufficient(2)));
sourcepub fn skip_while<F: Fn(u8) -> bool>(&mut self, f: F)
pub fn skip_while<F: Fn(u8) -> bool>(&mut self, f: F)
Skips bytes in this stream while the supplied condition is true for those bytes.
Example
let mut stream = Stream(&[1, 2, 3, 4]);
stream.skip_while(|b| b < 3);
assert_eq!(stream.extract::<u16, _>(()), Ok(0x0304));
assert_eq!(stream.extract::<u16, _>(()), Err(ExtractError::Insufficient(2)));
Trait Implementations
impl<'a> Copy for Stream<'a>
impl<'a> Eq for Stream<'a>
impl<'a> StructuralEq for Stream<'a>
impl<'a> StructuralPartialEq for Stream<'a>
Auto Trait Implementations
impl<'a> RefUnwindSafe for Stream<'a>
impl<'a> Send for Stream<'a>
impl<'a> Sync for Stream<'a>
impl<'a> Unpin for Stream<'a>
impl<'a> UnwindSafe for Stream<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
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