Function from_bytes_with_config

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

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

§Parameters

  • bytes: The binary slice to deserialize. Must represent a valid serialized value of type T.
  • config: The Config specifying deserialization settings (endianness, optional strategy, etc.).

§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_with_config, BinaryParse};
use binja::config::{Config, EndiannessStrategy, OptionalStrategy};

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

let config = Config {
    endianness_strategy: EndiannessStrategy::Big,
    optional_strategy: OptionalStrategy::Tagged,
    ..Default::default()
};

let bytes = vec![0x00, 0x00, 0x00, 0x2A, 0x01, 0x00, 0x00, 0x00, 0x07];
let (value, size): (Example, usize) = from_bytes_with_config(&bytes, config).unwrap();
assert_eq!(
    value,
    Example {
        field1: 42,
        field2: Some(7),
    }
);
assert_eq!(size, 0);