Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional borsh serialisation support #146

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {
let float = crate::OrderedFloat(1.0f64);
let buffer = borsh::to_vec(&float).expect("failed to serialize value");
let deser_float: crate::OrderedFloat<f64> =
borsh::from_slice(&buffer).expect("failed to deserialize value");
assert_eq!(deser_float, float);
}

#[test]
fn test_not_nan() {
let float = crate::NotNan(1.0f64);
let buffer = borsh::to_vec(&float).expect("failed to serialize value");
let deser_float: crate::NotNan<f64> =
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