Skip to content

Commit

Permalink
feat: add BorshSerialize & BorshDeserialize behind feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Sep 13, 2024
1 parent 6e3d361 commit 87dbf77
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 18 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to `const-decimal`.

## Unreleased

- Added `BorshSerialize` and `BorshDeserialize` behind `borsh` feature flag.
- Added `AddAssign`, `SubAssign`, `MulAssign`, and `DivAssign`.

## 0.1.0

- Initialize release.
66 changes: 66 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ module_name_repetitions = "allow"

[features]
serde = ["dep:serde"]
borsh = ["dep:borsh"]

[dependencies]
borsh = { version = "1.5.1", features = ["derive"], optional = true }
num-traits = "0.2.19"
paste = "1.0.15"
ruint = "1.12.3"
Expand Down
22 changes: 4 additions & 18 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssi
use crate::integer::{Integer, SignedInteger};

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Decimal<I, const D: u8>(pub I);
Expand Down Expand Up @@ -153,21 +154,6 @@ mod tests {

use super::*;

macro_rules! apply_to_common_variants {
($macro:ident) => {
$macro!(u8, 1);
$macro!(i8, 1);
$macro!(u16, 2);
$macro!(i16, 2);
$macro!(u32, 5);
$macro!(i32, 5);
$macro!(u64, 9);
$macro!(i64, 9);
$macro!(u128, 18);
$macro!(i128, 18);
};
}

macro_rules! test_basic_ops {
($underlying:ty, $decimals:literal) => {
paste! {
Expand Down Expand Up @@ -604,7 +590,7 @@ mod tests {
});
}

apply_to_common_variants!(test_basic_ops);
apply_to_common_variants!(fuzz_against_primitive);
apply_to_common_variants!(differential_fuzz);
crate::macros::apply_to_common_variants!(test_basic_ops);
crate::macros::apply_to_common_variants!(fuzz_against_primitive);
crate::macros::apply_to_common_variants!(differential_fuzz);
}
22 changes: 22 additions & 0 deletions src/foreign_traits/borsh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[cfg(test)]
mod tests {
use borsh::{BorshDeserialize, BorshSerialize};
use proptest::prelude::*;

use crate::macros::generate_tests_for_common_variants;
use crate::{Decimal, Integer};

generate_tests_for_common_variants!(round_trip_borsh);

fn round_trip_borsh<I, const D: u8>()
where
I: Integer<D> + Arbitrary + BorshSerialize + BorshDeserialize,
{
proptest!(|(input: Decimal<I, D>)| {
let serialized = borsh::to_vec(&input).unwrap();
let recovered = borsh::from_slice(&serialized).unwrap();

prop_assert_eq!(input, recovered);
});
}
}
2 changes: 2 additions & 0 deletions src/foreign_traits/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "borsh")]
mod borsh;
#[cfg(test)]
mod malachite;
#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ mod foreign_traits;
mod full_mul_div;
/// Trait definition for underlying integer.
mod integer;
/// Macros used in tests.
#[cfg(test)]
#[macro_use]
pub(crate) mod macros;

pub use aliases::*;
pub use decimal::*;
Expand Down
42 changes: 42 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
macro_rules! apply_to_common_variants {
($macro:ident) => {
$macro!(u8, 1);
$macro!(i8, 1);
$macro!(u16, 2);
$macro!(i16, 2);
$macro!(u32, 5);
$macro!(i32, 5);
$macro!(u64, 9);
$macro!(i64, 9);
$macro!(u128, 18);
$macro!(i128, 18);
};
}

macro_rules! generate_tests_for_common_variants {
($f:ident) => {
crate::macros::generate_test!($f, u8, 1);
crate::macros::generate_test!($f, i8, 1);
crate::macros::generate_test!($f, u16, 2);
crate::macros::generate_test!($f, i16, 2);
crate::macros::generate_test!($f, u32, 5);
crate::macros::generate_test!($f, i32, 5);
crate::macros::generate_test!($f, u64, 9);
crate::macros::generate_test!($f, i64, 9);
crate::macros::generate_test!($f, u128, 18);
crate::macros::generate_test!($f, i128, 18);
};
}

macro_rules! generate_test {
($f:ident, $underlying:ty, $decimals:literal) => {
::paste::paste! {
#[test]
fn [<$f _ $underlying _ $decimals >]() {
$f::<$underlying, $decimals>();
}
}
};
}

pub(crate) use {apply_to_common_variants, generate_test, generate_tests_for_common_variants};

0 comments on commit 87dbf77

Please sign in to comment.