[−][src]Trait packs::structure::struct_sum::PackableStructSum
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>
size: usize,
tag_byte: u8,
reader: &mut T
) -> Result<Self, DecodeError>
fn write_struct_body<T: Write>(
&self,
writer: &mut T
) -> Result<usize, EncodeError>
&self,
writer: &mut T
) -> Result<usize, EncodeError>
fn fields_len(&self) -> usize
fn tag_byte(&self) -> u8
Implementations on Foreign Types
impl PackableStructSum for ()
[src]
A unit implementation for StructVariant
which can be used a placeholder to deny any
structures.
fn read_struct_body<T: Read>(
_: usize,
_: u8,
_: &mut T
) -> Result<Self, DecodeError>
[src]
_: usize,
_: u8,
_: &mut T
) -> Result<Self, DecodeError>
fn write_struct_body<T: Write>(&self, _: &mut T) -> Result<usize, EncodeError>
[src]
fn fields_len(&self) -> usize
[src]
fn tag_byte(&self) -> u8
[src]
Implementors
impl PackableStructSum for StdStruct
[src]
fn read_struct_body<T__PACKS_PROC_IMPL_PACKUNPACK: Read>(
size: usize,
tag_byte: u8,
reader: &mut T__PACKS_PROC_IMPL_PACKUNPACK
) -> Result<Self, DecodeError>
[src]
size: usize,
tag_byte: u8,
reader: &mut T__PACKS_PROC_IMPL_PACKUNPACK
) -> Result<Self, DecodeError>
fn write_struct_body<T__PACKS_PROC_IMPL_PACKUNPACK: Write>(
&self,
writer: &mut T__PACKS_PROC_IMPL_PACKUNPACK
) -> Result<usize, EncodeError>
[src]
&self,
writer: &mut T__PACKS_PROC_IMPL_PACKUNPACK
) -> Result<usize, EncodeError>
fn fields_len(&self) -> usize
[src]
fn tag_byte(&self) -> u8
[src]
impl PackableStructSum for GenericStruct
[src]
fn read_struct_body<T: Read>(
size: usize,
tag_byte: u8,
reader: &mut T
) -> Result<Self, DecodeError>
[src]
size: usize,
tag_byte: u8,
reader: &mut T
) -> Result<Self, DecodeError>
fn write_struct_body<T: Write>(
&self,
writer: &mut T
) -> Result<usize, EncodeError>
[src]
&self,
writer: &mut T
) -> Result<usize, EncodeError>