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 9eec22f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 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", "rkyv/std"]
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"]

[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
3 changes: 3 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,7 @@ pub struct UnpackedDecimal {
feature = "borsh",
derive(borsh::BorshDeserialize, borsh::BorshSerialize, borsh::BorshSchema)
)]
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
pub struct Decimal {
// Bits 0-15: unused
// Bits 16-23: Contains "e", a value between 0-28 that indicates the scale
Expand Down

0 comments on commit 9eec22f

Please sign in to comment.