Repository: near/borsh-rs
Pull request #124 (2023-02-03 19:44:32)

Implement serialize/deserialize for core::ops::Range

preston-evans98: > @preston-evans98 Overall, it looks good to me. Could you also add tests and a record to the CHANGELOG file in the root of the project?

Sure thing! Clippy is failing for reasons that look (at a glance) unrelated to this change. Have you encountered those before? If not, I can dig in and debug.
preston-evans98: It turned out the `clippy` issue was just some new lints added since the last pull. I've fixed the warnings and rebased on top.

In case you prefer to keep the PRs separate, I've opened #125. Feel free to close without comment if you'd prefer to merge both changes together.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cbe42f4b..b18ea148 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+- Implemented (de)ser for `core::ops::range`
+
 ## [0.10.0] - 2023-01-19
 
 - Fix no-std feature (some of the imports incorrectly used `std::` instead of `crate::maybestd::`)
diff --git a/borsh-derive-internal/src/lib.rs b/borsh-derive-internal/src/lib.rs
index e01b99c8..01ef4074 100644
--- a/borsh-derive-internal/src/lib.rs
+++ b/borsh-derive-internal/src/lib.rs
@@ -1,4 +1,6 @@
 #![recursion_limit = "128"]
+// TODO: re-enable this lint when we bump msrv to 1.58
+#![allow(clippy::uninlined_format_args)]
 
 mod attribute_helpers;
 mod enum_de;
diff --git a/borsh-derive/src/lib.rs b/borsh-derive/src/lib.rs
index 55f368b8..86ae04d5 100644
--- a/borsh-derive/src/lib.rs
+++ b/borsh-derive/src/lib.rs
@@ -1,3 +1,5 @@
+// TODO: re-enable this lint when we bump msrv to 1.58
+#![allow(clippy::uninlined_format_args)]
 extern crate proc_macro;
 use proc_macro::TokenStream;
 use proc_macro2::Span;
diff --git a/borsh-schema-derive-internal/src/lib.rs b/borsh-schema-derive-internal/src/lib.rs
index b75c1ee6..23c1bb89 100644
--- a/borsh-schema-derive-internal/src/lib.rs
+++ b/borsh-schema-derive-internal/src/lib.rs
@@ -1,4 +1,6 @@
 #![recursion_limit = "128"]
+// TODO: re-enable this lint when we bump msrv to 1.58
+#![allow(clippy::uninlined_format_args)]
 
 mod helpers;
 
diff --git a/borsh/src/de/mod.rs b/borsh/src/de/mod.rs
index 973af3f7..c90d030b 100644
--- a/borsh/src/de/mod.rs
+++ b/borsh/src/de/mod.rs
@@ -281,6 +281,19 @@ impl BorshDeserialize for bool {
     }
 }
 
+impl<T> BorshDeserialize for core::ops::Range<T>
+where
+    T: BorshDeserialize,
+{
+    #[inline]
+    fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
+        Ok(Self {
+            start: T::deserialize_reader(reader)?,
+            end: T::deserialize_reader(reader)?,
+        })
+    }
+}
+
 impl<T> BorshDeserialize for Option<T>
 where
     T: BorshDeserialize,
diff --git a/borsh/src/generate_schema_schema.rs b/borsh/src/generate_schema_schema.rs
index f93bdb32..ed0fcac9 100644
--- a/borsh/src/generate_schema_schema.rs
+++ b/borsh/src/generate_schema_schema.rs
@@ -1,4 +1,6 @@
 //! Generate `BorshSchemaCointainer` for `BorshSchemaContainer` and save it into a file.
+// TODO: re-enable this lint when we bump msrv to 1.58
+#![allow(clippy::uninlined_format_args)]
 use borsh::schema::BorshSchema;
 use borsh::BorshSerialize;
 use std::fs::File;
diff --git a/borsh/src/lib.rs b/borsh/src/lib.rs
index 8db27c42..cf51b18a 100644
--- a/borsh/src/lib.rs
+++ b/borsh/src/lib.rs
@@ -1,4 +1,6 @@
 #![cfg_attr(not(feature = "std"), no_std)]
+// TODO: re-enable this lint when we bump msrv to 1.58
+#![allow(clippy::uninlined_format_args)]
 
 #[cfg(not(feature = "std"))]
 extern crate alloc;
diff --git a/borsh/src/ser/mod.rs b/borsh/src/ser/mod.rs
index 84e0573a..b905db07 100644
--- a/borsh/src/ser/mod.rs
+++ b/borsh/src/ser/mod.rs
@@ -157,6 +157,17 @@ impl BorshSerialize for bool {
     }
 }
 
+impl<T> BorshSerialize for core::ops::Range<T>
+where
+    T: BorshSerialize,
+{
+    #[inline]
+    fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
+        self.start.serialize(writer)?;
+        self.end.serialize(writer)
+    }
+}
+
 impl<T> BorshSerialize for Option<T>
 where
     T: BorshSerialize,


Test patch:
diff --git a/borsh/tests/test_simple_structs.rs b/borsh/tests/test_simple_structs.rs
index deb74985..ce484828 100644
--- a/borsh/tests/test_simple_structs.rs
+++ b/borsh/tests/test_simple_structs.rs
@@ -23,6 +23,7 @@ struct A<'a> {
     lazy: Option<u64>,
     c: std::borrow::Cow<'a, str>,
     cow_arr: std::borrow::Cow<'a, [std::borrow::Cow<'a, str>]>,
+    range_u32: std::ops::Range<u32>,
     #[borsh_skip]
     skipped: Option<u64>,
 }
@@ -105,6 +106,7 @@ fn test_simple_struct() {
         lazy: Some(5),
         c: std::borrow::Cow::Borrowed("Hello"),
         cow_arr: std::borrow::Cow::Borrowed(&cow_arr),
+        range_u32: 12..71,
         skipped: Some(6),
     };
     let encoded_a = a.try_to_vec().unwrap();
@@ -140,6 +142,7 @@ fn test_simple_struct() {
             std::borrow::Cow::Borrowed("Hello1"),
             std::borrow::Cow::Owned("Hello2".to_string()),
         ]),
+        range_u32: 12..71,
         skipped: None,
     };