Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
paupino authored Oct 9, 2024
2 parents 03eb303 + 7ae131e commit 03f2c29
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
8 changes: 6 additions & 2 deletions 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 @@ -72,7 +73,7 @@ proptest = ["dep:proptest"]
rand = ["dep:rand"]
rkyv = ["dep:rkyv"]
rkyv-safe = ["rkyv/validation"]
rocket-traits = ["dep:rocket"]
rocket-traits = ["dep:rocket", "std"]
rust-fuzz = ["dep:arbitrary"]
serde = ["dep:serde"]
serde-arbitrary-precision = ["serde-with-arbitrary-precision"]
Expand All @@ -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.
36 changes: 36 additions & 0 deletions examples/rkyv-remote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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::serialize)]
bytes: [u8; 16],
}

impl From<RkyvDecimal> for Decimal {
fn from(RkyvDecimal { bytes }: RkyvDecimal) -> Self {
Self::deserialize(bytes)
}
}

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
2 changes: 1 addition & 1 deletion src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ impl Decimal {
pub fn from_scientific(value: &str) -> Result<Decimal, Error> {
const ERROR_MESSAGE: &str = "Failed to parse";

let mut split = value.splitn(2, |c| c == 'e' || c == 'E');
let mut split = value.splitn(2, ['e', 'E']);

let base = split.next().ok_or_else(|| Error::from(ERROR_MESSAGE))?;
let exp = split.next().ok_or_else(|| Error::from(ERROR_MESSAGE))?;
Expand Down
2 changes: 2 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,10 @@ impl<'de> serde::de::Visitor<'de> for DecimalVisitor {
}
}

#[cfg(any(feature = "serde-with-float", feature = "serde-with-arbitrary-precision"))]
struct OptionDecimalVisitor;

#[cfg(any(feature = "serde-with-float", feature = "serde-with-arbitrary-precision"))]
impl<'de> serde::de::Visitor<'de> for OptionDecimalVisitor {
type Value = Option<Decimal>;

Expand Down

0 comments on commit 03f2c29

Please sign in to comment.