feat: default bincode 2.0.0-rc.*

This commit is contained in:
Vincent Herlemont
2023-10-29 09:42:30 +01:00
parent 383379fe1e
commit 39466299ed
22 changed files with 68 additions and 11 deletions
@@ -0,0 +1,34 @@
use bincode;
use bincode::{config, Decode, Encode};
pub struct Bincode;
impl<T: bincode::Encode> native_model::Encode<T> for Bincode {
type Error = bincode::error::EncodeError;
fn encode(obj: &T) -> Result<Vec<u8>, bincode::error::EncodeError> {
bincode::encode_to_vec(obj, config::standard())
}
}
impl<T: bincode::Decode> native_model::Decode<T> for Bincode {
type Error = bincode::error::DecodeError;
fn decode(data: Vec<u8>) -> Result<T, bincode::error::DecodeError> {
bincode::decode_from_slice(&data, config::standard()).map(|(result, _)| result)
}
}
use native_model::native_model;
#[derive(Encode, Decode, PartialEq, Debug)]
#[native_model(id = 1, version = 1, with = Bincode)]
struct DotV1(u32, u32);
#[test]
fn test_bincode_encode_decode() {
// Application 1
let dot = DotV1(1, 2);
let bytes = native_model::encode(&dot).unwrap();
// Application 1
let (dot, _) = native_model::decode::<DotV1>(bytes).unwrap();
assert_eq!(dot, DotV1(1, 2));
}
@@ -0,0 +1,50 @@
use bincode;
use serde::{Deserialize, Serialize};
// fn native_model_encode_body<T: Serialize>(
// model: &T,
// ) -> Result<Vec<u8>, bincode::error::EncodeError> {
// {
// bincode::serde::encode_to_vec(model, bincode::config::standard())
// }
// }
//
// fn native_model_decode_body<T: for<'a> Deserialize<'a>>(
// data: Vec<u8>,
// ) -> Result<T, bincode::error::DecodeError> {
// {
// Ok(bincode::serde::decode_from_slice(&data, bincode::config::standard())?.0)
// }
// }
pub struct Bincode;
impl<T: Serialize> native_model::Encode<T> for Bincode {
type Error = bincode::error::EncodeError;
fn encode(obj: &T) -> Result<Vec<u8>, bincode::error::EncodeError> {
bincode::serde::encode_to_vec(obj, bincode::config::standard())
}
}
impl<T: for<'a> Deserialize<'a>> native_model::Decode<T> for Bincode {
type Error = bincode::error::DecodeError;
fn decode(data: Vec<u8>) -> Result<T, bincode::error::DecodeError> {
Ok(bincode::serde::decode_from_slice(&data, bincode::config::standard())?.0)
}
}
use native_model::native_model;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[native_model(id = 1, version = 1, with = Bincode)]
struct DotV1(u32, u32);
#[test]
fn test_bincode_serde_serialize_deserialize() {
// Application 1
let dot = DotV1(1, 2);
let bytes = native_model::encode(&dot).unwrap();
// Application 1
let (dot, _) = native_model::decode::<DotV1>(bytes).unwrap();
assert_eq!(dot, DotV1(1, 2));
}
@@ -0,0 +1,2 @@
mod bincode;
mod bincode_serde;
@@ -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::bincode_1_3::Bincode)]
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 });
}
@@ -0,0 +1,18 @@
use native_model::{native_model, bincode_2_rc};
use serde::Deserialize;
use serde::Serialize;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[native_model(id = 1, version = 1, with = native_model::bincode_2_rc::Bincode)]
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 });
}
@@ -0,0 +1,2 @@
mod bincode_1_3;
mod bincode_2_rc;
@@ -0,0 +1,110 @@
use bincode::{config, Decode, Encode};
use native_model::native_model;
pub struct Bincode;
impl<T: bincode::Encode> native_model::Encode<T> for Bincode {
type Error = bincode::error::EncodeError;
fn encode(obj: &T) -> Result<Vec<u8>, bincode::error::EncodeError> {
bincode::encode_to_vec(obj, config::standard())
}
}
impl<T: bincode::Decode> native_model::Decode<T> for Bincode {
type Error = bincode::error::DecodeError;
fn decode(data: Vec<u8>) -> Result<T, bincode::error::DecodeError> {
bincode::decode_from_slice(&data, config::standard()).map(|(result, _)| result)
}
}
#[derive(Encode, Decode, PartialEq, Debug)]
#[native_model(id = 1, version = 1, with = Bincode)]
struct DotV1(u32, u32);
#[derive(Encode, Decode, PartialEq, Debug)]
#[native_model(id = 1, version = 2, with = Bincode, from = DotV1)]
struct DotV2 {
name: String,
x: u64,
y: u64,
}
impl From<DotV1> for DotV2 {
fn from(dot: DotV1) -> Self {
DotV2 {
name: "".to_string(),
x: dot.0 as u64,
y: dot.1 as u64,
}
}
}
impl From<DotV2> for DotV1 {
fn from(dot: DotV2) -> Self {
DotV1(dot.x as u32, dot.y as u32)
}
}
#[derive(Encode, Decode, PartialEq, Debug)]
#[native_model(id = 1, version = 3, with = Bincode, try_from = (DotV2, anyhow::Error))]
struct DotV3 {
name: String,
cord: Cord,
}
#[derive(Encode, Decode, PartialEq, Debug)]
struct Cord {
x: u64,
y: u64,
}
impl TryFrom<DotV2> for DotV3 {
type Error = anyhow::Error;
fn try_from(dot: DotV2) -> Result<Self, Self::Error> {
Ok(DotV3 {
name: dot.name,
cord: Cord { x: dot.x, y: dot.y },
})
}
}
impl TryFrom<DotV3> for DotV2 {
type Error = anyhow::Error;
fn try_from(dot: DotV3) -> Result<Self, Self::Error> {
Ok(DotV2 {
name: dot.name,
x: dot.cord.x,
y: dot.cord.y,
})
}
}
#[test]
fn simple_test() {
let dot = DotV1(1, 2);
let bytes = native_model::encode(&dot).unwrap();
let (dot_decoded, _) = native_model::decode::<DotV1>(bytes.clone()).unwrap();
assert_eq!(dot, dot_decoded);
let (dot_decoded, _) = native_model::decode::<DotV2>(bytes.clone()).unwrap();
assert_eq!(
DotV2 {
name: "".to_string(),
x: 1,
y: 2
},
dot_decoded
);
let (dot_decoded, _) = native_model::decode::<DotV3>(bytes.clone()).unwrap();
assert_eq!(
DotV3 {
name: "".to_string(),
cord: Cord { x: 1, y: 2 }
},
dot_decoded
);
}
@@ -0,0 +1,69 @@
use bincode;
use bincode::{config, Decode, Encode};
use native_model::native_model;
pub struct Bincode;
impl<T: bincode::Encode> native_model::Encode<T> for Bincode {
type Error = bincode::error::EncodeError;
fn encode(obj: &T) -> Result<Vec<u8>, bincode::error::EncodeError> {
bincode::encode_to_vec(obj, config::standard())
}
}
impl<T: bincode::Decode> native_model::Decode<T> for Bincode {
type Error = bincode::error::DecodeError;
fn decode(data: Vec<u8>) -> Result<T, bincode::error::DecodeError> {
bincode::decode_from_slice(&data, config::standard()).map(|(result, _)| result)
}
}
#[derive(Encode, Decode, PartialEq, Debug)]
#[native_model(id = 1, version = 1, with = Bincode)]
struct DotV1(u32, u32);
#[derive(Encode, Decode, PartialEq, Debug)]
#[native_model(id = 1, version = 2, with = Bincode, from = DotV1)]
struct DotV2 {
name: String,
x: u64,
y: u64,
}
impl From<DotV1> for DotV2 {
fn from(dot: DotV1) -> Self {
DotV2 {
name: "".to_string(),
x: dot.0 as u64,
y: dot.1 as u64,
}
}
}
impl From<DotV2> for DotV1 {
fn from(dot: DotV2) -> Self {
DotV1(dot.x as u32, dot.y as u32)
}
}
#[test]
fn run_example() {
// Application 1
let dot = DotV1(1, 2);
let bytes = native_model::encode(&dot).unwrap();
// Application 1 sends bytes to Application 2.
// Application 2
let (mut dot, source_version) = native_model::decode::<DotV2>(bytes).unwrap();
// Use the struct DataV2 which has more fields and a different structure.
dot.name = "Dot".to_string();
dot.x = 5;
// Encode the dot with the application 1 version in order to be compatible with it.
let bytes = native_model::encode_downgrade(dot, source_version).unwrap();
// Application 2 sends bytes to Application 1.
// Application 1
let (dot, _) = native_model::decode::<DotV1>(bytes).unwrap();
assert_eq!(dot, DotV1(5, 2));
}
@@ -0,0 +1,3 @@
mod default_codec;
mod example_define_model;
mod example_main;