[][src]Trait packs::structure::struct_sum::PackableStructSum

pub trait PackableStructSum: Sized {
    fn read_struct_body<T: Read>(
        size: usize,
        tag_byte: u8,
        reader: &mut T
    ) -> Result<Self, DecodeError>;
fn write_struct_body<T: Write>(
        &self,
        writer: &mut T
    ) -> Result<usize, EncodeError>;
fn fields_len(&self) -> usize;
fn tag_byte(&self) -> u8; }

A sum type of possible structs this type supports. The type is a trait to abstract away from a type which looks something like

struct S1;
struct S2;

enum ValueStructs {
    S1(S1),
    S2(S2)
}

in the context of PackStream. This would then implement the trait as follows:

impl PackableStructSum for ValueStructs {
fn read_struct_body<T: Read>(size: usize,tag_byte: u8,reader: &mut T) -> Result<Self, DecodeError> {
    match tag_byte {
         0x01 => todo!(), // S1's tag byte, read S1
         0x02 => todo!(), // S2's tag byte, read S2
         _ => Err(DecodeError::UnexpectedTagByte(tag_byte))
    }
}

fn write_struct_body<T: Write>(&self,writer: &mut T) -> Result<usize, EncodeError> {
    match self {
        ValueStructs::S1(s1) => todo!(), // write s1's body (no marker, no size, no tag)
        ValueStructs::S2(s2) => todo!(), // write s2's body (..)
    }
}

fn fields_len(&self) -> usize {
    2
}

fn tag_byte(&self) -> u8 {
    match self {
        ValueStructs::S1(_) => 0x01,
        ValueStructs::S2(_) => 0x02,
}
}

}

Usually, such a variant can be generate with a macro provided by packs_proc.

PackStream implementation

A PackableStructSum has an auto-implementation for Pack and Unpack making it possible to decode and encode user defined structs, which are part of a variant.

Required methods

fn read_struct_body<T: Read>(
    size: usize,
    tag_byte: u8,
    reader: &mut T
) -> Result<Self, DecodeError>

fn write_struct_body<T: Write>(
    &self,
    writer: &mut T
) -> Result<usize, EncodeError>

fn fields_len(&self) -> usize

fn tag_byte(&self) -> u8

Loading content...

Implementations on Foreign Types

impl PackableStructSum for ()[src]

A unit implementation for StructVariant which can be used a placeholder to deny any structures.

Loading content...

Implementors

impl PackableStructSum for StdStruct[src]

impl PackableStructSum for GenericStruct[src]

Loading content...