Trait uavcan::Serializable[][src]

pub trait Serializable {
    const BIT_LENGTH_MIN: usize;
    const FLATTENED_FIELDS_NUMBER: usize;

    fn serialize(
        &self,
        flattened_field: &mut usize,
        bit: &mut usize,
        last_field: bool,
        buffer: &mut SerializationBuffer
    ) -> SerializationResult;
fn deserialize(
        &mut self,
        flattened_field: &mut usize,
        bit: &mut usize,
        last_field: bool,
        buffer: &mut DeserializationBuffer
    ) -> DeserializationResult; }

The trait that needs to be implemented for all types that will be sent over Uavcan

The (de)serialization is based on flattening all structures to primitive fields The serializer will then iterate over all fields and bits

Associated Constants

The minimum bit length an uavcan type can have

Examples


// The primitive types have a fixed amount of bits
assert_eq!(u2::BIT_LENGTH_MIN, 2);

// The static arrays also have a fixed amount of bits
assert_eq!(<[i62; 4] as Serializable>::BIT_LENGTH_MIN, 62*4);
 
// The dynamic arrays have their length coding included even though they can be optimized in some cases
assert_eq!(Dynamic::<[void11; 3]>::BIT_LENGTH_MIN, 2);

// Structs have the sum of all fields `MIN_BIT_LENGTH` as their `MIN_BIT_LENGTH`.
#[derive(UavcanStruct)]
struct Foo {
    v1: u2,
    v2: [i62; 4],
    v3: Dynamic<[void11; 3]>,
}

assert_eq!(Foo::BIT_LENGTH_MIN, 2 + 62*4 + 2);

// Enums have the minimum of all variants `MIN_BIT_LENGTH` as their `MIN_BIT_LENGTH`.
// (But this is not implemented yet)

Number of primitive fields after flattening of data type.

Flattening of a struct consists of replacing all structs with its fields. Flattening of an enum consists of putting all fields in order

Examples

Flattening of struct

#[derive(UavcanStruct)]
struct InnerStruct {
    v1: u8,
    v2: u8,
}

#[derive(UavcanStruct)]
struct OuterStruct {
    v1: InnerStruct,
    v2: InnerStruct,
}

assert_eq!(InnerStruct::FLATTENED_FIELDS_NUMBER, 2);
assert_eq!(OuterStruct::FLATTENED_FIELDS_NUMBER, 4);

Flattening of enum

#[derive(UavcanStruct)]
enum InnerEnum {
    V1(u8),
    V2(u8),
}

#[derive(UavcanStruct)]
enum OuterEnum {
    V1(InnerEnum),
    V2(InnerEnum),
}

assert_eq!(InnerEnum::FLATTENED_FIELDS_NUMBER, 2);
assert_eq!(OuterEnum::FLATTENED_FIELDS_NUMBER, 4);

Required Methods

Implementors