forked from vincent-herlemont/native_model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbincode_2.rs
More file actions
54 lines (50 loc) · 2 KB
/
bincode_2.rs
File metadata and controls
54 lines (50 loc) · 2 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
//! [bincode 2.0](https://crates.io/crates/bincode/2.0.1) ·
//! Enable the `bincode_2` feature and annotate your type with
//! `native_model::bincode_2::Bincode` to have `native_db` use this crate for
//! serializing & deserializing.
/// Used to specify the
/// [bincode 2.0](https://crates.io/crates/bincode/2.0.1)
/// crate for serialization & deserialization.
///
/// # Warning
///
/// `bincode` [does not implement](https://docs.rs/bincode/2.0.1/bincode/serde/index.html#known-issues)
/// all [serde](https://crates.io/crates/serde) features. Errors may be
/// encountered when using this with some types.
///
/// If you are encountering errors when using this codec on your types, try
/// using the `rmp_serde_1_3` codec instead.
///
/// # Basic usage
///
/// After enabling the `bincode_2` feature in your `Cargo.toml`, use the
/// [`with`](crate::native_model) attribute on your type to instruct
/// `native_model` to use `Bincode` for serialization & deserialization.
///
/// Example usage:
///
/// ```rust
/// # use native_model::*;
/// #[derive(Clone, Default, serde::Deserialize, serde::Serialize)]
/// #[native_model(id = 1, version = 1, with = native_model::bincode_2::Bincode)]
/// struct MyStruct {
/// my_string: String
/// }
/// ```
pub struct Bincode;
#[cfg(all(feature = "serde", feature = "bincode_2"))]
impl<T: serde::Serialize> super::Encode<T> for Bincode {
type Error = bincode_2::error::EncodeError;
/// Serializes a type into bytes using the `bincode` `2.0` crate.
fn encode(obj: &T) -> Result<Vec<u8>, Self::Error> {
bincode_2::serde::encode_to_vec(obj, bincode_2::config::standard())
}
}
#[cfg(all(feature = "serde", feature = "bincode_2"))]
impl<T: for<'de> serde::Deserialize<'de>> super::Decode<T> for Bincode {
type Error = bincode_2::error::DecodeError;
/// Deserializes a type from bytes using the `bincode` `2.0` crate.
fn decode(data: Vec<u8>) -> Result<T, Self::Error> {
Ok(bincode_2::serde::decode_from_slice(&data, bincode_2::config::standard())?.0)
}
}