Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle #[codec(index = …)] in regular enums #80

Merged
merged 87 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
37c0a57
Parameterize CompactForm String for optional SCALE impl
ascjones Dec 7, 2020
643f09d
Merge remote-tracking branch 'origin/master' into aj-compact-string
dvdplm Dec 14, 2020
9a7ccbf
Fix no-std compilation
dvdplm Dec 14, 2020
254fee1
Obey the fmt
dvdplm Dec 14, 2020
e74e4f9
Introduce String trait for Form
ascjones Dec 16, 2020
7860c79
Rename "Compact" to "Frozen" (and associated fallout)
dvdplm Dec 16, 2020
579f958
Docs cleanup and more renames
dvdplm Dec 17, 2020
8333e5a
Cleanup
dvdplm Dec 17, 2020
2818f7b
More cleanup
dvdplm Dec 17, 2020
7706a38
Merge branch 'aj-compact-string' into dp-rename-compact-to-frozen
dvdplm Dec 17, 2020
e03a2cd
obey the fmt
dvdplm Dec 17, 2020
3a95663
Add a `compact` flag to `Field` to indicate that this type is to be e…
dvdplm Dec 17, 2020
004e107
Clippy warnings
dvdplm Dec 17, 2020
93a9aeb
Acommodate older clippy
dvdplm Dec 17, 2020
6569e50
Derive (scale) compact fields
dvdplm Dec 28, 2020
f098101
Merge branch 'master' into dp-flag-types-as-compact
dvdplm Jan 4, 2021
b43cdfc
Use utils from parity-scale-codec-derive
dvdplm Jan 4, 2021
eda2769
Merge remote-tracking branch 'origin/master' into dp-flag-types-as-co…
dvdplm Jan 5, 2021
6321da9
Merge branch 'dp-flag-types-as-compact' into dp-handle-scale-skip
dvdplm Jan 5, 2021
1d32a38
Merge remote-tracking branch 'origin/master' into dp-handle-scale-skip
dvdplm Jan 19, 2021
d1700b2
fmt
dvdplm Jan 19, 2021
3387355
Merge branch 'master' into dp-handle-scale-skip
dvdplm Feb 17, 2021
d29c9be
Attempt to fix CI
dvdplm Feb 18, 2021
0426479
FIx CI take 2
dvdplm Feb 18, 2021
0a97eb1
Merge branch 'master' into dp-handle-scale-skip
dvdplm Mar 2, 2021
81e3519
Merge branch 'master' into dp-handle-scale-skip
dvdplm Mar 17, 2021
e8d2a74
Use is_compact from utils
dvdplm Mar 17, 2021
d9239c5
Fn is enough
dvdplm Mar 17, 2021
98292c5
Doc tweaks
dvdplm Mar 18, 2021
71d7e11
Add tests for enums
dvdplm Mar 18, 2021
00aee2f
Add test for indexed enum
dvdplm Mar 22, 2021
a31acc5
Oops
dvdplm Mar 22, 2021
e70425f
Update derive/src/utils.rs
dvdplm Mar 23, 2021
abdb081
Review feedback
dvdplm Mar 23, 2021
c53eb38
fmt
dvdplm Mar 23, 2021
7c0c6f8
wip
dvdplm Mar 23, 2021
1a0a030
Add indexed_*
dvdplm Mar 23, 2021
2dd05cb
Renames, sort out todos
dvdplm Mar 23, 2021
81e6fc3
fmt
dvdplm Mar 23, 2021
14bbbe9
Capture docs for types, fields, and enum variants
ascjones Apr 26, 2021
4e696dc
Add docs to derive tests
ascjones Apr 26, 2021
55fc739
Fmt
ascjones Apr 26, 2021
ded2f3f
Fmt
ascjones Apr 27, 2021
3b78d47
Make clippy happy
ascjones Apr 27, 2021
c74422d
Fix struct docs tests
ascjones Apr 27, 2021
329d915
Fix struct docs tests
ascjones Apr 27, 2021
637ab5a
Add missing Vec import
ascjones Apr 27, 2021
efdd3e4
Fix test
ascjones Apr 27, 2021
77fc88a
Clear docs
ascjones Apr 27, 2021
33f16f1
Merge branch 'master' into dp-handle-scale-index-in-regular-enums
dvdplm May 1, 2021
f06b059
Cleanup
dvdplm May 1, 2021
d15566f
Random change to kick CI in the butt
dvdplm May 6, 2021
0d4d126
Fix(?) trybuild
dvdplm May 6, 2021
912fbcb
Merge branch 'master' into aj-docs
ascjones May 25, 2021
4151589
Promote build to it's own mod, split fields and variant builders
ascjones May 25, 2021
a0743b0
Refactor variant construction with builder
ascjones May 25, 2021
fd6f893
Fix up derive and unit tests with Variant builder
ascjones May 25, 2021
52f5e83
Fmt
ascjones May 25, 2021
2d2ab5c
Condense build module back to single file
ascjones May 25, 2021
c4bf49d
Fix up doc tests
ascjones May 25, 2021
05d875b
Fix up README
ascjones May 25, 2021
790a6b8
Define initial field builder
ascjones May 26, 2021
0f3b795
Fix up field building
ascjones May 26, 2021
655279a
Fix tests
ascjones May 26, 2021
63b986a
Fully qualify calls to stringify
ascjones May 26, 2021
150ef4e
Default impl for FieldBuilder
ascjones May 26, 2021
43551f4
Fix up default impl
ascjones May 26, 2021
0decd01
Fix spelling errors
ascjones May 26, 2021
e031939
Remove clear_docs
ascjones May 26, 2021
fdd96c3
Fully qualify module_path
ascjones May 26, 2021
585a671
Update derive/src/lib.rs
ascjones May 26, 2021
a7179b5
Merge remote-tracking branch 'origin/aj-docs' into aj-docs
ascjones May 26, 2021
4d73ae4
Use callback for variant builder
ascjones May 26, 2021
59ff44c
Update README
ascjones May 26, 2021
e8ea40b
Remove leading space in doc comments
ascjones May 26, 2021
1c01b94
Satisfy clippy
ascjones May 26, 2021
0edec05
Add test for doc capture
ascjones May 27, 2021
fcc8985
Rename index back to discriminant, changes for this will be in https:…
ascjones May 27, 2021
6fb8042
Rename index back to discriminant, changes for this will be in https:…
ascjones May 27, 2021
a0430e4
Merge branch 'master' into dp-handle-scale-index-in-regular-enums
ascjones May 27, 2021
1cc85a7
Merge branch 'aj-docs' into dp-handle-scale-index-in-regular-enums
ascjones May 27, 2021
8e61118
Update builders and fix tests
ascjones May 27, 2021
0bb56bf
Merge branch 'master' into dp-handle-scale-index-in-regular-enums
ascjones Jun 6, 2021
b8ee0a7
Restore index on variant builder and fix tests
ascjones Jun 6, 2021
2ebcd0c
Fmt
ascjones Jun 6, 2021
e016e9e
Clippy
ascjones Jun 6, 2021
0a61395
Clippy
ascjones Jun 6, 2021
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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ pub enum TypeDef<T: Form = MetaForm> {

The following "built-in" types have predefined `TypeInfo` definitions:

- **Primitives:** `bool`, `char`, `str`, `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64
`, `i128`.
- **Primitives:** `bool`, `char`, `str`, `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64`, `i128`.

- **Sequence:** Variable size sequence of elements of `T`, where `T` implements `TypeInfo`. e.g. `[T]`, `&[T]`, `&mut
[T]`, `Vec<T>`
Expand Down Expand Up @@ -234,7 +233,7 @@ struct Foo<S> { #[codec(compact)] a: S }

You may experience the following error when using this generic type without the correct bounds:

```
```sh
error[E0275]: overflow evaluating the requirement `_::_parity_scale_codec::Compact<_>: Decode`
```

Expand Down
8 changes: 6 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ fn is_c_like_enum(variants: &VariantList) -> bool {
fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStream2 {
let variants = &data_enum.variants;

if is_c_like_enum(&variants) {
if is_c_like_enum(variants) {
return generate_c_like_enum_def(variants, scale_info)
}

Expand All @@ -262,6 +262,7 @@ fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStrea
let ident = &v.ident;
let v_name = quote! {::core::stringify!(#ident) };
let docs = utils::get_doc_literals(&v.attrs);
let index = utils::maybe_index(v).map(|i| quote!(.index(#i)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to overstate how much nicer this is compared to what I had.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was one of my goals with the builder pattern changes, so thanks!, The main priorities being type safe construction and friendliness for the derive macro. The tradeoff being that it is not fantastic for constructing type infos by hand, but we are assuming that is not very common.


let fields = match v.fields {
Fields::Named(ref fs) => {
Expand All @@ -287,7 +288,10 @@ fn generate_variant_type(data_enum: &DataEnum, scale_info: &Ident) -> TokenStrea

quote! {
.variant(#v_name, |v|
v.fields(#fields).docs(&[ #( #docs ),* ])
v
.fields(#fields)
.docs(&[ #( #docs ),* ])
#index
)
}
});
Expand Down
4 changes: 2 additions & 2 deletions derive/src/trait_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ fn collect_types_to_bind(
.iter()
.filter(|field| {
// Only add a bound if the type uses a generic.
type_contains_idents(&field.ty, &ty_params)
type_contains_idents(&field.ty, ty_params)
&&
// Remove all remaining types that start/contain the input ident
// to not have them in the where clause.
!type_or_sub_type_path_starts_with_ident(&field.ty, &input_ident)
!type_or_sub_type_path_starts_with_ident(&field.ty, input_ident)
})
.map(|f| (f.ty.clone(), utils::is_compact(f)))
.collect()
Expand Down
30 changes: 20 additions & 10 deletions derive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,26 @@ pub fn get_doc_literals(attrs: &[syn::Attribute]) -> Vec<syn::Lit> {
/// Look for a `#[codec(index = $int)]` attribute on a variant. If no attribute
/// is found, fall back to the discriminant or just the variant index.
pub fn variant_index(v: &Variant, i: usize) -> TokenStream {
// first look for an attribute
let index = find_meta_item(v.attrs.iter(), |meta| {
// first look for an `index` attribute…
let index = maybe_index(v);
// …then fallback to discriminant or just index
index.map(|i| quote! { #i }).unwrap_or_else(|| {
v.discriminant
.as_ref()
.map(|&(_, ref expr)| quote! { #expr })
.unwrap_or_else(|| quote! { #i })
})
}

/// Look for a `#[codec(index = $int)]` outer attribute on a variant.
/// If found, it is expected to be a parseable as a `u8` (panics otherwise).
pub fn maybe_index(variant: &Variant) -> Option<u8> {
let outer_attrs = variant
.attrs
.iter()
.filter(|attr| attr.style == AttrStyle::Outer);

find_meta_item(outer_attrs, |meta| {
if let NestedMeta::Meta(Meta::NameValue(ref nv)) = meta {
if nv.path.is_ident("index") {
if let Lit::Int(ref v) = nv.lit {
Expand All @@ -72,14 +90,6 @@ pub fn variant_index(v: &Variant, i: usize) -> TokenStream {
}

None
});

// then fallback to discriminant or just index
index.map(|i| quote! { #i }).unwrap_or_else(|| {
v.discriminant
.as_ref()
.map(|&(_, ref expr)| quote! { #expr })
.unwrap_or_else(|| quote! { #i })
})
}

Expand Down
20 changes: 17 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
//! }
//! }
//! ```
//! ## Enum without fields
//! ## Enum without fields, aka C-style enums.
//! ```
//! # use scale_info::{build::{Fields, Variants}, MetaType, Path, Type, TypeInfo, Variant};
//! enum Foo {
Expand Down Expand Up @@ -407,7 +407,7 @@ impl<N> FieldBuilder<N, field_state::TypeAssigned> {
self.name,
self.ty.expect("Type should be set by builder"),
self.type_name,
&self.docs,
self.docs,
)
}
}
Expand Down Expand Up @@ -453,6 +453,7 @@ impl Variants {
pub struct VariantBuilder {
name: &'static str,
fields: Vec<Field<MetaForm>>,
index: Option<u8>,
discriminant: Option<u64>,
docs: Vec<&'static str>,
}
Expand All @@ -464,6 +465,7 @@ impl VariantBuilder {
name,
fields: Vec::new(),
discriminant: None,
index: None,
docs: Vec::new(),
}
}
Expand All @@ -474,6 +476,12 @@ impl VariantBuilder {
self
}

/// Set the variant's codec index.
pub fn index(mut self, index: u8) -> Self {
self.index = Some(index);
self
}

/// Initialize the variant's fields.
pub fn fields<F>(mut self, fields_builder: FieldsBuilder<F>) -> Self {
self.fields = fields_builder.finalize();
Expand All @@ -488,6 +496,12 @@ impl VariantBuilder {

/// Complete building and create final [`Variant`] instance.
pub fn finalize(self) -> Variant<MetaForm> {
Variant::new(self.name, self.fields, self.discriminant, self.docs)
Variant::new(
self.name,
self.fields,
self.index,
self.discriminant,
self.docs,
)
}
}
75 changes: 75 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,78 @@ fn basic_struct_with_phantoms() {

assert_type!(SomeStruct<bool>, struct_bool_type_info);
}

#[test]
fn basic_enum_with_index() {
use scale::Encode;

#[allow(unused)]
#[derive(Encode)]
enum IndexedRustEnum {
#[codec(index = 3)]
A(bool),
#[codec(index = 0)]
B {
b: u8,
},
C(u16, u32),
D,
}
impl TypeInfo for IndexedRustEnum {
type Identity = Self;

fn type_info() -> Type {
Type::builder()
.path(Path::new("IndexedRustEnum", module_path!()))
.variant(
Variants::new()
.variant("A", |v| {
v.index(3).fields(
Fields::unnamed()
.field(|f| f.ty::<bool>().type_name("bool")),
)
})
.variant("B", |v| {
v.index(0).fields(
Fields::named()
.field(|f| f.ty::<u8>().name("b").type_name("u8")),
)
})
.variant("C", |v| {
v.fields(
Fields::unnamed()
.field(|f| f.ty::<u16>().type_name("u16"))
.field(|f| f.ty::<u32>().type_name("u32")),
)
})
.variant_unit("D"),
)
}
}

let ty = Type::builder()
.path(Path::new("IndexedRustEnum", module_path!()))
.variant(
Variants::new()
.variant("A", |v| {
v.index(3).fields(
Fields::unnamed().field(|f| f.ty::<bool>().type_name("bool")),
)
})
.variant("B", |v| {
v.index(0).fields(
Fields::named().field(|f| f.ty::<u8>().name("b").type_name("u8")),
)
})
.variant("C", |v| {
v.fields(
Fields::unnamed()
.field(|f| f.ty::<u16>().type_name("u16"))
.field(|f| f.ty::<u32>().type_name("u32")),
)
})
.variant_unit("D"),
);

assert_type!(IndexedRustEnum, ty);
}
13 changes: 11 additions & 2 deletions src/ty/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ pub struct Variant<T: Form = MetaForm> {
serde(skip_serializing_if = "Vec::is_empty", default)
)]
fields: Vec<Field<T>>,
/// Index of the variant, used in `parity-scale-codec`
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Option::is_none", default)
)]
index: Option<u8>,
/// The discriminant of the variant.
///
/// # Note
Expand Down Expand Up @@ -184,6 +190,7 @@ impl IntoPortable for Variant {
Variant {
name: self.name.into_portable(registry),
fields: registry.map_into_portable(self.fields),
index: self.index,
discriminant: self.discriminant,
docs: registry.map_into_portable(self.docs),
}
Expand All @@ -195,13 +202,15 @@ impl Variant {
pub(crate) fn new(
name: &'static str,
fields: Vec<Field<MetaForm>>,
index: Option<u64>,
index: Option<u8>,
discriminant: Option<u64>,
docs: Vec<&'static str>,
) -> Self {
Self {
name,
fields,
discriminant: index,
index,
discriminant,
docs,
}
}
Expand Down
35 changes: 35 additions & 0 deletions test_suite/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,41 @@ fn enum_derive() {
assert_type!(E<bool>, ty);
}

#[test]
fn enum_derive_with_codec_index() {
#[allow(unused)]
#[derive(TypeInfo, Encode)]
enum E<T> {
#[codec(index = 5)]
A(T),
#[codec(index = 0)]
B { b: T },
#[codec(index = 13)]
C,
}

let ty = Type::builder()
.path(Path::new("E", "derive"))
.type_params(tuple_meta_type!(bool))
.variant(
Variants::new()
.variant("A", |v| {
v.index(5).fields(
Fields::unnamed().field(|f| f.ty::<bool>().type_name("T")),
)
})
.variant("B", |v| {
v.index(0).fields(
Fields::named()
.field(|f| f.ty::<bool>().name("b").type_name("T")),
)
})
.variant("C", |v| v.index(13)),
);

assert_type!(E<bool>, ty);
}

#[test]
fn recursive_type_derive() {
#[allow(unused)]
Expand Down
52 changes: 52 additions & 0 deletions test_suite/tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,58 @@ fn test_enum() {
}));
}

#[test]
fn enums_with_scale_indexed_variants() {
#[derive(TypeInfo, Encode)]
enum Animal {
#[codec(index = 123)]
Ape(u8),
#[codec(index = 12)]
Boar { a: u16, b: u32 },
#[codec(index = 1)]
Cat,
#[codec(index = 0)]
Dog(u64, u128),
}

assert_json_for_type::<Animal>(json!({
"path": ["json", "Animal"],
"def": {
"variant": {
"variants": [
{
"name": "Ape",
"index": 123,
"fields": [
{ "type": 0, "typeName": "u8" }
]
},
{
"name": "Boar",
"index": 12,
"fields": [
{ "name": "a", "type": 1, "typeName": "u16" },
{ "name": "b", "type": 2, "typeName": "u32" }
]
},
{
"name": "Cat",
"index": 1,
},
{
"name": "Dog",
"index": 0,
"fields": [
{ "type": 3, "typeName": "u64" },
{ "type": 4, "typeName": "u128" }
]
}
]
}
}
}));
}

#[test]
fn test_recursive_type_with_box() {
#[derive(TypeInfo)]
Expand Down