Repository: near/borsh-rs
Pull request #102 (2022-08-15 15:29:01)

fix: codegen sanity for enum serialization
structs were fixed in https://github.com/near/borsh-rs/commit/985a20b0289b034a53fe6b2e3c799349d8cbd95f but this was not done for enums.

cc @BenKurrek


Patch:
diff --git a/borsh-derive-internal/src/enum_de.rs b/borsh-derive-internal/src/enum_de.rs
index 92ef2d96..97cfde85 100644
--- a/borsh-derive-internal/src/enum_de.rs
+++ b/borsh-derive-internal/src/enum_de.rs
@@ -77,7 +77,7 @@ pub fn enum_de(input: &ItemEnum, cratename: Ident) -> syn::Result<TokenStream2>
     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> {
                     #variant_idx
                     let mut return_value = match variant_idx {
                         #variant_arms
@@ -98,7 +98,7 @@ pub fn enum_de(input: &ItemEnum, cratename: Ident) -> syn::Result<TokenStream2>
     } 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> {
                     #variant_idx
                     let return_value = match variant_idx {
                         #variant_arms
diff --git a/borsh-derive-internal/src/enum_ser.rs b/borsh-derive-internal/src/enum_ser.rs
index 11a7caeb..a625a984 100644
--- a/borsh-derive-internal/src/enum_ser.rs
+++ b/borsh-derive-internal/src/enum_ser.rs
@@ -94,7 +94,7 @@ pub fn enum_ser(input: &ItemEnum, cratename: Ident) -> syn::Result<TokenStream2>
     }
     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> {
                 let variant_idx: u8 = match self {
                     #variant_idx_body
                 };
diff --git a/borsh/src/de/mod.rs b/borsh/src/de/mod.rs
index f4b2d940..91ecf26a 100644
--- a/borsh/src/de/mod.rs
+++ b/borsh/src/de/mod.rs
@@ -572,8 +572,8 @@ where
     fn deserialize(buf: &mut &[u8]) -> Result<Self> {
         let mut result = [T::default(); N];
         if N > 0 && !T::copy_from_bytes(buf, &mut result)? {
-            for i in 0..N {
-                result[i] = T::deserialize(buf)?;
+            for i in result.iter_mut() {
+                *i = T::deserialize(buf)?;
             }
         }
         Ok(result)
diff --git a/borsh/src/schema.rs b/borsh/src/schema.rs
index f0ea4db0..1183eee8 100644
--- a/borsh/src/schema.rs
+++ b/borsh/src/schema.rs
@@ -29,7 +29,7 @@ pub type VariantName = String;
 /// The name of the field in the struct (can be used to convert JSON to Borsh using the schema).
 pub type FieldName = String;
 /// The type that we use to represent the definition of the Borsh type.
-#[derive(Clone, PartialEq, Debug, BorshSerialize, BorshDeserialize, BorshSchemaMacro)]
+#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize, BorshSchemaMacro)]
 pub enum Definition {
     /// A fixed-size array with the length known at the compile time and the same-type elements.
     Array { length: u32, elements: Declaration },
@@ -47,7 +47,7 @@ pub enum Definition {
 }
 
 /// The collection representing the fields of a struct.
-#[derive(Clone, PartialEq, Debug, BorshSerialize, BorshDeserialize, BorshSchemaMacro)]
+#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize, BorshSchemaMacro)]
 pub enum Fields {
     /// The struct with named fields.
     NamedFields(Vec<(FieldName, Declaration)>),
@@ -58,7 +58,7 @@ pub enum Fields {
 }
 
 /// All schema information needed to deserialize a single type.
-#[derive(Clone, PartialEq, Debug, BorshSerialize, BorshDeserialize, BorshSchemaMacro)]
+#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize, BorshSchemaMacro)]
 pub struct BorshSchemaContainer {
     /// Declaration of the type.
     pub declaration: Declaration,
diff --git a/borsh/src/ser/mod.rs b/borsh/src/ser/mod.rs
index b3244c00..8cc03c9e 100644
--- a/borsh/src/ser/mod.rs
+++ b/borsh/src/ser/mod.rs
@@ -153,7 +153,7 @@ impl_for_float!(f64);
 impl BorshSerialize for bool {
     #[inline]
     fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
-        (if *self { 1u8 } else { 0u8 }).serialize(writer)
+        (u8::from(*self)).serialize(writer)
     }
 }
 


Test patch:
diff --git a/borsh/tests/test_macro_namespace_collisions.rs b/borsh/tests/test_macro_namespace_collisions.rs
index a035350c..f228ae5b 100644
--- a/borsh/tests/test_macro_namespace_collisions.rs
+++ b/borsh/tests/test_macro_namespace_collisions.rs
@@ -5,3 +5,9 @@ mod core {}
 
 #[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
 struct A;
+
+#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
+enum B {
+    C,
+    D,
+}