feat: default postcard 1.0
This commit is contained in:
@@ -23,6 +23,7 @@ native_model_macro = { version = "0.3.30", path = "native_model_macro" }
|
||||
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||
bincode_1_3 = { package = "bincode", version = "1.3", optional = true }
|
||||
bincode_2_rc = { package = "bincode", version = "2.0.0-rc.3", features = ["serde"], optional = true }
|
||||
postcard_1_0 = { package = "postcard", version = "1.0", features = ["alloc"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_json = "1.0"
|
||||
@@ -30,7 +31,7 @@ criterion = { version = "0.5.1" }
|
||||
skeptic = "0.13"
|
||||
|
||||
[features]
|
||||
default = ["serde", "bincode_1_3", "bincode_2_rc"]
|
||||
default = ["serde", "bincode_1_3", "bincode_2_rc", "postcard_1_0"]
|
||||
|
||||
[[bench]]
|
||||
name = "overhead"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod bincode_1_3;
|
||||
pub mod bincode_2_rc;
|
||||
pub mod postcard_1_0;
|
||||
|
||||
/// Encode trait for your own encoding method.
|
||||
///
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
use postcard_1_0::{from_bytes, to_allocvec, Error};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub struct PostCard;
|
||||
|
||||
impl<T: Serialize> super::Encode<T> for PostCard {
|
||||
type Error = Error;
|
||||
fn encode(obj: &T) -> Result<Vec<u8>, Error> {
|
||||
Ok(to_allocvec(obj)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: for<'a> Deserialize<'a>> super::Decode<T> for PostCard {
|
||||
type Error = Error;
|
||||
fn decode(data: Vec<u8>) -> Result<T, Error> {
|
||||
Ok(from_bytes(&data)?)
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,11 @@ name = "tests_crate"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
||||
[workspace]
|
||||
|
||||
[dev-dependencies]
|
||||
native_model = { path = "../" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
bincode = { version = "2.0.0-rc.3", features = ["serde"] }
|
||||
anyhow = "1.0.44"
|
||||
anyhow = "1.0"
|
||||
postcard = { version = "1.0", features = ["alloc"] }
|
||||
@@ -1,4 +1,4 @@
|
||||
use native_model::{native_model, bincode_2_rc};
|
||||
use native_model::{native_model};
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
mod bincode_1_3;
|
||||
mod bincode_2_rc;
|
||||
mod bincode_2_rc;
|
||||
mod postcard_1_0;
|
||||
@@ -0,0 +1,18 @@
|
||||
use native_model::{native_model};
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
#[native_model(id = 1, version = 1, with = native_model::postcard_1_0::PostCard)]
|
||||
struct Example {
|
||||
a: u32,
|
||||
b: u32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_decode() {
|
||||
let example = Example { a: 1, b: 2 };
|
||||
let bytes = native_model::encode(&example).unwrap();
|
||||
let (example, _) = native_model::decode::<Example>(bytes).unwrap();
|
||||
assert_eq!(example, Example { a: 1, b: 2 });
|
||||
}
|
||||
Reference in New Issue
Block a user