Skip to content

Commit

Permalink
Merge pull request #146 from Fuuzetsu/borsh
Browse files Browse the repository at this point in the history
Optional borsh serialisation support
  • Loading branch information
mbrubeck committed Dec 4, 2023
2 parents 8111b34 + 8e5bb29 commit 8a87f6a
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
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
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

0 comments on commit 8a87f6a

Please sign in to comment.