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

add rkyv support #520

Merged
merged 2 commits into from
Jun 7, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ all-features = true
arbitrary = { default-features = false, optional = true, version = "1.0" }
arrayvec = { default-features = false, version = "0.7" }
borsh = { default-features = false, optional = true, version = "0.9.3" }
bytecheck = { default-features= false, optional = true, version = "0.6" }
byteorder = { default-features = false, optional = true, version = "1.3" }
bytes = { default-features = false, optional = true, version = "1.0" }
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, features = ["size_32", "std"], optional = true, version = "0.7" }
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 +61,7 @@ default = ["serde", "std"]
legacy-ops = []
maths = []
maths-nopanic = ["maths"]
rkyv-safe = ["bytecheck", "rkyv", "rkyv/validation"]
rocket-traits = ["rocket"]
rust-fuzz = ["arbitrary"]
serde-arbitrary-precision = ["serde-with-arbitrary-precision"]
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", "--features=rkyv-safe"]

[tasks.test-rand]
command = "cargo"
args = ["test", "--workspace", "--features=rand"]
Expand Down
5 changes: 5 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,10 @@ 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`.
Supports rkyv's safe API when the `rkyv-safe` feature is enabled as well.

### `rocket-traits`

Enable support for Rocket forms by implementing the `FromFormField` trait.
Expand Down
11 changes: 11 additions & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::constants::{
use crate::ops;
use crate::Error;

#[cfg(feature = "rkyv-safe")]
use bytecheck::CheckBytes;
use core::{
cmp::{Ordering::Equal, *},
fmt,
Expand All @@ -19,6 +21,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 +105,13 @@ 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))
)]
#[cfg_attr(feature = "rkyv-safe", archive_attr(derive(CheckBytes)))]
pub struct Decimal {
// Bits 0-15: unused
// Bits 16-23: Contains "e", a value between 0-28 that indicates the scale
Expand Down
43 changes: 35 additions & 8 deletions tests/decimal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,33 @@ 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();

#[cfg(feature = "rkyv-safe")]
{
let archived = rkyv::check_archived_root::<Decimal>(&bytes[..]).unwrap();
assert_eq!(archived, &a);
}

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 +2583,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