Skip to content

Commit

Permalink
Codegen: enum variant indices (paritytech#308)
Browse files Browse the repository at this point in the history
* fix: use metadata index in enum variants

* test: updated code gen test with enum index
  • Loading branch information
sander2 authored and 0623forbidden committed Feb 15, 2022
1 parent 4c67c26 commit d3641f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions codegen/src/types/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,11 @@ fn generate_enum() {
use super::root;
#[derive(::subxt::codec::Encode, ::subxt::codec::Decode)]
pub enum E {
# [codec (index = 0)]
A,
# [codec (index = 1)]
B (::core::primitive::bool,),
# [codec (index = 2)]
C { a: ::core::primitive::u32, },
}
}
Expand Down Expand Up @@ -467,7 +470,9 @@ fn box_fields_enum() {
use super::root;
#[derive(::subxt::codec::Encode, ::subxt::codec::Decode)]
pub enum E {
# [codec (index = 0)]
A(::std::boxed::Box<::core::primitive::bool>,),
# [codec (index = 1)]
B { a: ::std::boxed::Box<::core::primitive::u32>, },
}
}
Expand Down
6 changes: 5 additions & 1 deletion codegen/src/types/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ impl<'a> quote::ToTokens for TypeDefGen<'a> {
} else {
self.composite_fields(v.fields(), &type_params, false)
};
variants.push(quote! { #variant_name #fields });
let index = proc_macro2::Literal::u8_unsuffixed(v.index());
variants.push(quote! {
#[codec(index = #index)]
#variant_name #fields
});
let unused_params_set = unused_type_params.iter().cloned().collect();
let used_params = type_params_set.difference(&unused_params_set);

Expand Down

0 comments on commit d3641f0

Please sign in to comment.