Skip to content

Commit

Permalink
fix: fully qualify #cratename::BorshSchema in derive-generated code…
Browse files Browse the repository at this point in the history
… to void function name collisions leading to compilation errors (#244)
  • Loading branch information
dj8yfo authored Oct 12, 2023
1 parent 49df318 commit 4ef9d00
Show file tree
Hide file tree
Showing 42 changed files with 378 additions and 144 deletions.
2 changes: 1 addition & 1 deletion borsh-derive/src/internals/attributes/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Attributes {
}
pub(crate) fn collect_bounds(&self, ty: BoundType) -> Vec<WherePredicate> {
let predicates = self.get_bounds(ty);
predicates.unwrap_or(vec![])
predicates.unwrap_or_default()
}
}

Expand Down
4 changes: 2 additions & 2 deletions borsh-derive/src/internals/schema/enums/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn process(input: &ItemEnum, cratename: Path) -> syn::Result<TokenStream2> {
tag_width: 1,
variants: #cratename::__private::maybestd::vec![#(#variants_defs),*],
};
#cratename::schema::add_definition(Self::declaration(), definition, definitions);
#cratename::schema::add_definition(<Self as #cratename::BorshSchema>::declaration(), definition, definitions);
}
};

Expand Down Expand Up @@ -147,7 +147,7 @@ fn process_variant(
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>::declaration())
(#discriminant_variable as i64, #variant_name.to_string(), <#full_variant_ident #inner_struct_ty_generics as #cratename::BorshSchema>::declaration())
};
Ok(VariantOutput {
inner_struct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,21 @@ impl borsh::BorshSchema for X {
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "A".to_string(), < XA > ::declaration()),
(discriminant_1 as i64, "B".to_string(), < XB > ::declaration()),
(discriminant_2 as i64, "C".to_string(), < XC > ::declaration()),
(discriminant_3 as i64, "D".to_string(), < XD > ::declaration()),
(discriminant_4 as i64, "E".to_string(), < XE > ::declaration()),
(discriminant_5 as i64, "F".to_string(), < XF > ::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,21 @@ impl borsh::BorshSchema for X {
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "A".to_string(), < XA > ::declaration()),
(discriminant_1 as i64, "B".to_string(), < XB > ::declaration()),
(discriminant_2 as i64, "C".to_string(), < XC > ::declaration()),
(discriminant_3 as i64, "D".to_string(), < XD > ::declaration()),
(discriminant_4 as i64, "E".to_string(), < XE > ::declaration()),
(discriminant_5 as i64, "F".to_string(), < XF > ::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ impl borsh::BorshSchema for A {
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon > ::declaration()),
(discriminant_1 as i64, "Eggs".to_string(), < AEggs > ::declaration()),
(discriminant_2 as i64, "Salad".to_string(), < ASalad > ::declaration()),
(discriminant_3 as i64, "Sausage".to_string(), < ASausage >
::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ where
{
fn declaration() -> borsh::schema::Declaration {
let params = borsh::__private::maybestd::vec![
< C > ::declaration(), < W > ::declaration()
< C as borsh::BorshSchema > ::declaration(), < W as borsh::BorshSchema >
::declaration()
];
format!(r#"{}<{}>"#, "A", params.join(", "))
}
Expand Down Expand Up @@ -49,14 +50,19 @@ where
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon > ::declaration()),
(discriminant_1 as i64, "Eggs".to_string(), < AEggs > ::declaration()),
(discriminant_2 as i64, "Salad".to_string(), < ASalad < C > >
::declaration()), (discriminant_3 as i64, "Sausage".to_string(), <
ASausage < W > > ::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ where
{
fn declaration() -> borsh::schema::Declaration {
let params = borsh::__private::maybestd::vec![
< U > ::declaration(), < C > ::declaration()
< U as borsh::BorshSchema > ::declaration(), < C as borsh::BorshSchema >
::declaration()
];
format!(r#"{}<{}>"#, "A", params.join(", "))
}
Expand Down Expand Up @@ -51,14 +52,20 @@ where
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon > ::declaration()),
(discriminant_1 as i64, "Eggs".to_string(), < AEggs > ::declaration()),
(discriminant_2 as i64, "Salad".to_string(), < ASalad < C > >
::declaration()), (discriminant_3 as i64, "Sausage".to_string(), <
ASausage < W, U > > ::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ where
W: borsh::BorshSchema,
{
fn declaration() -> borsh::schema::Declaration {
let params = borsh::__private::maybestd::vec![< W > ::declaration()];
let params = borsh::__private::maybestd::vec![
< W as borsh::BorshSchema > ::declaration()
];
format!(r#"{}<{}>"#, "A", params.join(", "))
}
fn add_definitions_recursively(
Expand Down Expand Up @@ -50,14 +52,19 @@ where
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon > ::declaration()),
(discriminant_1 as i64, "Eggs".to_string(), < AEggs > ::declaration()),
(discriminant_2 as i64, "Salad".to_string(), < ASalad < C > >
::declaration()), (discriminant_3 as i64, "Sausage".to_string(), <
ASausage < W > > ::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ impl borsh::BorshSchema for A {
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "B".to_string(), < AB > ::declaration()),
(discriminant_1 as i64, "Negative".to_string(), < ANegative >
::declaration())
(discriminant_0 as i64, "B".to_string(), < AB as borsh::BorshSchema >
::declaration()), (discriminant_1 as i64, "Negative".to_string(), <
ANegative as borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ where
{
fn declaration() -> borsh::schema::Declaration {
let params = borsh::__private::maybestd::vec![
< T > ::declaration(), < K > ::declaration(), < K::Associated >
::declaration(), < V > ::declaration()
< T as borsh::BorshSchema > ::declaration(), < K as borsh::BorshSchema >
::declaration(), < K::Associated as borsh::BorshSchema > ::declaration(), < V
as borsh::BorshSchema > ::declaration()
];
format!(r#"{}<{}>"#, "EnumParametrized", params.join(", "))
}
Expand Down Expand Up @@ -60,12 +61,17 @@ where
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "B".to_string(), < EnumParametrizedB < K, V > >
::declaration()), (discriminant_1 as i64, "C".to_string(), <
EnumParametrizedC < T > > ::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ where
{
fn declaration() -> borsh::schema::Declaration {
let params = borsh::__private::maybestd::vec![
< T > ::declaration(), < K > ::declaration(), < < K as TraitName >
::Associated > ::declaration(), < V > ::declaration()
< T as borsh::BorshSchema > ::declaration(), < K as borsh::BorshSchema >
::declaration(), < < K as TraitName > ::Associated as borsh::BorshSchema >
::declaration(), < V as borsh::BorshSchema > ::declaration()
];
format!(r#"{}<{}>"#, "EnumParametrized", params.join(", "))
}
Expand Down Expand Up @@ -61,12 +62,17 @@ where
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "B".to_string(), < EnumParametrizedB < K, V > >
::declaration()), (discriminant_1 as i64, "C".to_string(), <
EnumParametrizedC < T > > ::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ where
{
fn declaration() -> borsh::schema::Declaration {
let params = borsh::__private::maybestd::vec![
< K > ::declaration(), < V > ::declaration()
< K as borsh::BorshSchema > ::declaration(), < V as borsh::BorshSchema >
::declaration()
];
format!(r#"{}<{}>"#, "A", params.join(", "))
}
Expand Down Expand Up @@ -41,12 +42,16 @@ where
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "B".to_string(), < AB < K, V > >
::declaration()), (discriminant_1 as i64, "C".to_string(), < AC < K > >
::declaration())
(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())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ impl borsh::BorshSchema for A {
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon > ::declaration()),
(discriminant_1 as i64, "Eggs".to_string(), < AEggs > ::declaration())
(discriminant_0 as i64, "Bacon".to_string(), < ABacon as
borsh::BorshSchema > ::declaration()), (discriminant_1 as i64, "Eggs"
.to_string(), < AEggs as borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ impl reexporter::borsh::BorshSchema for A {
let definition = reexporter::borsh::schema::Definition::Enum {
tag_width: 1,
variants: reexporter::borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon > ::declaration()),
(discriminant_1 as i64, "Eggs".to_string(), < AEggs > ::declaration())
(discriminant_0 as i64, "Bacon".to_string(), < ABacon as
reexporter::borsh::BorshSchema > ::declaration()), (discriminant_1 as
i64, "Eggs".to_string(), < AEggs as reexporter::borsh::BorshSchema >
::declaration())
],
};
reexporter::borsh::schema::add_definition(
Self::declaration(),
<Self as reexporter::borsh::BorshSchema>::declaration(),
definition,
definitions,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ impl borsh::BorshSchema for A {
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
(discriminant_0 as i64, "Bacon".to_string(), < ABacon > ::declaration())
(discriminant_0 as i64, "Bacon".to_string(), < ABacon as
borsh::BorshSchema > ::declaration())
],
};
borsh::schema::add_definition(Self::declaration(), definition, definitions);
borsh::schema::add_definition(
<Self as borsh::BorshSchema>::declaration(),
definition,
definitions,
);
}
}

Loading

0 comments on commit 4ef9d00

Please sign in to comment.