feat: add support for rmp-serde
This commit is contained in:
committed by
Vincent Herlemont
parent
736eb712f4
commit
646673efda
@@ -24,6 +24,7 @@ serde = { version = "1.0.200", features = ["derive"], optional = true }
|
||||
bincode_1_3 = { package = "bincode", version = "1.3.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.8", features = ["alloc"], optional = true }
|
||||
rmp_serde_1_3 = { package = "rmp-serde", version = "1.3", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serde_json = "1.0.116"
|
||||
@@ -38,4 +39,4 @@ name = "overhead"
|
||||
harness = false
|
||||
|
||||
[build-dependencies]
|
||||
skeptic = "0.13.7"
|
||||
skeptic = "0.13.7"
|
||||
|
||||
@@ -1,19 +1,48 @@
|
||||
use bincode_1_3::{deserialize, serialize, Error};
|
||||
use serde::{Deserialize, Serialize};
|
||||
//! ⚠️ [`Read the docs before using`](crate::bincode_1_3::Bincode#warning).
|
||||
//! Annotate your type with `native_model::bincode_1_3::Bincode` to use the
|
||||
//! bincode 1.3 crate for serializing & deserializing.
|
||||
|
||||
/// Used to specify the [bincode](https://crates.io/crates/bincode/1.3.3) `1.3`
|
||||
/// crate for serialization & deserialization.
|
||||
///
|
||||
/// # Warning
|
||||
///
|
||||
/// `bincode` [does not implement](https://github.com/bincode-org/bincode/issues/548)
|
||||
/// all [serde](https://crates.io/crates/serde) features. Errors may be
|
||||
/// encountered when using this with some types.
|
||||
///
|
||||
/// # Basic usage
|
||||
///
|
||||
/// Use the [`with`](crate::native_model) attribute on your type to instruct
|
||||
/// `native_model` to use `Bincode` for serialization & deserialization.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```rust
|
||||
/// #[native_model(id = 1, version = 1, with = native_model::bincode_1_3::Bincode)]
|
||||
/// struct MyStruct {
|
||||
/// my_string: String
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
#[doc(cfg(all(feature = "serde", feature = "bincode_1_3")))]
|
||||
#[derive(Default)]
|
||||
pub struct Bincode;
|
||||
|
||||
impl<T: Serialize> super::Encode<T> for Bincode {
|
||||
type Error = Error;
|
||||
fn encode(obj: &T) -> Result<Vec<u8>, Error> {
|
||||
Ok(serialize(obj)?)
|
||||
#[cfg(all(feature = "serde", feature = "bincode_1_3"))]
|
||||
impl<T: serde::Serialize> super::Encode<T> for Bincode {
|
||||
type Error = bincode_1_3::Error;
|
||||
/// Serializes a type into bytes using the `bincode` `1.3` crate.
|
||||
fn encode(obj: &T) -> Result<Vec<u8>, Self::Error> {
|
||||
bincode_1_3::serialize(obj)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: for<'a> Deserialize<'a>> super::Decode<T> for Bincode {
|
||||
type Error = Error;
|
||||
fn decode(data: Vec<u8>) -> Result<T, Error> {
|
||||
Ok(deserialize(&data[..])?)
|
||||
#[cfg(all(feature = "serde", feature = "bincode_1_3"))]
|
||||
impl<T: for<'de> serde::Deserialize<'de>> super::Decode<T> for Bincode {
|
||||
type Error = bincode_1_3::Error;
|
||||
/// Deserializes a type from bytes using the `bincode` `1.3` crate.
|
||||
fn decode(data: Vec<u8>) -> Result<T, Self::Error> {
|
||||
bincode_1_3::deserialize(&data[..])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,53 @@
|
||||
use bincode_2_rc::{
|
||||
config,
|
||||
error::{DecodeError, EncodeError},
|
||||
serde::{decode_from_slice, encode_to_vec},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
//! ⚠️ [`Read the docs before using`](crate::bincode_2_rc::Bincode#warning).
|
||||
//! Annotate your type with `native_model::bincode_2_rc::Bincode` to use
|
||||
//! the bincode 2.0.0-rc.3 crate for serializing & deserializing.
|
||||
|
||||
/// Used to specify the [bincode](https://crates.io/crates/bincode/2.0.0-rc.3)
|
||||
/// `2.0.0-rc.3` crate for serialization & deserialization.
|
||||
///
|
||||
/// # Warning
|
||||
///
|
||||
/// `bincode` [does not implement](https://docs.rs/bincode/2.0.0-rc.3/bincode/serde/index.html#known-issues)
|
||||
/// all [serde](https://crates.io/crates/serde) features. Errors may be
|
||||
/// encountered when using this with some types.
|
||||
///
|
||||
/// # Basic usage
|
||||
///
|
||||
/// Use the [`with`](crate::native_model) attribute on your type to instruct
|
||||
/// `native_model` to use `Bincode` for serialization & deserialization.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```rust
|
||||
/// #[native_model(id = 1, version = 1, with = native_model::bincode_2_rc::Bincode)]
|
||||
/// struct MyStruct {
|
||||
/// my_string: String
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
#[doc(cfg(all(feature = "serde", feature = "bincode_2_rc")))]
|
||||
pub struct Bincode;
|
||||
|
||||
impl<T: Serialize> super::Encode<T> for Bincode {
|
||||
type Error = EncodeError;
|
||||
fn encode(obj: &T) -> Result<Vec<u8>, EncodeError> {
|
||||
encode_to_vec(obj, config::standard())
|
||||
#[cfg(all(feature = "serde", feature = "bincode_2_rc"))]
|
||||
impl<T: serde::Serialize> super::Encode<T> for Bincode {
|
||||
type Error = bincode_2_rc::error::EncodeError;
|
||||
/// Serializes a type into bytes using the `bincode` `2.0.0-rc.3` crate.
|
||||
fn encode(obj: &T) -> Result<Vec<u8>, Self::Error> {
|
||||
bincode_2_rc::serde::encode_to_vec(
|
||||
obj,
|
||||
bincode_2_rc::config::standard()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: for<'a> Deserialize<'a>> super::Decode<T> for Bincode {
|
||||
type Error = DecodeError;
|
||||
fn decode(data: Vec<u8>) -> Result<T, DecodeError> {
|
||||
Ok(decode_from_slice(&data, config::standard())?.0)
|
||||
#[cfg(all(feature = "serde", feature = "bincode_2_rc"))]
|
||||
impl<T: for<'de> serde::Deserialize<'de>> super::Decode<T> for Bincode {
|
||||
type Error = bincode_2_rc::error::DecodeError;
|
||||
/// Deserializes a type from bytes using the `bincode` `2.0.0-rc.3` crate.
|
||||
fn decode(data: Vec<u8>) -> Result<T, Self::Error> {
|
||||
Ok(bincode_2_rc::serde::decode_from_slice(
|
||||
&data,
|
||||
bincode_2_rc::config::standard()
|
||||
)?.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#[cfg(all(feature = "serde", feature = "bincode_1_3"))]
|
||||
//! Traits and implementations for encoding types into a series of bytes and
|
||||
//! decoding bytes back into types.
|
||||
|
||||
#[cfg(any(all(feature = "serde", feature = "bincode_1_3"), doc))]
|
||||
pub mod bincode_1_3;
|
||||
#[cfg(all(feature = "serde", feature = "bincode_2_rc"))]
|
||||
#[cfg(any(all(feature = "serde", feature = "bincode_2_rc"), doc))]
|
||||
pub mod bincode_2_rc;
|
||||
#[cfg(all(feature = "serde", feature = "postcard_1_0"))]
|
||||
#[cfg(any(all(feature = "serde", feature = "postcard_1_0"), doc))]
|
||||
pub mod postcard_1_0;
|
||||
#[cfg(any(all(feature = "serde", feature = "rmp_serde_1_3"), doc))]
|
||||
pub mod rmp_serde_1_3;
|
||||
|
||||
/// Encode trait for your own encoding method.
|
||||
///
|
||||
|
||||
@@ -1,18 +1,46 @@
|
||||
use postcard_1_0::{from_bytes, to_allocvec, Error};
|
||||
use serde::{Deserialize, Serialize};
|
||||
//! ⚠️ [`Read the docs before using`](crate::postcard_1_0::PostCard#warning).
|
||||
//! Annotate your type with `native_model::postcard_1_0::PostCard` to
|
||||
//! use the postcard 1.0 crate for serializing & deserializing.
|
||||
|
||||
/// Used to specify the [postcard](https://crates.io/crates/postcard/1.0.8)
|
||||
/// `1.0` crate for serialization & deserialization.
|
||||
///
|
||||
/// # Warning
|
||||
///
|
||||
/// `postcard` does not implement all [serde](https://crates.io/crates/serde)
|
||||
/// features. Errors may be encountered when using this with some types.
|
||||
///
|
||||
/// # Basic usage
|
||||
///
|
||||
/// Use the [`with`](crate::native_model) attribute on your type to instruct
|
||||
/// `native_model` to use `PostCard` for serialization & deserialization.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```rust
|
||||
/// #[native_model(id = 1, version = 1, with = native_model::postcard_1_0::PostCard)]
|
||||
/// struct MyStruct {
|
||||
/// my_string: String
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
#[doc(cfg(all(feature = "serde", feature = "postcard_1_0")))]
|
||||
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)?)
|
||||
#[cfg(all(feature = "serde", feature = "postcard_1_0"))]
|
||||
impl<T: serde::Serialize> super::Encode<T> for PostCard {
|
||||
type Error = postcard_1_0::Error;
|
||||
/// Serializes a type into bytes using the `postcard` `1.0` crate.
|
||||
fn encode(obj: &T) -> Result<Vec<u8>, Self::Error> {
|
||||
postcard_1_0::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)?)
|
||||
#[cfg(all(feature = "serde", feature = "postcard_1_0"))]
|
||||
impl<T: for<'de> serde::Deserialize<'de>> super::Decode<T> for PostCard {
|
||||
type Error = postcard_1_0::Error;
|
||||
/// Deserializes a type from bytes using the `postcard` `1.0` crate.
|
||||
fn decode(data: Vec<u8>) -> Result<T, Self::Error> {
|
||||
postcard_1_0::from_bytes(&data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//! [`Annotate your type`](crate::native_model) with
|
||||
//! `native_model::rmp_serde_1_3::RmpSerde` to use the rmp-serde 1.3 crate for
|
||||
//! serializing & deserializing.
|
||||
|
||||
/// Used to specify the [rmp-serde](https://crates.io/crates/rmp-serde/1.3.0)
|
||||
/// `1.3` crate for serialization & deserialization.
|
||||
///
|
||||
/// # Basic usage
|
||||
///
|
||||
/// Use the [`with`](crate::native_model) attribute on your type to instruct
|
||||
/// `native_model` to use `RmpSerde` for serialization & deserialization.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```rust
|
||||
/// #[native_model(id = 1, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
||||
/// struct MyStruct {
|
||||
/// my_string: String
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
#[doc(cfg(all(feature = "serde", feature = "rmp_serde_1_3")))]
|
||||
pub struct RmpSerde;
|
||||
|
||||
#[cfg(all(feature = "serde", feature = "rmp_serde_1_3"))]
|
||||
impl<T: serde::Serialize> crate::Encode<T> for RmpSerde {
|
||||
type Error = rmp_serde_1_3::encode::Error;
|
||||
/// Serializes a type into bytes using the `rmp-serde` `1.3` crate.
|
||||
fn encode(obj: &T) -> Result<Vec<u8>, Self::Error> {
|
||||
rmp_serde_1_3::encode::to_vec(obj)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "serde", feature = "rmp_serde_1_3"))]
|
||||
impl<T: for<'de> serde::Deserialize<'de>> crate::Decode<T> for RmpSerde {
|
||||
type Error = rmp_serde_1_3::decode::Error;
|
||||
/// Deserializes a type from bytes using the `rmp-serde` `1.3` crate.
|
||||
fn decode(data: Vec<u8>) -> Result<T, Self::Error> {
|
||||
rmp_serde_1_3::decode::from_slice(&data)
|
||||
}
|
||||
}
|
||||
@@ -14,12 +14,19 @@
|
||||
//! - The crate is in early development, and performance is expected to improve over time.
|
||||
//!
|
||||
//! See examples in the [README.md](https://github.com/vincent-herlemont/native_model) file.
|
||||
//!
|
||||
//! You may also want to check [David Koloski](https://github.com/djkoloski)'s
|
||||
//! [Rust serialization benchmarks](https://github.com/djkoloski/rust_serialization_benchmark)
|
||||
//! for help selecting the codec right for your project.
|
||||
|
||||
#![feature(doc_cfg)]
|
||||
|
||||
#[cfg(any(
|
||||
feature = "serde",
|
||||
feature = "bincode_1_3",
|
||||
feature = "bincode_2_rc",
|
||||
feature = "postcard_1_0"
|
||||
feature = "postcard_1_0",
|
||||
doc
|
||||
))]
|
||||
mod codec;
|
||||
|
||||
@@ -27,7 +34,8 @@ mod codec;
|
||||
feature = "serde",
|
||||
feature = "bincode_1_3",
|
||||
feature = "bincode_2_rc",
|
||||
feature = "postcard_1_0"
|
||||
feature = "postcard_1_0",
|
||||
doc
|
||||
))]
|
||||
pub use codec::*;
|
||||
mod header;
|
||||
@@ -187,7 +195,6 @@ pub trait Model: Sized {
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let version = version.clone();
|
||||
let mut data = self.native_model_encode_downgrade_body(version)?;
|
||||
let data = crate::native_model_encode(&mut data, Self::native_model_id(), version);
|
||||
Ok(data)
|
||||
|
||||
Reference in New Issue
Block a user