Skip to content

Commit fc7d8ea

Browse files
authored
Prep v0.10.1 release: more impls for EncodeAsFields (#38)
* Prep v0.10.1 release: more impls for EncodeAsFields * Address a couple of clippy lints * more clippy
1 parent 552fb48 commit fc7d8ea

File tree

4 files changed

+129
-4
lines changed

4 files changed

+129
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The format is based on [Keep a Changelog].
44

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

7+
## [0.10.1] - 2025-12-16
8+
9+
Add implementations of `EncodeAsFields` for `Vec<T>`, `[T; N]`, `&[T]` and tuples, so long as the values implement `EncodeAsType`.
10+
711
## [0.10.0] - 2024-11-15
812

913
This release updates scale-bits to 0.7.0 which is exposed in the public API of scale-encode.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = ["scale-encode", "scale-encode-derive", "testing/no_std"]
33
resolver = "2"
44

55
[workspace.package]
6-
version = "0.10.0"
6+
version = "0.10.1"
77
authors = ["Parity Technologies <[email protected]>"]
88
edition = "2021"
99
license = "Apache-2.0"

scale-encode/src/error/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Context {
4242
}
4343

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

4747
impl<'a> Path<'a> {
4848
/// Cheaply convert the path to an owned version.

scale-encode/src/impls/mod.rs

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl EncodeAsType for str {
132132
}
133133
}
134134

135-
impl<'a, T> EncodeAsType for &'a T
135+
impl<T> EncodeAsType for &T
136136
where
137137
T: EncodeAsType + ?Sized,
138138
{
@@ -453,6 +453,94 @@ impl<K: AsRef<str>, V: EncodeAsType> EncodeAsFields for BTreeMap<K, V> {
453453
}
454454
}
455455

456+
impl<const N: usize, V: EncodeAsType> EncodeAsFields for [V; N] {
457+
fn encode_as_fields_to<R: TypeResolver>(
458+
&self,
459+
fields: &mut dyn FieldIter<'_, R::TypeId>,
460+
types: &R,
461+
out: &mut Vec<u8>,
462+
) -> Result<(), Error> {
463+
Composite::new(self.iter().map(|v| (None, CompositeField::new(v))))
464+
.encode_composite_fields_to(fields, types, out)
465+
}
466+
}
467+
468+
impl<V: EncodeAsType> EncodeAsFields for &[V] {
469+
fn encode_as_fields_to<R: TypeResolver>(
470+
&self,
471+
fields: &mut dyn FieldIter<'_, R::TypeId>,
472+
types: &R,
473+
out: &mut Vec<u8>,
474+
) -> Result<(), Error> {
475+
Composite::new(self.iter().map(|v| (None, CompositeField::new(v))))
476+
.encode_composite_fields_to(fields, types, out)
477+
}
478+
}
479+
480+
impl<V: EncodeAsType> EncodeAsFields for Vec<V> {
481+
fn encode_as_fields_to<R: TypeResolver>(
482+
&self,
483+
fields: &mut dyn FieldIter<'_, R::TypeId>,
484+
types: &R,
485+
out: &mut Vec<u8>,
486+
) -> Result<(), Error> {
487+
Composite::new(self.iter().map(|v| (None, CompositeField::new(v))))
488+
.encode_composite_fields_to(fields, types, out)
489+
}
490+
}
491+
492+
macro_rules! impl_tuple_encode_as_fields {
493+
($($param:ident $idx:tt),+) => {
494+
impl <$($param: EncodeAsType),+> EncodeAsFields for ($($param,)+) {
495+
fn encode_as_fields_to<R: TypeResolver>(
496+
&self,
497+
fields: &mut dyn FieldIter<'_, R::TypeId>,
498+
types: &R,
499+
out: &mut Vec<u8>,
500+
) -> Result<(), Error> {
501+
Composite::new(
502+
[
503+
$(
504+
(None, CompositeField::new(&self.$idx))
505+
),+
506+
].into_iter(),
507+
)
508+
.encode_composite_fields_to(fields, types, out)
509+
}
510+
}
511+
}
512+
}
513+
514+
impl_tuple_encode_as_fields!(A 0);
515+
impl_tuple_encode_as_fields!(A 0, B 1);
516+
impl_tuple_encode_as_fields!(A 0, B 1, C 2);
517+
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3);
518+
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4);
519+
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5);
520+
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5, G 6);
521+
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);
522+
impl_tuple_encode_as_fields!(A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8);
523+
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);
524+
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);
525+
526+
impl EncodeAsFields for () {
527+
fn encode_as_fields_to<R: TypeResolver>(
528+
&self,
529+
fields: &mut dyn FieldIter<'_, R::TypeId>,
530+
_types: &R,
531+
_out: &mut Vec<u8>,
532+
) -> Result<(), Error> {
533+
if fields.len() != 0 {
534+
Err(Error::new(ErrorKind::WrongLength {
535+
actual_len: 0,
536+
expected_len: fields.len(),
537+
}))
538+
} else {
539+
Ok(())
540+
}
541+
}
542+
}
543+
456544
// Generate EncodeAsType impls for simple types that can be easily transformed
457545
// into types we have impls for already.
458546
macro_rules! impl_encode_like {
@@ -1102,7 +1190,39 @@ mod test {
11021190
some_field: 3,
11031191
another: 2,
11041192
},
1105-
)
1193+
);
1194+
1195+
assert_encodes_fields_like_type(
1196+
(1u64, 2u8),
1197+
Foo {
1198+
some_field: 1,
1199+
another: 2,
1200+
},
1201+
);
1202+
1203+
assert_encodes_fields_like_type(
1204+
vec![1u64, 2u64],
1205+
Foo {
1206+
some_field: 1,
1207+
another: 2,
1208+
},
1209+
);
1210+
1211+
assert_encodes_fields_like_type(
1212+
[1u64, 2u64],
1213+
Foo {
1214+
some_field: 1,
1215+
another: 2,
1216+
},
1217+
);
1218+
1219+
assert_encodes_fields_like_type(
1220+
&[1u64, 2u64][..],
1221+
Foo {
1222+
some_field: 1,
1223+
another: 2,
1224+
},
1225+
);
11061226
}
11071227

11081228
#[test]
@@ -1163,6 +1283,7 @@ mod test {
11631283
}
11641284

11651285
#[test]
1286+
#[allow(unused_assignments)]
11661287
fn encode_to_number_skipping_attrs_via_macro_works() {
11671288
struct NotEncodeAsType;
11681289

0 commit comments

Comments
 (0)