Function from_bytes

Source
pub fn from_bytes<T>(bytes: &[u8]) -> Result<(T, usize)>
where T: BinaryParse,
Expand description

Deserializes a binary slice into a value of type T using the default configuration.

§Default Configuration

  • Optional Strategy: Tagged (uses a single byte to indicate Some or None)
  • Endianness: Little-endian
  • Limit: No size limit
  • Container Length: 4 bytes (used to decode the length of sequences, strings, etc.)

§Parameters

  • bytes: The binary slice to deserialize. Must represent a valid serialized value of type T.

§Returns

  • Ok((T, usize)): The deserialized value and the number of bytes read.
  • Err(Error): If deserialization fails or the input is invalid.

§Example

use binja::{from_bytes, BinaryParse};

#[derive(BinaryParse, PartialEq, Debug)]
struct Example {
    field1: u32,
    field2: Option<u32>,
}

let bytes = vec![0x2A, 0x0, 0x0, 0x0, 0x1, 0x7, 0x0, 0x0, 0x0];
let (value, size): (Example, usize) = from_bytes(&bytes).unwrap();
assert_eq!(
    value,
    Example {
        field1: 42,
        field2: Some(7),
    }
);
assert_eq!(size, 0);