File: rkyv-remote.rs

package info (click to toggle)
rust-rust-decimal 1.39.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,372 kB
  • sloc: makefile: 2
file content (35 lines) | stat: -rw-r--r-- 964 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
extern crate rkyv_0_8 as rkyv;

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

/// 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);
}