Trait deku::DekuRead

source ·
pub trait DekuRead<'a, Ctx = ()> {
    fn read(
        input: &'a BitSlice<u8, Msb0>,
        ctx: Ctx
    ) -> Result<(&'a BitSlice<u8, Msb0>, Self), DekuError>
    where
        Self: Sized
; }
Expand description

“Reader” trait: read bits and construct type

Required Methods§

Read bits and construct type

  • input - Input as bits
  • ctx - A context required by context-sensitive reading. A unit type () means no context needed.

Returns the remaining bits after parsing in addition to Self.

Implementations on Foreign Types§

wrapper around u8::read with consideration to context, such as bit size true if the result of the read is 1, false if 0 and error otherwise

Read a T from input and store as Some(T)

  • inner_ctx - The context required by T. It will be passed to every Ts when constructing.
Examples
let input = vec![1u8, 2, 3, 4];
let (rest, v) = Option::<u32>::read(input.view_bits(), Endian::Little).unwrap();
assert!(rest.is_empty());
assert_eq!(v, Some(0x04030201))

Read u8s until the given limit

  • limit - the limiting factor on the amount of u8s to read
  • inner_ctx - The context required by u8. It will be passed to every u8s when constructing.
Examples
let input = vec![1u8, 2, 3, 4];
let (rest, v) = <&[u8]>::read(input.view_bits(), (4.into(), Endian::Little)).unwrap();
assert!(rest.is_empty());
assert_eq!(&[1u8, 2, 3, 4], v)

NOP on read

Read Ts until the given limit

  • limit - the limiting factor on the amount of Ts to read
  • inner_ctx - The context required by T. It will be passed to every Ts when constructing.
Examples
let input = vec![1u8, 2, 3, 4];
let (rest, v) = Vec::<u32>::read(input.view_bits(), (1.into(), Endian::Little)).unwrap();
assert!(rest.is_empty());
assert_eq!(vec![0x04030201], v)

Read Ts until the given limit from input for types which don’t require context.

Read a T from input and store as Cow

Read K, Vs until the given limit

  • limit - the limiting factor on the amount of K, Vs to read
  • inner_ctx - The context required by K, V. It will be passed to every K, Vs when constructing.
Examples
let input: Vec<u8> = vec![100, 1, 2, 3, 4];
let (rest, map) = HashMap::<u8, u32>::read(input.view_bits(), (1.into(), Endian::Little)).unwrap();
assert!(rest.is_empty());
let mut expected = HashMap::<u8, u32>::default();
expected.insert(100, 0x04030201);
assert_eq!(expected, map)

Read K, Vs until the given limit from input for types which don’t require context.

Read Ts until the given limit

  • limit - the limiting factor on the amount of Ts to read
  • inner_ctx - The context required by T. It will be passed to every Ts when constructing.
Examples
let input = vec![1u8, 2, 3, 4];
let expected: HashSet<u32> = vec![0x04030201].into_iter().collect();
let (rest, set) = HashSet::<u32>::read(input.view_bits(), (1.into(), Endian::Little)).unwrap();
assert!(rest.is_empty());
assert_eq!(expected, set)

Read Ts until the given limit from input for types which don’t require context.

Read a T from input and store as Box

Read Ts until the given limit

Implementors§