Skip to content

Commit d82f943

Browse files
authored
Revert breaking changes, bump version to 3.6.9 (#545)
* Revert "Update MaxEncodedLen derive macro (#512)" This reverts commit 1e3f80c. * Revert "Fix max_encoded_len for Compact fields (#508)" This reverts commit ddf9439 * Bump version to 3.6.9 * Cargo.lock * ui test
1 parent e9e396c commit d82f943

File tree

8 files changed

+40
-115
lines changed

8 files changed

+40
-115
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "parity-scale-codec"
33
description = "SCALE - Simple Concatenating Aggregated Little Endians"
4-
version = "3.6.8"
4+
version = "3.6.9"
55
authors = ["Parity Technologies <admin@parity.io>"]
66
license = "Apache-2.0"
77
repository = "https://github.com/paritytech/parity-scale-codec"
@@ -12,7 +12,7 @@ rust-version = "1.60.0"
1212
[dependencies]
1313
arrayvec = { version = "0.7", default-features = false }
1414
serde = { version = "1.0.193", default-features = false, optional = true }
15-
parity-scale-codec-derive = { path = "derive", version = ">= 3.6.8", default-features = false, optional = true }
15+
parity-scale-codec-derive = { path = "derive", version = ">= 3.6.9", default-features = false, optional = true }
1616
bitvec = { version = "1", default-features = false, features = [ "alloc" ], optional = true }
1717
bytes = { version = "1", default-features = false, optional = true }
1818
byte-slice-cast = { version = "1.2.2", default-features = false }

derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "parity-scale-codec-derive"
33
description = "Serialization and deserialization derive macro for Parity SCALE Codec"
4-
version = "3.6.8"
4+
version = "3.6.9"
55
authors = ["Parity Technologies <admin@parity.io>"]
66
license = "Apache-2.0"
77
edition = "2021"

derive/src/max_encoded_len.rs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
use crate::{
1919
trait_bounds,
20-
utils::{self, codec_crate_path, custom_mel_trait_bound, has_dumb_trait_bound, should_skip},
20+
utils::{codec_crate_path, custom_mel_trait_bound, has_dumb_trait_bound, should_skip},
2121
};
2222
use quote::{quote, quote_spanned};
23-
use syn::{parse_quote, spanned::Spanned, Data, DeriveInput, Field, Fields};
23+
use syn::{parse_quote, spanned::Spanned, Data, DeriveInput, Fields, Type};
2424

2525
/// impl for `#[derive(MaxEncodedLen)]`
2626
pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
@@ -43,13 +43,13 @@ pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::Tok
4343
parse_quote!(#crate_path::MaxEncodedLen),
4444
None,
4545
has_dumb_trait_bound(&input.attrs),
46-
&crate_path,
46+
&crate_path
4747
) {
4848
return e.to_compile_error().into()
4949
}
5050
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
5151

52-
let data_expr = data_length_expr(&input.data, &crate_path);
52+
let data_expr = data_length_expr(&input.data);
5353

5454
quote::quote!(
5555
const _: () = {
@@ -64,22 +64,22 @@ pub fn derive_max_encoded_len(input: proc_macro::TokenStream) -> proc_macro::Tok
6464
}
6565

6666
/// generate an expression to sum up the max encoded length from several fields
67-
fn fields_length_expr(fields: &Fields, crate_path: &syn::Path) -> proc_macro2::TokenStream {
68-
let fields_iter: Box<dyn Iterator<Item = &Field>> = match fields {
69-
Fields::Named(ref fields) => Box::new(fields.named.iter().filter_map(|field| {
70-
if should_skip(&field.attrs) {
67+
fn fields_length_expr(fields: &Fields) -> proc_macro2::TokenStream {
68+
let type_iter: Box<dyn Iterator<Item = &Type>> = match fields {
69+
Fields::Named(ref fields) => Box::new(
70+
fields.named.iter().filter_map(|field| if should_skip(&field.attrs) {
7171
None
7272
} else {
73-
Some(field)
74-
}
75-
})),
76-
Fields::Unnamed(ref fields) => Box::new(fields.unnamed.iter().filter_map(|field| {
77-
if should_skip(&field.attrs) {
73+
Some(&field.ty)
74+
})
75+
),
76+
Fields::Unnamed(ref fields) => Box::new(
77+
fields.unnamed.iter().filter_map(|field| if should_skip(&field.attrs) {
7878
None
7979
} else {
80-
Some(field)
81-
}
82-
})),
80+
Some(&field.ty)
81+
})
82+
),
8383
Fields::Unit => Box::new(std::iter::empty()),
8484
};
8585
// expands to an expression like
@@ -92,18 +92,9 @@ fn fields_length_expr(fields: &Fields, crate_path: &syn::Path) -> proc_macro2::T
9292
// `max_encoded_len` call. This way, if one field's type doesn't implement
9393
// `MaxEncodedLen`, the compiler's error message will underline which field
9494
// caused the issue.
95-
let expansion = fields_iter.map(|field| {
96-
let ty = &field.ty;
97-
if utils::is_compact(&field) {
98-
quote_spanned! {
99-
ty.span() => .saturating_add(
100-
<<#ty as #crate_path::HasCompact>::Type as #crate_path::MaxEncodedLen>::max_encoded_len()
101-
)
102-
}
103-
} else {
104-
quote_spanned! {
105-
ty.span() => .saturating_add(<#ty as #crate_path::MaxEncodedLen>::max_encoded_len())
106-
}
95+
let expansion = type_iter.map(|ty| {
96+
quote_spanned! {
97+
ty.span() => .saturating_add(<#ty>::max_encoded_len())
10798
}
10899
});
109100
quote! {
@@ -112,9 +103,9 @@ fn fields_length_expr(fields: &Fields, crate_path: &syn::Path) -> proc_macro2::T
112103
}
113104

114105
// generate an expression to sum up the max encoded length of each field
115-
fn data_length_expr(data: &Data, crate_path: &syn::Path) -> proc_macro2::TokenStream {
106+
fn data_length_expr(data: &Data) -> proc_macro2::TokenStream {
116107
match *data {
117-
Data::Struct(ref data) => fields_length_expr(&data.fields, crate_path),
108+
Data::Struct(ref data) => fields_length_expr(&data.fields),
118109
Data::Enum(ref data) => {
119110
// We need an expression expanded for each variant like
120111
//
@@ -130,7 +121,7 @@ fn data_length_expr(data: &Data, crate_path: &syn::Path) -> proc_macro2::TokenSt
130121
// Each variant expression's sum is computed the way an equivalent struct's would be.
131122

132123
let expansion = data.variants.iter().map(|variant| {
133-
let variant_expression = fields_length_expr(&variant.fields, crate_path);
124+
let variant_expression = fields_length_expr(&variant.fields);
134125
quote! {
135126
.max(#variant_expression)
136127
}

src/compact.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use crate::alloc::vec::Vec;
2020
use crate::codec::{Encode, Decode, Input, Output, EncodeAsRef};
2121
use crate::encode_like::EncodeLike;
2222
use crate::Error;
23-
#[cfg(feature = "max-encoded-len")]
24-
use crate::MaxEncodedLen;
2523
#[cfg(feature = "fuzz")]
2624
use arbitrary::Arbitrary;
2725

@@ -208,39 +206,18 @@ impl<'de, T> serde::Deserialize<'de> for Compact<T> where T: serde::Deserialize<
208206
}
209207
}
210208

211-
/// Requires the presence of `MaxEncodedLen` when the `max-encoded-len` feature is active.
212-
// Remove this trait when the feature is removed.
213-
#[cfg(feature = "max-encoded-len")]
214-
pub trait MaybeMaxEncodedLen: MaxEncodedLen {}
215-
#[cfg(feature = "max-encoded-len")]
216-
impl<T: MaxEncodedLen> MaybeMaxEncodedLen for T {}
217-
218-
/// Requires the presence of `MaxEncodedLen` when the `max-encoded-len` feature is active.
219-
// Remove this trait when the feature is removed.
220-
#[cfg(not(feature = "max-encoded-len"))]
221-
pub trait MaybeMaxEncodedLen {}
222-
#[cfg(not(feature = "max-encoded-len"))]
223-
impl<T> MaybeMaxEncodedLen for T {}
224-
225209
/// Trait that tells you if a given type can be encoded/decoded in a compact way.
226210
pub trait HasCompact: Sized {
227211
/// The compact type; this can be
228-
type Type: for<'a> EncodeAsRef<'a, Self> + Decode + From<Self> + Into<Self> + MaybeMaxEncodedLen;
212+
type Type: for<'a> EncodeAsRef<'a, Self> + Decode + From<Self> + Into<Self>;
229213
}
230214

231215
impl<'a, T: 'a> EncodeAsRef<'a, T> for Compact<T> where CompactRef<'a, T>: Encode + From<&'a T> {
232216
type RefType = CompactRef<'a, T>;
233217
}
234218

235-
#[cfg(feature = "max-encoded-len")]
236-
impl<T> MaxEncodedLen for Compact<T> where T: CompactAs, Compact<T::As>: MaxEncodedLen, Compact<T>: Encode {
237-
fn max_encoded_len() -> usize {
238-
Compact::<T::As>::max_encoded_len()
239-
}
240-
}
241-
242219
impl<T: 'static> HasCompact for T where
243-
Compact<T>: for<'a> EncodeAsRef<'a, T> + Decode + From<Self> + Into<Self> + MaybeMaxEncodedLen
220+
Compact<T>: for<'a> EncodeAsRef<'a, T> + Decode + From<Self> + Into<Self>
244221
{
245222
type Type = Compact<T>;
246223
}

src/max_encoded_len.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ macro_rules! impl_primitives {
4646
};
4747
}
4848

49+
impl_primitives!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool);
50+
4951
impl_primitives!(
50-
u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, bool,
5152
NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroI8, NonZeroI16, NonZeroI32,
5253
NonZeroI64, NonZeroI128
5354
);

tests/max_encoded_len.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! Tests for MaxEncodedLen derive macro
1717
#![cfg(all(feature = "derive", feature = "max-encoded-len"))]
1818

19-
use parity_scale_codec::{Compact, Decode, Encode, MaxEncodedLen};
19+
use parity_scale_codec::{MaxEncodedLen, Compact, Decode, Encode};
2020

2121
#[derive(Encode, MaxEncodedLen)]
2222
struct Primitives {
@@ -64,46 +64,6 @@ fn generic_max_length() {
6464
assert_eq!(Generic::<u32>::max_encoded_len(), u32::max_encoded_len() * 2);
6565
}
6666

67-
#[derive(Encode, MaxEncodedLen)]
68-
struct CompactField {
69-
#[codec(compact)]
70-
t: u64,
71-
v: u64,
72-
}
73-
74-
#[test]
75-
fn compact_field_max_length() {
76-
assert_eq!(CompactField::max_encoded_len(), 17);
77-
assert_eq!(
78-
CompactField::max_encoded_len(),
79-
Compact::<u64>::max_encoded_len() + u64::max_encoded_len()
80-
);
81-
}
82-
83-
84-
#[derive(Encode, MaxEncodedLen)]
85-
struct CompactFieldGenerics<T: MaxEncodedLen> {
86-
#[codec(compact)]
87-
t: T,
88-
v: u64,
89-
}
90-
91-
#[test]
92-
fn compact_field_generics_max_length() {
93-
assert_eq!(
94-
CompactFieldGenerics::<u64>::max_encoded_len(),
95-
CompactField::max_encoded_len()
96-
);
97-
}
98-
99-
#[derive(Encode, MaxEncodedLen)]
100-
struct CompactStruct(#[codec(compact)] u64);
101-
102-
#[test]
103-
fn compact_struct_max_length() {
104-
assert_eq!(CompactStruct::max_encoded_len(), Compact::<u64>::max_encoded_len());
105-
}
106-
10767
#[derive(Encode, MaxEncodedLen)]
10868
struct TwoGenerics<T, U> {
10969
t: T,
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
error[E0277]: the trait bound `NotMel: MaxEncodedLen` is not satisfied
1+
error[E0599]: no function or associated item named `max_encoded_len` found for struct `NotMel` in the current scope
22
--> tests/max_encoded_len_ui/unsupported_variant.rs:8:9
33
|
4+
4 | struct NotMel;
5+
| ------------- function or associated item `max_encoded_len` not found for this struct
6+
...
47
8 | NotMel(NotMel),
5-
| ^^^^^^ the trait `MaxEncodedLen` is not implemented for `NotMel`
8+
| ^^^^^^ function or associated item not found in `NotMel`
69
|
7-
= help: the following other types implement trait `MaxEncodedLen`:
8-
bool
9-
i8
10-
i16
11-
i32
12-
i64
13-
i128
14-
u8
15-
u16
16-
and $N others
10+
= help: items from traits can only be used if the trait is implemented and in scope
11+
= note: the following trait defines an item `max_encoded_len`, perhaps you need to implement it:
12+
candidate #1: `MaxEncodedLen`

0 commit comments

Comments
 (0)