Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [0.10.1] - 2025-12-16

Add implementations of `EncodeAsFields` for `Vec<T>`, `[T; N]`, `&[T]` and tuples, so long as the values implement `EncodeAsType`.

## [0.10.0] - 2024-11-15

This release updates scale-bits to 0.7.0 which is exposed in the public API of scale-encode.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["scale-encode", "scale-encode-derive", "testing/no_std"]
resolver = "2"

[workspace.package]
version = "0.10.0"
version = "0.10.1"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion scale-encode/src/error/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Context {
}

/// The current path that we're trying to encode.
pub struct Path<'a>(Cow<'a, Vec<Location>>);
pub struct Path<'a>(Cow<'a, [Location]>);

impl<'a> Path<'a> {
/// Cheaply convert the path to an owned version.
Expand Down
125 changes: 123 additions & 2 deletions scale-encode/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl EncodeAsType for str {
}
}

impl<'a, T> EncodeAsType for &'a T
impl<T> EncodeAsType for &T
where
T: EncodeAsType + ?Sized,
{
Expand Down Expand Up @@ -453,6 +453,94 @@ impl<K: AsRef<str>, V: EncodeAsType> EncodeAsFields for BTreeMap<K, V> {
}
}

impl<const N: usize, V: EncodeAsType> EncodeAsFields for [V; N] {
fn encode_as_fields_to<R: TypeResolver>(
&self,
fields: &mut dyn FieldIter<'_, R::TypeId>,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), Error> {
Composite::new(self.iter().map(|v| (None, CompositeField::new(v))))
.encode_composite_fields_to(fields, types, out)
}
}

impl<V: EncodeAsType> EncodeAsFields for &[V] {
fn encode_as_fields_to<R: TypeResolver>(
&self,
fields: &mut dyn FieldIter<'_, R::TypeId>,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), Error> {
Composite::new(self.iter().map(|v| (None, CompositeField::new(v))))
.encode_composite_fields_to(fields, types, out)
}
}

impl<V: EncodeAsType> EncodeAsFields for Vec<V> {
fn encode_as_fields_to<R: TypeResolver>(
&self,
fields: &mut dyn FieldIter<'_, R::TypeId>,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), Error> {
Composite::new(self.iter().map(|v| (None, CompositeField::new(v))))
.encode_composite_fields_to(fields, types, out)
}
}

macro_rules! impl_tuple_encode_as_fields {
($($param:ident $idx:tt),+) => {
impl <$($param: EncodeAsType),+> EncodeAsFields for ($($param,)+) {
fn encode_as_fields_to<R: TypeResolver>(
&self,
fields: &mut dyn FieldIter<'_, R::TypeId>,
types: &R,
out: &mut Vec<u8>,
) -> Result<(), Error> {
Composite::new(
[
$(
(None, CompositeField::new(&self.$idx))
),+
].into_iter(),
)
.encode_composite_fields_to(fields, types, out)
}
}
}
}

impl_tuple_encode_as_fields!(A 0);
impl_tuple_encode_as_fields!(A 0, B 1);
impl_tuple_encode_as_fields!(A 0, B 1, C 2);
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3);
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4);
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5);
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5, G 6);
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8);
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9);
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10);

impl EncodeAsFields for () {
fn encode_as_fields_to<R: TypeResolver>(
&self,
fields: &mut dyn FieldIter<'_, R::TypeId>,
_types: &R,
_out: &mut Vec<u8>,
) -> Result<(), Error> {
if fields.len() != 0 {
Err(Error::new(ErrorKind::WrongLength {
actual_len: 0,
expected_len: fields.len(),
}))
} else {
Ok(())
}
}
}

// Generate EncodeAsType impls for simple types that can be easily transformed
// into types we have impls for already.
macro_rules! impl_encode_like {
Expand Down Expand Up @@ -1102,7 +1190,39 @@ mod test {
some_field: 3,
another: 2,
},
)
);

assert_encodes_fields_like_type(
(1u64, 2u8),
Foo {
some_field: 1,
another: 2,
},
);

assert_encodes_fields_like_type(
vec![1u64, 2u64],
Foo {
some_field: 1,
another: 2,
},
);

assert_encodes_fields_like_type(
[1u64, 2u64],
Foo {
some_field: 1,
another: 2,
},
);

assert_encodes_fields_like_type(
&[1u64, 2u64][..],
Foo {
some_field: 1,
another: 2,
},
);
}

#[test]
Expand Down Expand Up @@ -1163,6 +1283,7 @@ mod test {
}

#[test]
#[allow(unused_assignments)]
fn encode_to_number_skipping_attrs_via_macro_works() {
struct NotEncodeAsType;

Expand Down
Loading