Skip to content

Commit

Permalink
add rkyv-0.8 example
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Samuels committed Oct 4, 2024
1 parent ca982d6 commit 798ff79
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ criterion = { default-features = false, version = "0.5" }
csv = "1"
futures = { default-features = false, version = "0.3" }
rand = { default-features = false, features = ["getrandom"], version = "0.8" }
rkyv-0_8 = { version = "0.8", package = "rkyv" }
rust_decimal_macros = { default-features = false, version = "1.33" }
serde = { default-features = false, features = ["derive"], version = "1.0" }
serde_json = "1.0"
Expand Down Expand Up @@ -90,9 +91,12 @@ harness = false
name = "comparison"
path = "benches/comparison.rs"

[[example]]
name = "rkyv-remote"

[workspace]
members = [
".",
"./macros"
"./macros",
]
resolver = "2"
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,12 @@ two).

### `rkyv`

Enables [rkyv](https://github.com/rkyv/rkyv) serialization for `Decimal`.
Enables [rkyv](https://github.com/rkyv/rkyv) serialization for `Decimal`. In order to avoid breaking changes, this is currently locked at version `0.7`.

Supports rkyv's safe API when the `rkyv-safe` feature is enabled as well.

If `rkyv` support for versions `0.8` of greater is desired, `rkyv`'s [remote derives](https://rkyv.org/derive-macro-features/remote-derive.html) should be used instead. See `examples/rkyv-remote`.

### `rocket-traits`

Enable support for Rocket forms by implementing the `FromFormField` trait.
Expand Down
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ cargo run
This example shows how to use the `serde` crate to serialize and deserialize the `Decimal` type using multiple different
serialization formats.

## rkyv-remote

This example shows shows how to use the `rkyv` crate's remote derive for the `Decimal` type.
38 changes: 38 additions & 0 deletions examples/rkyv-remote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
extern crate rkyv_0_8 as rkyv;

use rkyv::{rancor::Error, Archive, Deserialize, Serialize};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;

/// The type containing a [`Decimal`] that will be de/serialized.
#[derive(Archive, Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Root {
#[rkyv(with = RkyvDecimal)]
decimal: Decimal,
}

/// Archived layout of [`Decimal`]
#[derive(Archive, Serialize, Deserialize)]
#[rkyv(remote = Decimal)]
struct RkyvDecimal {
#[rkyv(getter = Decimal::mantissa)]
mantissa: i128,
#[rkyv(getter = Decimal::scale)]
scale: u32,
}

impl From<RkyvDecimal> for Decimal {
fn from(RkyvDecimal { mantissa, scale }: RkyvDecimal) -> Self {
Self::from_i128_with_scale(mantissa, scale)
}
}

fn main() {
let test_value = Root { decimal: dec!(123.456) };

let bytes = rkyv::to_bytes::<Error>(&test_value).expect("Failed to serialize");

let roundtrip_value = rkyv::from_bytes::<Root, Error>(&bytes).expect("Failed to deserialize");

assert_eq!(test_value, roundtrip_value);
}
2 changes: 0 additions & 2 deletions examples/serde-json-scenarios/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ version = "0.0.0"
edition = "2021"
publish = false

[workspace]

[dependencies]
rust_decimal = { path = "../..", features = ["serde-with-arbitrary-precision"] }
rust_decimal_macros = { path = "../../macros" }
Expand Down

0 comments on commit 798ff79

Please sign in to comment.