-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbitcode.rs
More file actions
82 lines (72 loc) · 2.3 KB
/
Copy pathbitcode.rs
File metadata and controls
82 lines (72 loc) · 2.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/// A codec that relies on `bitcode` to encode data.
///
/// This is only available with the **`bitcode` feature** feature enabled.
///
/// If you want to use `serde` with bitcode, please use the `bitcode_serde` feature instead.
pub struct BitcodeCodec;
impl<T: bitcode::Encode> crate::Encoder<T> for BitcodeCodec {
type Error = ();
type Encoded = Vec<u8>;
fn encode(val: &T) -> Result<Self::Encoded, Self::Error> {
Ok(bitcode::encode(val))
}
}
impl<T: for<'a> bitcode::Decode<'a>> crate::Decoder<T> for BitcodeCodec {
type Error = bitcode::Error;
type Encoded = [u8];
fn decode(val: &Self::Encoded) -> Result<T, Self::Error> {
bitcode::decode(val)
}
}
#[cfg(test)]
mod tests {
use crate::{Decoder, Encoder};
use super::*;
#[test]
fn test_bitcode_codec() {
#[derive(Clone, Debug, PartialEq, bitcode::Encode, bitcode::Decode)]
struct Test {
s: String,
i: i32,
}
let t = Test {
s: String::from("party time 🎉"),
i: 42,
};
let enc = BitcodeCodec::encode(&t).unwrap();
let dec: Test = BitcodeCodec::decode(&enc).unwrap();
assert_eq!(dec, t);
}
#[test]
fn test_bitcode_codec_complex() {
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Debug, PartialEq, bitcode::Encode, bitcode::Decode)]
struct Test {
s: String,
i: i32,
m: HashMap<String, i32>,
b: BTreeMap<String, i32>,
a: Arc<String>,
}
let t = Test {
s: String::from("party time 🎉"),
i: 42,
m: HashMap::from([("a".to_string(), 1), ("b".to_string(), 2)]),
b: BTreeMap::from([("a".to_string(), 1), ("b".to_string(), 2)]),
a: Arc::new(String::from("party time 🎉"))
};
let enc = BitcodeCodec::encode(&t).unwrap();
let dec: Test = BitcodeCodec::decode(&enc).unwrap();
assert_eq!(dec, t);
}
#[test]
fn test_bitcode_codec_error() {
#[derive(bitcode::Encode, bitcode::Decode)]
struct Test;
let enc = [0];
let dec: Result<Test, bitcode::Error> = BitcodeCodec::decode(&enc);
assert!(dec.is_err());
}
}