Skip to content

Commit

Permalink
feat: avoid collisions of imports due to derive-generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
frol committed Mar 3, 2021
1 parent c62cdfb commit 8b1e237
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions borsh-derive-internal/src/struct_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn struct_de(input: &ItemStruct, cratename: Ident) -> syn::Result<TokenStrea
if let Some(method_ident) = init_method {
Ok(quote! {
impl #impl_generics #cratename::de::BorshDeserialize for #name #ty_generics #where_clause {
fn deserialize(buf: &mut &[u8]) -> core::result::Result<Self, #cratename::maybestd::io::Error> {
fn deserialize(buf: &mut &[u8]) -> ::core::result::Result<Self, #cratename::maybestd::io::Error> {
let mut return_value = #return_value;
return_value.#method_ident();
Ok(return_value)
Expand All @@ -74,7 +74,7 @@ pub fn struct_de(input: &ItemStruct, cratename: Ident) -> syn::Result<TokenStrea
} else {
Ok(quote! {
impl #impl_generics #cratename::de::BorshDeserialize for #name #ty_generics #where_clause {
fn deserialize(buf: &mut &[u8]) -> core::result::Result<Self, #cratename::maybestd::io::Error> {
fn deserialize(buf: &mut &[u8]) -> ::core::result::Result<Self, #cratename::maybestd::io::Error> {
Ok(#return_value)
}
}
Expand Down
8 changes: 4 additions & 4 deletions borsh-derive-internal/src/struct_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn struct_ser(input: &ItemStruct, cratename: Ident) -> syn::Result<TokenStre
}
Ok(quote! {
impl #impl_generics #cratename::ser::BorshSerialize for #name #ty_generics #where_clause {
fn serialize<W: #cratename::maybestd::io::Write>(&self, writer: &mut W) -> core::result::Result<(), #cratename::maybestd::io::Error> {
fn serialize<W: #cratename::maybestd::io::Write>(&self, writer: &mut W) -> ::core::result::Result<(), #cratename::maybestd::io::Error> {
#body
Ok(())
}
Expand Down Expand Up @@ -88,7 +88,7 @@ mod tests {
u64: borsh::ser::BorshSerialize,
String: borsh::ser::BorshSerialize
{
fn serialize<W: borsh::maybestd::io::Write>(&self, writer: &mut W) -> core::result::Result<(), borsh::maybestd::io::Error> {
fn serialize<W: borsh::maybestd::io::Write>(&self, writer: &mut W) -> ::core::result::Result<(), borsh::maybestd::io::Error> {
borsh::BorshSerialize::serialize(&self.x, writer)?;
borsh::BorshSerialize::serialize(&self.y, writer)?;
Ok(())
Expand All @@ -114,7 +114,7 @@ mod tests {
HashMap<K, V>: borsh::ser::BorshSerialize,
String: borsh::ser::BorshSerialize
{
fn serialize<W: borsh::maybestd::io::Write>(&self, writer: &mut W) -> core::result::Result<(), borsh::maybestd::io::Error> {
fn serialize<W: borsh::maybestd::io::Write>(&self, writer: &mut W) -> ::core::result::Result<(), borsh::maybestd::io::Error> {
borsh::BorshSerialize::serialize(&self.x, writer)?;
borsh::BorshSerialize::serialize(&self.y, writer)?;
Ok(())
Expand All @@ -141,7 +141,7 @@ mod tests {
HashMap<K, V>: borsh::ser::BorshSerialize,
String: borsh::ser::BorshSerialize
{
fn serialize<W: borsh::maybestd::io::Write>(&self, writer: &mut W) -> core::result::Result<(), borsh::maybestd::io::Error> {
fn serialize<W: borsh::maybestd::io::Write>(&self, writer: &mut W) -> ::core::result::Result<(), borsh::maybestd::io::Error> {
borsh::BorshSerialize::serialize(&self.x, writer)?;
borsh::BorshSerialize::serialize(&self.y, writer)?;
Ok(())
Expand Down
7 changes: 7 additions & 0 deletions borsh/tests/test_macro_namespace_collisions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Borsh macros should not collide with the local modules:
// https://github.com/near/borsh-rs/issues/11
mod std {}
mod core {}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
struct A;

0 comments on commit 8b1e237

Please sign in to comment.