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.

Example

extern crate struct_deser;
#[macro_use]
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
    #[be]
    version: u16,
    // u8 goes without endianess attribute
    ttl: u8,
    // mark as little endian
    #[le]
    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);
}

Modules

byteorder

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

Traits

FromBytes

Represents types that can be constructed from bytes.

FromBytesOrdered

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

Identifier

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.

IntoBytes

Represents types that can be serialized into bytes.

IntoBytesOrdered

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

SerializedByteLen

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