lib::ByteConsumer - Rust
Description: API documentation for the Rust `ByteConsumer` struct in crate `lib`.
Keywords: rust, rustlang, rust-lang, ByteConsumer

# [−][src]Struct lib::ByteConsumer

    pub struct ByteConsumer<'b> { /* fields omitted */ }

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.

# Arguments

- `nr_bytes` - A number representing the number of bytes to consume

Note: The memory slice returned is not copied, it's a reference to the initial slice

# Examples

    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.

# 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.

Note: The memory slice returned is not copied, it's a reference to the initial slice

# Examples

    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]

#### `pub fn consume_as_unchecked<T: Copy>(&mut self) -> T`[src]

## 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]

#### `fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `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]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.