pub struct Variant<'a, R, Vals> {
pub name: &'a str,
pub fields: Composite<R, Vals>,
}Expand description
This type represents named or unnamed composite values, and can be used
to help generate EncodeAsType impls. It’s primarily used by the exported
macros to do just that.
use scale_encode::{
Error, EncodeAsType, Composite, CompositeField, Variant, TypeResolver
};
enum MyType {
SomeField(bool),
OtherField { foo: u64, bar: String }
}
impl EncodeAsType for MyType {
fn encode_as_type_to<R: TypeResolver>(
&self,
type_id: R::TypeId,
types: &R,
out: &mut Vec<u8>
) -> Result<(), Error> {
match self {
MyType::SomeField(b) => Variant {
name: "SomeField",
fields: Composite::new([
(None, CompositeField::new(b)),
].into_iter())
}.encode_variant_as_type_to(type_id, types, out),
MyType::OtherField { foo, bar } => Variant {
name: "OtherField",
fields: Composite::new([
(Some("foo"), CompositeField::new(foo)),
(Some("bar"), CompositeField::new(bar))
].into_iter())
}.encode_variant_as_type_to(type_id, types, out)
}
}
}Fields§
§name: &'a strThe name of the variant we’ll try to encode into.
fields: Composite<R, Vals>The fields of the variant that we wish to encode.
Implementations§
Source§impl<'a, R, Vals> Variant<'a, R, Vals>where
R: TypeResolver + 'a,
Vals: ExactSizeIterator<Item = (Option<&'a str>, CompositeField<'a, R>)> + Clone,
impl<'a, R, Vals> Variant<'a, R, Vals>where
R: TypeResolver + 'a,
Vals: ExactSizeIterator<Item = (Option<&'a str>, CompositeField<'a, R>)> + Clone,
Sourcepub fn encode_variant_as_type(
&self,
type_id: R::TypeId,
types: &R,
) -> Result<Vec<u8>, Error>
pub fn encode_variant_as_type( &self, type_id: R::TypeId, types: &R, ) -> Result<Vec<u8>, Error>
A shortcut for Self::encode_variant_as_type_to() which internally
allocates a Vec and returns it.