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

chore: simplify discriminants handling in schema derives #241

Merged
merged 2 commits into from
Oct 25, 2023
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
42 changes: 14 additions & 28 deletions borsh-derive/src/internals/schema/enums/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub fn process(input: &ItemEnum, cratename: Path) -> syn::Result<TokenStream2> {
let discriminants = Discriminants::new(&input.variants);

// Generate functions that return the schema for variants.
let mut discriminant_variables = vec![];
let mut variants_defs = vec![];
let mut inner_defs = TokenStream2::new();
let mut add_recursive_defs = TokenStream2::new();
Expand All @@ -60,14 +59,12 @@ pub fn process(input: &ItemEnum, cratename: Path) -> syn::Result<TokenStream2> {
inner_defs.extend(variant_output.inner_struct);
add_recursive_defs.extend(variant_output.add_definitions_recursively_call);
variants_defs.push(variant_output.variant_entry);
discriminant_variables.push(variant_output.discriminant_variable_assignment);
}

let type_definitions = quote! {
fn add_definitions_recursively(definitions: &mut #cratename::__private::maybestd::collections::BTreeMap<#cratename::schema::Declaration, #cratename::schema::Definition>) {
#inner_defs
#add_recursive_defs
#(#discriminant_variables)*
let definition = #cratename::schema::Definition::Enum {
tag_width: 1,
variants: #cratename::__private::maybestd::vec![#(#variants_defs),*],
Expand All @@ -93,8 +90,6 @@ struct VariantOutput {
inner_struct: TokenStream2,
/// call to `add_definitions_recursively`.
add_definitions_recursively_call: TokenStream2,
/// declaration of `u8` variable, holding the value for discriminant of a variant
discriminant_variable_assignment: TokenStream2,
/// entry with a variant's declaration, element in vector of whole enum's definition
variant_entry: TokenStream2,
}
Expand All @@ -108,18 +103,9 @@ struct DiscriminantInfo<'a> {
fn process_discriminant(
variant_ident: &Ident,
info: DiscriminantInfo<'_>,
) -> syn::Result<(Ident, TokenStream2)> {
let discriminant_value =
info.discriminants
.get(variant_ident, info.use_discriminant, info.variant_idx)?;

let discriminant_variable_name = format!("discriminant_{}", info.variant_idx);
let discriminant_variable = Ident::new(&discriminant_variable_name, Span::call_site());

let discriminant_variable_assignment = quote! {
let #discriminant_variable: u8 = #discriminant_value;
};
Ok((discriminant_variable, discriminant_variable_assignment))
) -> syn::Result<TokenStream2> {
info.discriminants
.get(variant_ident, info.use_discriminant, info.variant_idx)
}

fn process_variant(
Expand All @@ -139,21 +125,21 @@ fn process_variant(
inner_struct_definition(variant, cratename, &full_variant_ident, enum_generics);
let (_ig, inner_struct_ty_generics, _wc) = inner_struct_generics.split_for_impl();

let add_definitions_recursively_call = quote! {
<#full_variant_ident #inner_struct_ty_generics as #cratename::BorshSchema>::add_definitions_recursively(definitions);
let variant_type = quote! {
Copy link
Collaborator

@dj8yfo dj8yfo Oct 24, 2023

Choose a reason for hiding this comment

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

a better name for variable might be fully_qualified_variant_type

<#full_variant_ident #inner_struct_ty_generics as #cratename::BorshSchema>
};
let discriminant_value = process_discriminant(&variant.ident, discriminant_info)?;

let (discriminant_variable, discriminant_variable_assignment) =
process_discriminant(&variant.ident, discriminant_info)?;

let variant_entry = quote! {
(#discriminant_variable as i64, #variant_name.to_string(), <#full_variant_ident #inner_struct_ty_generics as #cratename::BorshSchema>::declaration())
};
Ok(VariantOutput {
inner_struct,
add_definitions_recursively_call,
variant_entry,
discriminant_variable_assignment,
add_definitions_recursively_call: quote! {
#variant_type::add_definitions_recursively(definitions);
},
variant_entry: quote! {
(u8::from(#discriminant_value) as i64,
#variant_name.into(),
#variant_type::declaration())
},
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,17 @@ impl borsh::BorshSchema for X {
<XD as borsh::BorshSchema>::add_definitions_recursively(definitions);
<XE as borsh::BorshSchema>::add_definitions_recursively(definitions);
<XF as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let discriminant_2: u8 = 2u8;
let discriminant_3: u8 = 3u8;
let discriminant_4: u8 = 4u8;
let discriminant_5: u8 = 5u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "A".to_string(), < XA as borsh::BorshSchema >
::declaration()), (discriminant_1 as i64, "B".to_string(), < XB as
borsh::BorshSchema > ::declaration()), (discriminant_2 as i64, "C"
.to_string(), < XC as borsh::BorshSchema > ::declaration()),
(discriminant_3 as i64, "D".to_string(), < XD as borsh::BorshSchema >
::declaration()), (discriminant_4 as i64, "E".to_string(), < XE as
borsh::BorshSchema > ::declaration()), (discriminant_5 as i64, "F"
.to_string(), < XF as borsh::BorshSchema > ::declaration())
(u8::from(0u8) as i64, "A".into(), < XA as borsh::BorshSchema >
::declaration()), (u8::from(1u8) as i64, "B".into(), < XB as
borsh::BorshSchema > ::declaration()), (u8::from(2u8) as i64, "C".into(),
< XC as borsh::BorshSchema > ::declaration()), (u8::from(3u8) as i64, "D"
.into(), < XD as borsh::BorshSchema > ::declaration()), (u8::from(4u8) as
i64, "E".into(), < XE as borsh::BorshSchema > ::declaration()),
(u8::from(5u8) as i64, "F".into(), < XF as borsh::BorshSchema >
::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,17 @@ impl borsh::BorshSchema for X {
<XD as borsh::BorshSchema>::add_definitions_recursively(definitions);
<XE as borsh::BorshSchema>::add_definitions_recursively(definitions);
<XF as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0;
let discriminant_1: u8 = 20;
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
let discriminant_2: u8 = 20 + 1;
let discriminant_3: u8 = 20 + 1 + 1;
let discriminant_4: u8 = 10;
let discriminant_5: u8 = 10 + 1;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "A".to_string(), < XA as borsh::BorshSchema >
Copy link
Collaborator

Choose a reason for hiding this comment

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

the pr would be simpler to review if it didn't contain to_string -> into changes, which are a little bit off from its subject

::declaration()), (discriminant_1 as i64, "B".to_string(), < XB as
borsh::BorshSchema > ::declaration()), (discriminant_2 as i64, "C"
.to_string(), < XC as borsh::BorshSchema > ::declaration()),
(discriminant_3 as i64, "D".to_string(), < XD as borsh::BorshSchema >
::declaration()), (discriminant_4 as i64, "E".to_string(), < XE as
borsh::BorshSchema > ::declaration()), (discriminant_5 as i64, "F"
.to_string(), < XF as borsh::BorshSchema > ::declaration())
(u8::from(0) as i64, "A".into(), < XA as borsh::BorshSchema >
::declaration()), (u8::from(20) as i64, "B".into(), < XB as
borsh::BorshSchema > ::declaration()), (u8::from(20 + 1) as i64, "C"
.into(), < XC as borsh::BorshSchema > ::declaration()), (u8::from(20 + 1
+ 1) as i64, "D".into(), < XD as borsh::BorshSchema > ::declaration()),
(u8::from(10) as i64, "E".into(), < XE as borsh::BorshSchema >
::declaration()), (u8::from(10 + 1) as i64, "F".into(), < XF as
borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@ impl borsh::BorshSchema for A {
<AEggs as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASalad as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASausage as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let discriminant_2: u8 = 2u8;
let discriminant_3: u8 = 3u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "Eggs"
.to_string(), < AEggs as borsh::BorshSchema > ::declaration()),
(discriminant_2 as i64, "Salad".to_string(), < ASalad as
borsh::BorshSchema > ::declaration()), (discriminant_3 as i64, "Sausage"
.to_string(), < ASausage as borsh::BorshSchema > ::declaration())
(u8::from(0u8) as i64, "Bacon".into(), < ABacon as borsh::BorshSchema >
::declaration()), (u8::from(1u8) as i64, "Eggs".into(), < AEggs as
borsh::BorshSchema > ::declaration()), (u8::from(2u8) as i64, "Salad"
.into(), < ASalad as borsh::BorshSchema > ::declaration()),
(u8::from(3u8) as i64, "Sausage".into(), < ASausage as borsh::BorshSchema
> ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,15 @@ where
<AEggs as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASalad<C> as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASausage<W> as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let discriminant_2: u8 = 2u8;
let discriminant_3: u8 = 3u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "Eggs"
.to_string(), < AEggs as borsh::BorshSchema > ::declaration()),
(discriminant_2 as i64, "Salad".to_string(), < ASalad < C > as
borsh::BorshSchema > ::declaration()), (discriminant_3 as i64, "Sausage"
.to_string(), < ASausage < W > as borsh::BorshSchema > ::declaration())
(u8::from(0u8) as i64, "Bacon".into(), < ABacon as borsh::BorshSchema >
::declaration()), (u8::from(1u8) as i64, "Eggs".into(), < AEggs as
borsh::BorshSchema > ::declaration()), (u8::from(2u8) as i64, "Salad"
.into(), < ASalad < C > as borsh::BorshSchema > ::declaration()),
(u8::from(3u8) as i64, "Sausage".into(), < ASausage < W > as
borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,15 @@ where
<AEggs as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASalad<C> as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASausage<W, U> as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let discriminant_2: u8 = 2u8;
let discriminant_3: u8 = 3u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "Eggs"
.to_string(), < AEggs as borsh::BorshSchema > ::declaration()),
(discriminant_2 as i64, "Salad".to_string(), < ASalad < C > as
borsh::BorshSchema > ::declaration()), (discriminant_3 as i64, "Sausage"
.to_string(), < ASausage < W, U > as borsh::BorshSchema >
::declaration())
(u8::from(0u8) as i64, "Bacon".into(), < ABacon as borsh::BorshSchema >
::declaration()), (u8::from(1u8) as i64, "Eggs".into(), < AEggs as
borsh::BorshSchema > ::declaration()), (u8::from(2u8) as i64, "Salad"
.into(), < ASalad < C > as borsh::BorshSchema > ::declaration()),
(u8::from(3u8) as i64, "Sausage".into(), < ASausage < W, U > as
borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,15 @@ where
<AEggs as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASalad<C> as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASausage<W> as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let discriminant_2: u8 = 2u8;
let discriminant_3: u8 = 3u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "Eggs"
.to_string(), < AEggs as borsh::BorshSchema > ::declaration()),
(discriminant_2 as i64, "Salad".to_string(), < ASalad < C > as
borsh::BorshSchema > ::declaration()), (discriminant_3 as i64, "Sausage"
.to_string(), < ASausage < W > as borsh::BorshSchema > ::declaration())
(u8::from(0u8) as i64, "Bacon".into(), < ABacon as borsh::BorshSchema >
::declaration()), (u8::from(1u8) as i64, "Eggs".into(), < AEggs as
borsh::BorshSchema > ::declaration()), (u8::from(2u8) as i64, "Salad"
.into(), < ASalad < C > as borsh::BorshSchema > ::declaration()),
(u8::from(3u8) as i64, "Sausage".into(), < ASausage < W > as
borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ impl borsh::BorshSchema for A {
}
<AB as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ANegative as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "B".to_string(), < AB as borsh::BorshSchema >
::declaration()), (discriminant_1 as i64, "Negative".to_string(), <
ANegative as borsh::BorshSchema > ::declaration())
(u8::from(0u8) as i64, "B".into(), < AB as borsh::BorshSchema >
::declaration()), (u8::from(1u8) as i64, "Negative".into(), < ANegative
as borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,12 @@ where
<EnumParametrizedC<
T,
> as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "B".to_string(), < EnumParametrizedB < K, V > as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "C"
.to_string(), < EnumParametrizedC < T > as borsh::BorshSchema >
::declaration())
(u8::from(0u8) as i64, "B".into(), < EnumParametrizedB < K, V > as
borsh::BorshSchema > ::declaration()), (u8::from(1u8) as i64, "C".into(),
< EnumParametrizedC < T > as borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,12 @@ where
<EnumParametrizedC<
T,
> as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "B".to_string(), < EnumParametrizedB < K, V > as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "C"
.to_string(), < EnumParametrizedC < T > as borsh::BorshSchema >
::declaration())
(u8::from(0u8) as i64, "B".into(), < EnumParametrizedB < K, V > as
borsh::BorshSchema > ::declaration()), (u8::from(1u8) as i64, "C".into(),
< EnumParametrizedC < T > as borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ where
struct AC<K: Key>(K, Vec<A>);
<AB<K, V> as borsh::BorshSchema>::add_definitions_recursively(definitions);
<AC<K> as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "B".to_string(), < AB < K, V > as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "C"
.to_string(), < AC < K > as borsh::BorshSchema > ::declaration())
(u8::from(0u8) as i64, "B".into(), < AB < K, V > as borsh::BorshSchema >
::declaration()), (u8::from(1u8) as i64, "C".into(), < AC < K > as
borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ impl borsh::BorshSchema for A {
struct AEggs;
<ABacon as borsh::BorshSchema>::add_definitions_recursively(definitions);
<AEggs as borsh::BorshSchema>::add_definitions_recursively(definitions);
let discriminant_0: u8 = 0u8;
let discriminant_1: u8 = 1u8;
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "Eggs"
.to_string(), < AEggs as borsh::BorshSchema > ::declaration())
(u8::from(0u8) as i64, "Bacon".into(), < ABacon as borsh::BorshSchema >
::declaration()), (u8::from(1u8) as i64, "Eggs".into(), < AEggs as
borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(
Expand Down
Loading
Loading