[−][src]Struct byte_consumer::ByteConsumer
A type that represents an object that consumes a memory slice on demand. The consumer lives as long as the slice is alive.
Methods
impl<'b> ByteConsumer<'b>
[src]
pub fn new(stream: &'b [u8]) -> ByteConsumer
[src]
Creates a new ByteConsumer from a stream of bytes.
pub fn consume(&mut self, nr_bytes: usize) -> Option<&'b [u8]>
[src]
Consumes nr_bytes
from the original slice and returns an Option
that holds a reference
to a memory slice which is nr_bytes
long. If nr_bytes
is greater than the remaining
bytes of the consumer's slice then None
is returned.
Note: The memory slice returned is not copied, it's a reference to the initial slice
Arguments
nr_bytes
- A number representing the number of bytes to consume
Examples
use byte_consumer::ByteConsumer; let mut consumer = ByteConsumer::new(&[10, 20, 30, 40]); let first_two = consumer.consume(2); let second_two = consumer.consume(2); assert!(first_two.is_some()); assert_eq!(first_two.unwrap(), [10, 20]); assert!(second_two.is_some()); assert_eq!(second_two.unwrap(), [30, 40]); assert_eq!(consumer.consume(1), None);
pub fn consume_unchecked(&mut self, nr_bytes: usize) -> &'b [u8]
[src]
Consumes nr_bytes
from the original slice and returns a reference to a memory slice which is nr_bytes
long.
Note: The memory slice returned is not copied, it's a reference to the initial slice
Arguments
nr_bytes
- A number representing the number of bytes to consume
Panics
If nr_bytes
is greater than the remaining bytes in the consumer's slice then
then it panics with a custom message.
Examples
use byte_consumer::ByteConsumer; let mut consumer = ByteConsumer::new(&[10, 20, 30, 40]); let first_two = consumer.consume_unchecked(2); let second_two = consumer.consume_unchecked(2); assert_eq!(first_two, [10, 20]); assert_eq!(second_two, [30, 40]); // let next = consumer.consume_unchecked(1) <-- This should panic.
pub fn consume_as<T: Copy>(&mut self) -> Option<T>
[src]
Consumes as many bytes as the size of type T
and converts them to an object of type T
.
If the size of type T
in number of bytes is greater than the remaining bytes in the consumer's buffer
then it returns None.
Note: The type T
must implement the Copy
trait as we are moving the bytes out of the slice
Examples
use byte_consumer::ByteConsumer; let mut consumer = ByteConsumer::new(&[0x1, 0x2, 0x1, 0x1]); let first_byte_as_u8: Option<u8> = consumer.consume_as(); let second_byte_as_u8 = consumer.consume_as::<u8>(); let last_two_as_u16 = consumer.consume_as::<u16>(); assert!(first_byte_as_u8.is_some()); assert_eq!(first_byte_as_u8.unwrap(), 1); assert!(second_byte_as_u8.is_some()); assert_eq!(second_byte_as_u8.unwrap(), 2); assert!(last_two_as_u16.is_some()); assert_eq!(last_two_as_u16.unwrap(), 257); assert_eq!(consumer.consume_as::<u32>(), None);
pub fn consume_as_unchecked<T: Copy>(&mut self) -> T
[src]
Consumes as many bytes as the size of type T
and converts them to an object of type T
.
Note: The type T
must implement the Copy
trait as we are moving the bytes out of the slice
Panics
If size of type T
in bytes is greater than the remaining bytes in the consumer's buffer
then it panics with a custom message.
Examples
use byte_consumer::ByteConsumer; let mut consumer = ByteConsumer::new(&[0x1, 0x2, 0x1, 0x1]); let first_byte_as_u8: u8 = consumer.consume_as_unchecked(); let second_byte_as_u8 = consumer.consume_as_unchecked::<u8>(); let last_two_as_u16 = consumer.consume_as_unchecked::<u16>(); assert_eq!(first_byte_as_u8, 1); assert_eq!(second_byte_as_u8, 2); assert_eq!(last_two_as_u16, 257); // let next = consumer.consume_as_unchecked::<u32>() <-- This should panic.
pub fn has_remaining(&mut self) -> bool
[src]
Returns whether there are remaining bytes to consume or not.
Examples
// Calling has_remaining on a consumer with an empty slice should return false. use byte_consumer::ByteConsumer; let mut consumer = ByteConsumer::new(&[]); assert!(!consumer.has_remaining());
// Calling has_remaining on a non consumed or partially consumed slice should return true. // After consuming everything, we should get false use byte_consumer::ByteConsumer; let mut consumer = ByteConsumer::new(&[1, 2, 3, 4]); assert!(consumer.has_remaining()); consumer.consume(3); assert!(consumer.has_remaining()); consumer.consume(1); assert!(!consumer.has_remaining());
Auto Trait Implementations
impl<'b> RefUnwindSafe for ByteConsumer<'b>
impl<'b> Send for ByteConsumer<'b>
impl<'b> Sync for ByteConsumer<'b>
impl<'b> Unpin for ByteConsumer<'b>
impl<'b> UnwindSafe for ByteConsumer<'b>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,