pub struct DecoderBuffer<'a> { /* private fields */ }
Expand description

DecoderBuffer is a panic-free byte buffer for look-ahead decoding untrusted input

Implementations

Create a new DecoderBuffer from a byte slice

Move out the buffer’s slice. This should be used with caution, as it removes any panic protection this struct provides.

Decode a slice of bytes by count, removing the slice from the current buffer

let mut data = [0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);

let (slice, buffer) = buffer.decode_slice(5).unwrap();
assert_eq!(slice, [0u8, 1, 2, 3, 4][..]);

assert!(buffer.is_empty());

Decode a value of type T, splitting the data from the current buffer

let mut data = [0, 1, 2, 3, 4, 5, 6];
let buffer = DecoderBuffer::new(&mut data);

let (value, buffer) = buffer.decode::<u8>().unwrap();
assert_eq!(value, 0);

let (value, buffer) = buffer.decode::<u16>().unwrap();
assert_eq!(value, 258);

let (value, buffer) = buffer.decode::<u32>().unwrap();
assert_eq!(value, 50_595_078);

assert!(buffer.is_empty());

Decode a slice prefixed by type Length, splitting the data from the current buffer. With a Length as encoded u8:

let mut data = [5, 0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);
let (slice, buffer) = buffer.decode_slice_with_len_prefix::<u8>().unwrap();
assert_eq!(slice, [0u8, 1, 2, 3, 4][..]);
assert!(buffer.is_empty())

With a Length as encoded u16:

let mut data = [0, 5, 0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);
let (slice, buffer) = buffer.decode_slice_with_len_prefix::<u16>().unwrap();
assert_eq!(slice, [0u8, 1, 2, 3, 4][..]);
assert!(buffer.is_empty())

Decode a value of type T prefixed by type Length, splitting the data from the current buffer. With a Length as encoded u8 and T as u16:

let mut data = [2, 0, 1, 2, 3];
let buffer = DecoderBuffer::new(&mut data);
let (value, buffer) = buffer.decode_with_len_prefix::<u8, u16>().unwrap();
assert_eq!(value, 1);
assert_eq!(buffer, [2, 3][..])

The DecoderValue implementation of T must consume the entire subslice otherwise an error will be returned.

let mut data = [3, 0, 1, 2];
let buffer = DecoderBuffer::new(&mut data);
let result = buffer.decode_with_len_prefix::<u8, u16>();
assert!(result.is_err())

Decode a parameterized value of type T implementing DecoderParameterizedValue

Skip a count of bytes, discarding the bytes

let mut data = [0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);
let buffer = buffer.skip(3).unwrap();
assert_eq!(buffer, [3, 4][..]);

Skip a number of bytes encoded as a length prefix of type Length With a Length encoded as u8:

let mut data = [5, 0, 1, 2, 3, 4];
let buffer = DecoderBuffer::new(&mut data);
let buffer = buffer.skip_with_len_prefix::<u8>().unwrap();
assert!(buffer.is_empty());

Skip a count of bytes, returning a CheckedRange for later access

Skip a number of bytes encoded as a length prefix of type Length into a CheckedRange for later access

Reads data from a CheckedRange

Create a peeking DecoderBuffer from the current buffer view

let mut data = [0, 1];
let buffer = DecoderBuffer::new(&mut data);

let peek = buffer.peek();
let (value, peek) = peek.decode::<u16>().unwrap();
assert_eq!(value, 1);
assert!(peek.is_empty());

// `buffer` still contains the previous view
assert_eq!(buffer, [0, 1][..]);

Returns a single byte at index

let mut data = [0, 1, 2];
let buffer = DecoderBuffer::new(&mut data);

assert_eq!(buffer.peek_byte(0).unwrap(), 0);
assert_eq!(buffer.peek_byte(1).unwrap(), 1);
assert_eq!(buffer.peek_byte(2).unwrap(), 2);

// `buffer` has not changed
assert_eq!(buffer, [0, 1, 2][..]);

Returns a PeekBuffer by range

Returns an error if the buffer is not empty.

let mut data = [1];
let buffer = DecoderBuffer::new(&mut data);

assert!(buffer.ensure_empty().is_err());
let (_, buffer) = buffer.decode::<u8>().unwrap();
assert!(buffer.ensure_empty().is_ok());

Returns an error if the buffer does not have at least len bytes.

let mut data = [0, 1, 2];
let buffer = DecoderBuffer::new(&mut data);

assert!(buffer.ensure_len(2).is_ok());
assert!(buffer.ensure_len(5).is_err());

Returns the number of bytes in the buffer.

let mut data = [0, 1, 2];
let buffer = DecoderBuffer::new(&mut data);

assert_eq!(buffer.len(), 3);
let (_, buffer) = buffer.decode::<u8>().unwrap();
assert_eq!(buffer.len(), 2);

Returns true if the buffer has a length of 0.

let mut data = [1];
let buffer = DecoderBuffer::new(&mut data);

assert!(!buffer.is_empty());
let (_, buffer) = buffer.decode::<u8>().unwrap();
assert!(buffer.is_empty());

Borrows the buffer’s slice. This should be used with caution, as it removes any panic protection this struct provides.

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
Encodes the value into the encoder
Returns the encoding size with no buffer constrains
Returns the encoding size for the given encoder’s capacity
Encodes the value into the encoder, while potentially mutating the value itself
Encodes the value into the encoder with a prefix of Len
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. 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
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
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. 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 resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
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.