Skip to content

Commit

Permalink
Optional borsh serialisation support
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuuzetsu committed Dec 4, 2023
1 parent 8111b34 commit cf964be
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ arbitrary = { version = "1.0.0", optional = true }
proptest = { version = "1.0.0", optional = true }
speedy = { version = "0.8.3", optional = true, default-features = false }
bytemuck = { version = "1.12.2", optional = true, default-features = false }
borsh = { version = "1.2.0", optional = true, default-features = false }

[dev-dependencies]
serde_test = "1.0"
Expand Down
71 changes: 71 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,77 @@ mod impl_speedy {
}
}

#[cfg(feature = "borsh")]
mod impl_borsh {
extern crate borsh;
use super::{NotNan, OrderedFloat};
use num_traits::float::FloatCore;

impl<T> borsh::BorshSerialize for OrderedFloat<T>
where
T: borsh::BorshSerialize,
{
#[inline]
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
<T as borsh::BorshSerialize>::serialize(&self.0, writer)
}
}

impl<T> borsh::BorshDeserialize for OrderedFloat<T>
where
T: borsh::BorshDeserialize,
{
#[inline]
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
<T as borsh::BorshDeserialize>::deserialize_reader(reader).map(Self)
}
}

impl<T> borsh::BorshSerialize for NotNan<T>
where
T: borsh::BorshSerialize,
{
#[inline]
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
<T as borsh::BorshSerialize>::serialize(&self.0, writer)
}
}

impl<T> borsh::BorshDeserialize for NotNan<T>
where
T: FloatCore + borsh::BorshDeserialize,
{
#[inline]
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
let float = <T as borsh::BorshDeserialize>::deserialize_reader(reader)?;
NotNan::new(float).map_err(|_| {
borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
"expected a non-NaN float",
)
})
}
}

#[test]
fn test_ordered_float() {
type T = crate::OrderedFloat<f64>;
let float: T = crate::OrderedFloat(1.0f64);
let buffer = borsh::to_vec(&float).expect("failed to serialize value");
let deser_float: T = borsh::from_slice(&buffer).expect("failed to deserialize value");
assert_eq!(deser_float, float);
}

#[test]
fn test_not_nan() {
type T = crate::NotNan<f64>;
let float: T = crate::NotNan(1.0f64);
let buffer = borsh::to_vec(&float).expect("failed to serialize value");
let deser_float: T = borsh::from_slice(&buffer).expect("failed to deserialize value");
assert_eq!(deser_float, float);
}
}

#[cfg(all(feature = "std", feature = "schemars"))]
mod impl_schemars {
extern crate schemars;
Expand Down

0 comments on commit cf964be

Please sign in to comment.