byteme
Macro to provide from() & into() implementations for a struct with either enums and positive integer field.
We have made the following assumptions about the fields of the struct:
- The fields are public.
- The fields will have the following types
u8u16u32u64u128usize[u8]- an enum
- For enum, we must attach a
#[byte_me($size)]attribute, where size is any of the positive integer types. - The enum declration must
#[derive(FromPrimitive)]from thenum-derivecrate.
The num-derive crate is required to generate the FromPrimitive trait for enums. Having said that, the same
functionality can be achieved using num-enum crate. It provides furthur control over the enum data types,
and might prove handy. here is the discussion
on the topic.
Example
use ByteMe;
pub use FromPrimitive;
;
let frame = FrameOne ;
let size = SIZE; // Get the number of bytes in the frame
let bytes: = frame.into; // Converts the frame into vector of bytes
let frame: FrameOne = bytes.into; // Converts the bytes back to frame