Struct tarrasque::Stream

source ·
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

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)));

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)));

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)));

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.