forked from Synphonyte/codee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrkyv.rs
More file actions
61 lines (53 loc) · 1.65 KB
/
rkyv.rs
File metadata and controls
61 lines (53 loc) · 1.65 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::{Decoder, Encoder};
use std::error::Error;
use std::sync::Arc;
use rkyv::rancor::{Fallible, Strategy};
use rkyv::ser::allocator::ArenaHandle;
use rkyv::{bytecheck, rancor, Archive, Deserialize, Serialize};
use rkyv::api::high::{HighSerializer, HighValidator};
use rkyv::de::Pool;
/// A codec that relies on `rkyv` to encode data in the msgpack format.
///
/// This is only available with the **`rkyv` feature** enabled.
pub struct RkyvCodec;
impl<T> Encoder<T> for RkyvCodec
where
T: for<'a> Serialize<HighSerializer<Vec<u8>, ArenaHandle<'a>, rancor::Error>>
{
type Error = rancor::Error;
type Encoded = Vec<u8>;
fn encode(val: &T) -> Result<Self::Encoded, Self::Error> {
Ok(rkyv::api::high::to_bytes_in(val, Vec::new())?)
}
}
impl<T> Decoder<T> for RkyvCodec
where
T: Archive,
for<'a> T::Archived:
'a + bytecheck::CheckBytes<HighValidator<'a, rancor::Error>> + Deserialize<T, Strategy<Pool, rancor::Error>>
{
type Error = Arc<dyn Error>;
type Encoded = [u8];
fn decode(val: &Self::Encoded) -> Result<T, Self::Error> {
rkyv::from_bytes::<T, rancor::Error>(val).map_err(|e| Arc::new(e) as Arc<dyn Error>)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rkyv_codec() {
#[derive(Clone, Debug, PartialEq, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
struct Test {
s: String,
i: i32,
}
let t = Test {
s: String::from("party time 🎉"),
i: 42,
};
let enc = RkyvCodec::encode(&t).unwrap();
let dec: Test = RkyvCodec::decode(&enc).unwrap();
assert_eq!(dec, t);
}
}