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

DecoderBufferMut is a panic-free, mutable byte buffer for decoding untrusted input

Implementations

Create a new DecoderBufferMut from a byte slice

Freeze the mutable buffer into a DecoderBuffer

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

Mutably borrow 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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::new(&mut data);
let (value, buffer) = buffer.decode_with_len_prefix::<u8, u16>().unwrap();
assert_eq!(value, 1);
assert_eq!(buffer, [2, 3][..])

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

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

Decode a parameterized value of type T implementing DecoderParameterizedValueMut

Skip a count of bytes, discarding the bytes

let mut data = [0, 1, 2, 3, 4];
let buffer = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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 = DecoderBufferMut::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

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

Performs the conversion.

Performs the conversion.

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 !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

Performs the conversion.

Performs the conversion.

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.