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 str
The 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.
Auto Trait Implementations§
impl<'a, R, Vals> Freeze for Variant<'a, R, Vals>where
Vals: Freeze,
impl<'a, R, Vals> RefUnwindSafe for Variant<'a, R, Vals>where
Vals: RefUnwindSafe,
R: RefUnwindSafe,
impl<'a, R, Vals> Send for Variant<'a, R, Vals>
impl<'a, R, Vals> Sync for Variant<'a, R, Vals>
impl<'a, R, Vals> Unpin for Variant<'a, R, Vals>
impl<'a, R, Vals> UnwindSafe for Variant<'a, R, Vals>where
Vals: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more