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,
impl<'a, R, Vals> Composite<R, Vals>where
R: TypeResolver + 'a,
Vals: ExactSizeIterator<Item = (Option<&'a str>, CompositeField<'a, R>)> + Clone,
Sourcepub fn new(vals: Vals) -> Self
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());
Sourcepub fn encode_composite_as_type(
&self,
type_id: R::TypeId,
types: &R,
) -> Result<Vec<u8>, Error>
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.
Sourcepub fn encode_composite_as_type_to(
&self,
type_id: R::TypeId,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), Error>
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.