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
use encoding::{de, from_slice, ser, serde_bytes, to_vec, Cbor, Error as EncodingError};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
pub type MethodNum = u64;
pub const METHOD_SEND: MethodNum = 0;
pub const METHOD_CONSTRUCTOR: MethodNum = 1;
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize, Hash, Eq, Default)]
#[serde(transparent)]
pub struct Serialized {
#[serde(with = "serde_bytes")]
bytes: Vec<u8>,
}
impl Cbor for Serialized {}
impl Deref for Serialized {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.bytes
}
}
impl Serialized {
pub fn new(bytes: Vec<u8>) -> Self {
Self { bytes }
}
pub fn serialize<O: ser::Serialize>(obj: O) -> Result<Self, EncodingError> {
Ok(Self {
bytes: to_vec(&obj)?,
})
}
pub fn bytes(&self) -> &[u8] {
&self.bytes
}
pub fn deserialize<O: de::DeserializeOwned>(&self) -> Result<O, EncodingError> {
Ok(from_slice(&self.bytes)?)
}
}