Skip to content

Commit 8e5bb29

Browse files
committed
Optional borsh serialisation support
1 parent 8111b34 commit 8e5bb29

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ arbitrary = { version = "1.0.0", optional = true }
2424
proptest = { version = "1.0.0", optional = true }
2525
speedy = { version = "0.8.3", optional = true, default-features = false }
2626
bytemuck = { version = "1.12.2", optional = true, default-features = false }
27+
borsh = { version = "1.2.0", optional = true, default-features = false }
2728

2829
[dev-dependencies]
2930
serde_test = "1.0"

src/lib.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,77 @@ mod impl_speedy {
21732173
}
21742174
}
21752175

2176+
#[cfg(feature = "borsh")]
2177+
mod impl_borsh {
2178+
extern crate borsh;
2179+
use super::{NotNan, OrderedFloat};
2180+
use num_traits::float::FloatCore;
2181+
2182+
impl<T> borsh::BorshSerialize for OrderedFloat<T>
2183+
where
2184+
T: borsh::BorshSerialize,
2185+
{
2186+
#[inline]
2187+
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
2188+
<T as borsh::BorshSerialize>::serialize(&self.0, writer)
2189+
}
2190+
}
2191+
2192+
impl<T> borsh::BorshDeserialize for OrderedFloat<T>
2193+
where
2194+
T: borsh::BorshDeserialize,
2195+
{
2196+
#[inline]
2197+
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
2198+
<T as borsh::BorshDeserialize>::deserialize_reader(reader).map(Self)
2199+
}
2200+
}
2201+
2202+
impl<T> borsh::BorshSerialize for NotNan<T>
2203+
where
2204+
T: borsh::BorshSerialize,
2205+
{
2206+
#[inline]
2207+
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
2208+
<T as borsh::BorshSerialize>::serialize(&self.0, writer)
2209+
}
2210+
}
2211+
2212+
impl<T> borsh::BorshDeserialize for NotNan<T>
2213+
where
2214+
T: FloatCore + borsh::BorshDeserialize,
2215+
{
2216+
#[inline]
2217+
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
2218+
let float = <T as borsh::BorshDeserialize>::deserialize_reader(reader)?;
2219+
NotNan::new(float).map_err(|_| {
2220+
borsh::io::Error::new(
2221+
borsh::io::ErrorKind::InvalidData,
2222+
"expected a non-NaN float",
2223+
)
2224+
})
2225+
}
2226+
}
2227+
2228+
#[test]
2229+
fn test_ordered_float() {
2230+
let float = crate::OrderedFloat(1.0f64);
2231+
let buffer = borsh::to_vec(&float).expect("failed to serialize value");
2232+
let deser_float: crate::OrderedFloat<f64> =
2233+
borsh::from_slice(&buffer).expect("failed to deserialize value");
2234+
assert_eq!(deser_float, float);
2235+
}
2236+
2237+
#[test]
2238+
fn test_not_nan() {
2239+
let float = crate::NotNan(1.0f64);
2240+
let buffer = borsh::to_vec(&float).expect("failed to serialize value");
2241+
let deser_float: crate::NotNan<f64> =
2242+
borsh::from_slice(&buffer).expect("failed to deserialize value");
2243+
assert_eq!(deser_float, float);
2244+
}
2245+
}
2246+
21762247
#[cfg(all(feature = "std", feature = "schemars"))]
21772248
mod impl_schemars {
21782249
extern crate schemars;

0 commit comments

Comments
 (0)