Struct Composite

Source
pub struct Composite<R, Vals> { /* private fields */ }
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, TypeResolver
};

struct MyType {
   foo: bool,
   bar: u64,
   wibble: 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> {
        Composite::new([
            (Some("foo"), CompositeField::new(&self.foo)),
            (Some("bar"), CompositeField::new(&self.bar)),
            (Some("wibble"), CompositeField::new(&self.wibble))
        ].into_iter()).encode_composite_as_type_to(type_id, types, out)
    }
}

Composite cannot implement EncodeAsType itself, because it is tied to being encoded with a specific R: TypeResolver, whereas things implementing EncodeAsType need to be encodable using any TypeResolver. This is ultimately because EncodeAsType is not object safe, which prevents it from being used to describe CompositeFields.

Implementations§

Source§

impl<'a, R, Vals> Composite<R, Vals>
where R: TypeResolver + 'a, Vals: ExactSizeIterator<Item = (Option<&'a str>, CompositeField<'a, R>)> + Clone,

Source

pub fn new(vals: Vals) -> Self

Construct a new Composite type by providing an iterator over the fields that it contains.

use scale_encode::{ Composite, CompositeField };
use scale_info::PortableRegistry;

Composite::<PortableRegistry, _>::new([
    (Some("foo"), CompositeField::new(&123)),
    (Some("bar"), CompositeField::new(&"hello"))
].into_iter());
Source

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

A shortcut for Self::encode_composite_as_type_to() which internally allocates a Vec and returns it.

Source

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

Encode this composite value as the provided type to the output bytes.

Source

pub fn encode_composite_fields( &self, fields: &mut dyn FieldIter<'_, R::TypeId>, types: &R, ) -> Result<Vec<u8>, Error>

A shortcut for Self::encode_composite_fields_to() which internally allocates a Vec and returns it.

Source

pub fn encode_composite_fields_to( &self, fields: &mut dyn FieldIter<'_, R::TypeId>, types: &R, out: &mut Vec<u8>, ) -> Result<(), Error>

Encode the composite fields as the provided field description to the output bytes

Auto Trait Implementations§

§

impl<R, Vals> Freeze for Composite<R, Vals>
where Vals: Freeze,

§

impl<R, Vals> RefUnwindSafe for Composite<R, Vals>
where Vals: RefUnwindSafe, R: RefUnwindSafe,

§

impl<R, Vals> Send for Composite<R, Vals>
where Vals: Send, R: Send,

§

impl<R, Vals> Sync for Composite<R, Vals>
where Vals: Sync, R: Sync,

§

impl<R, Vals> Unpin for Composite<R, Vals>
where Vals: Unpin, R: Unpin,

§

impl<R, Vals> UnwindSafe for Composite<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.