Struct Variant

Source
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,

Source

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.

Source

pub fn encode_variant_as_type_to( &self, type_id: R::TypeId, types: &R, out: &mut Vec<u8>, ) -> Result<(), Error>

Encode the variant as the provided type to the output bytes.

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>
where Vals: Send, R: Send,

§

impl<'a, R, Vals> Sync for Variant<'a, R, Vals>
where Vals: Sync, R: Sync,

§

impl<'a, R, Vals> Unpin for Variant<'a, R, Vals>
where Vals: Unpin, R: Unpin,

§

impl<'a, R, Vals> UnwindSafe for Variant<'a, R, Vals>
where Vals: UnwindSafe, R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.