Crate struct_deser [] [src]

Simple serialization and deserialization of structs.

The aim of this crate is to help with implementing simple cases of (de)serialization of structs where the whole deserialization only consists of copying fixed amounts of bytes in appropriate byte order. In other words, the cases which are sometimes handled by casting a (packed) struct to array and vice-versa.

This crate should be used with struct_deser-derive, to avoid writing boilerplate.


extern crate struct_deser;
extern crate struct_deser_derive;

// derive traits
#[derive(StructDeser, Debug, Eq, PartialEq)]
struct Packet {
    // mark as big endian
    // this is mandatory because u16 has multiple bytes
    version: u16,
    // u8 goes without endianess attribute
    ttl: u8,
    // mark as little endian
    chksum: u32,

fn main() {
    use struct_deser::{SerializedByteLen,FromBytes,IntoBytes};

    let packet0 = Packet {
        version: 1,
        ttl: 42,
        chksum: 47,

    let mut bytes = [0; Packet::BYTE_LEN];
    packet0.into_bytes(&mut bytes);
    let packet1 = Packet::from_bytes(&bytes);

    assert_eq!(packet0, packet1);



Re-exported essential items from byteorder crate. This is intended mostly for struct_deser-derive.



Represents types that can be constructed from bytes.


Represents types that can be constructed from bytes with specific endianess.


This trait can be used for marking specific implementation with a constant, which can be used for matching, when determinint the type of message. This doesn't influence derived (de)serialization in any way.


Represents types that can be serialized into bytes.


Represents types that can be serialized into bytes with specific endianess.


Defines length (number of bytes) of struct when serialized.