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
use super::errors::Error;
use crate::{de::DeserializeOwned, from_slice, ser::Serialize, to_vec};
use cid::{Cid, Code::Blake2b256};
pub trait Cbor: Serialize + DeserializeOwned {
fn marshal_cbor(&self) -> Result<Vec<u8>, Error> {
Ok(to_vec(&self)?)
}
fn unmarshal_cbor(bz: &[u8]) -> Result<Self, Error> {
Ok(from_slice(bz)?)
}
fn cid(&self) -> Result<Cid, Error> {
Ok(cid::new_from_cbor(&self.marshal_cbor()?, Blake2b256))
}
}
impl<T> Cbor for Vec<T> where T: Cbor {}
impl<T> Cbor for Option<T> where T: Cbor {}