Debian Codegraph @

examples/rkyv-remote.rs

raw symbols SCIP
1 extern crate rkyv_0_8 as rkyv;
2
3 use rkyv::{rancor::Error, Archive, Deserialize, Serialize};
4 use rust_decimal::prelude::{dec, Decimal};
5
6 /// The type containing a [`Decimal`] that will be de/serialized.
7 #[derive(Archive, Serialize, Deserialize, Debug, PartialEq, Eq)]
8 struct Root {
9 #[rkyv(with = RkyvDecimal)]
10 decimal: Decimal,
11 }
12
13 /// Archived layout of [`Decimal`]
14 #[derive(Archive, Serialize, Deserialize)]
15 #[rkyv(remote = Decimal)]
16 struct RkyvDecimal {
17 #[rkyv(getter = Decimal::serialize)]
18 bytes: [u8; 16],
19 }
20
21 impl From<RkyvDecimal> for Decimal {
22 fn from(RkyvDecimal { bytes }: RkyvDecimal) -> Self {
23 Self::deserialize(bytes)
24 }
25 }
26
27 fn main() {
28 let test_value = Root { decimal: dec!(123.456) };
29
30 let bytes = rkyv::to_bytes::<Error>(&test_value).expect("Failed to serialize");
31
32 let roundtrip_value = rkyv::from_bytes::<Root, Error>(&bytes).expect("Failed to deserialize");
33
34 assert_eq!(test_value, roundtrip_value);
35 }