Skip to content

Commit

Permalink
add rkyv support
Browse files Browse the repository at this point in the history
  • Loading branch information
dovahcrow committed Jun 7, 2022
1 parent 8c27247 commit f39606e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ diesel = { default-features = false, optional = true, version = "1.4" }
num-traits = { default-features = false, features = ["i128"], version = "0.2" }
postgres = { default-features = false, optional = true, version = "0.19" }
rand = { default-features = false, optional = true, version = "0.8" }
rkyv = {default-features = false, version = "0.7", optional = true}
rocket = { default-features = false, optional = true, version = "0.5.0-rc.1" }
serde = { default-features = false, optional = true, version = "1.0" }
serde_json = { default-features = false, optional = true, version = "1.0" }
Expand Down Expand Up @@ -59,6 +60,7 @@ default = ["serde", "std"]
legacy-ops = []
maths = []
maths-nopanic = ["maths"]
rkyv = ["rkyv/size_32"]
rocket-traits = ["rocket"]
rust-fuzz = ["arbitrary"]
serde-arbitrary-precision = ["serde-with-arbitrary-precision"]
Expand All @@ -68,7 +70,7 @@ serde-str = ["serde-with-str"]
serde-with-arbitrary-precision = ["serde", "serde_json/arbitrary_precision", "serde_json/std"]
serde-with-float = ["serde"]
serde-with-str = ["serde"]
std = []
std = ["rkyv/std"]
tokio-pg = ["db-tokio-postgres"] # Backwards compatability

[workspace]
Expand Down
7 changes: 6 additions & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ args = ["test", "--workspace", "--no-default-features", "--features=maths-nopani
[tasks.test-misc]
dependencies = [
"test-rust-fuzz",
"test-borsh"
"test-borsh",
"test-rkyv"
]

[tasks.test-rust-fuzz]
Expand Down Expand Up @@ -251,6 +252,10 @@ args = ["test", "--workspace", "--tests", "--features=serde-with-str", "serde",
command = "cargo"
args = ["test", "--workspace", "--features=borsh"]

[tasks.test-rkyv]
command = "cargo"
args = ["test", "--workspace", "--features=rkyv"]

[tasks.test-rand]
command = "cargo"
args = ["test", "--workspace", "--features=rand"]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ assert_eq!(total.to_string(), "27.26");
* [c-repr](#c-repr)
* [legacy-ops](#legacy-ops)
* [maths](#maths)
* [rkyv](#rkyv)
* [rocket-traits](#rocket-traits)
* [rust-fuzz](#rust-fuzz)
* [std](#std)
Expand Down Expand Up @@ -144,6 +145,9 @@ non-panicking behavior, please use the feature: `maths-nopanic`.

Implements `rand::distributions::Distribution<Decimal>` to allow the creation of random instances.

### `rkyv`
Enables [rkyv](https://github.com/rkyv/rkyv) serialization for `Decimal`.

### `rocket-traits`

Enable support for Rocket forms by implementing the `FromFormField` trait.
Expand Down
8 changes: 8 additions & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use diesel::sql_types::Numeric;
#[cfg(not(feature = "std"))]
use num_traits::float::FloatCore;
use num_traits::{FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

/// The smallest value that can be represented by this decimal type.
const MIN: Decimal = Decimal {
Expand Down Expand Up @@ -101,6 +103,12 @@ pub struct UnpackedDecimal {
feature = "borsh",
derive(borsh::BorshDeserialize, borsh::BorshSerialize, borsh::BorshSchema)
)]
#[cfg_attr(
feature = "rkyv",
derive(Archive, Deserialize, Serialize),
archive(compare(PartialEq)),
archive_attr(derive(Debug))
)]
pub struct Decimal {
// Bits 0-15: unused
// Bits 16-23: Contains "e", a value between 0-28 that indicates the scale
Expand Down
37 changes: 29 additions & 8 deletions tests/decimal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,27 @@ fn it_can_serialize_deserialize_borsh() {
}
}

#[test]
#[cfg(feature = "rkyv")]
fn it_can_serialize_deserialize_rkyv() {
use rkyv::Deserialize;
let tests = [
"12.3456789",
"5233.9008808150288439427720175",
"-5233.9008808150288439427720175",
];
for test in &tests {
let a = Decimal::from_str(test).unwrap();
let bytes = rkyv::to_bytes::<_, 256>(&a).unwrap();

let archived = unsafe { rkyv::archived_root::<Decimal>(&bytes[..]) };
assert_eq!(archived, &a);

let deserialized: Decimal = archived.deserialize(&mut rkyv::Infallible).unwrap();
assert_eq!(deserialized, a);
}
}

#[test]
fn it_can_deserialize_unbounded_values() {
// Mantissa for these: 19393111376951473493673267553
Expand Down Expand Up @@ -2556,14 +2577,14 @@ fn it_can_return_the_min_value() {
#[test]
fn it_can_go_from_and_into() {
let d = Decimal::from_str("5").unwrap();
let di8 = 5u8.into();
let di32 = 5i32.into();
let disize = 5isize.into();
let di64 = 5i64.into();
let du8 = 5u8.into();
let du32 = 5u32.into();
let dusize = 5usize.into();
let du64 = 5u64.into();
let di8: Decimal = 5u8.into();
let di32: Decimal = 5i32.into();
let disize: Decimal = 5isize.into();
let di64: Decimal = 5i64.into();
let du8: Decimal = 5u8.into();
let du32: Decimal = 5u32.into();
let dusize: Decimal = 5usize.into();
let du64: Decimal = 5u64.into();

assert_eq!(d, di8);
assert_eq!(di8, di32);
Expand Down

0 comments on commit f39606e

Please sign in to comment.