feat: add with macro option

This commit is contained in:
Vincent Herlemont
2023-10-29 09:34:45 +01:00
parent dc725a3380
commit fd5bbfd964
17 changed files with 240 additions and 120 deletions
@@ -0,0 +1,34 @@
/// Encode trait for your own encoding method.
///
/// Example:
/// ```rust
/// 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, bincode::config::standard())
/// }
/// }
/// ```
pub trait Encode<T> {
type Error;
fn encode(obj: &T) -> Result<Vec<u8>, Self::Error>;
}
/// Decode trait for your own decoding method.
///
/// Example:
/// ```rust
/// pub struct Bincode;
///
/// 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, bincode::config::standard()).map(|(result, _)| result)
/// }
/// }
pub trait Decode<T> {
type Error;
fn decode(data: Vec<u8>) -> Result<T, Self::Error>;
}
+2
View File
@@ -15,10 +15,12 @@
//!
//! See examples in the [README.md](https://github.com/vincent-herlemont/native_model) file.
mod encode_decode;
mod header;
mod model;
pub mod wrapper;
pub use encode_decode::*;
pub use model::*;
/// Macro to generate a [`native_model`] implementation for a struct.
-1
View File
@@ -5,7 +5,6 @@ pub trait Model: Sized {
fn native_model_version() -> u32;
// --------------- Decode ---------------
fn native_model_decode_body(data: Vec<u8>, id: u32) -> DecodeResult<Self>
where
Self: Sized;