pub struct Stream<'s>(pub &'s [u8]);
Expand description
A stream of bytes from which values can be extracted.
Tuple Fields
0: &'s [u8]
Implementations
sourceimpl<'s> Stream<'s>
impl<'s> Stream<'s>
sourcepub fn extract<T, P>(&mut self, parameter: P) -> ExtractResult<'s, T>where
T: Extract<'s, P>,
pub fn extract<T, P>(&mut self, parameter: P) -> ExtractResult<'s, T>where
T: Extract<'s, 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(Insufficient(1)));
sourcepub fn peek<T, P>(&mut self, parameter: P) -> ExtractResult<'s, T>where
T: Extract<'s, P>,
pub fn peek<T, P>(&mut self, parameter: P) -> ExtractResult<'s, T>where
T: Extract<'s, 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(Insufficient(2)));
assert_eq!(stream.extract::<u16, _>(()), Err(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(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(Insufficient(2)));
Trait Implementations
impl<'s> Copy for Stream<'s>
impl<'s> Eq for Stream<'s>
impl<'s> StructuralEq for Stream<'s>
impl<'s> StructuralPartialEq for Stream<'s>
Auto Trait Implementations
impl<'s> RefUnwindSafe for Stream<'s>
impl<'s> Send for Stream<'s>
impl<'s> Sync for Stream<'s>
impl<'s> Unpin for Stream<'s>
impl<'s> UnwindSafe for Stream<'s>
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