Derive Macro Parse

#[derive(Parse)]
Expand description

Derives implementation of the Parse trait.

use veecle_os_data_support_someip::parse::{Parse, ParseExt};

#[derive(Debug, PartialEq, Parse)]
struct SomeIpStruct {
    foo: u16,
    bar: u32,
}

let bytes = &[0x0, 0x6, 0x1, 0x2, 0x3, 0x4];
let deserialized = SomeIpStruct {
    foo: 6,
    bar: 0x1020304,
};

let my_struct = SomeIpStruct::parse(bytes).unwrap();
assert_eq!(my_struct, deserialized);

It can also derive implementations for structs with a single lifetime parameter:

use veecle_os_data_support_someip::parse::{ByteReader, Parse, ParseError, ParseExt};

#[derive(Debug)]
struct Foo;

impl<'a> Parse<'a> for &'a Foo {
    fn parse_partial(reader: &mut ByteReader<'a>) -> Result<Self, ParseError> {
        Ok(&Foo)
    }
}

#[derive(Debug, Parse)]
struct WithLifetimeDerived<'foo> {
    inner: &'foo Foo,
}

assert!(WithLifetimeDerived::parse(&[]).is_ok());

Zero sized types and tuple structs can be derived as well.

use veecle_os_data_support_someip::parse::Parse;

#[derive(Parse)]
struct Zst;

#[derive(Parse)]
struct TupleStruct(u32, u16);

It cannot be derived for enums, unions, or structs with more than one lifetime.

use veecle_os_data_support_someip::parse::{Parse};

#[derive(Parse)]
enum Bad {}

#[derive(Parse)]
union AlsoBad {
  foo: u8,
  bar: u8,
}

#[derive(Parse)]
struct Lively<'a, 'b> {
  foo: PhantomData<(&'a (), &'b ())>,
}