mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-27 18:24:43 +10:00
feat: add with macro option
This commit is contained in:
@@ -1,30 +1,30 @@
|
||||
```rust,skt-main
|
||||
use bincode;
|
||||
use bincode::{{Decode, Encode}};
|
||||
use bincode::{{Decode, Encode, config}};
|
||||
use native_model_macro::native_model;
|
||||
|
||||
fn native_model_encode_body<T: bincode::Encode>(
|
||||
model: &T,
|
||||
) -> Result<Vec<u8>, bincode::error::EncodeError> {{
|
||||
{{
|
||||
bincode::encode_to_vec(model, bincode::config::standard())
|
||||
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())
|
||||
}}
|
||||
}}
|
||||
|
||||
fn native_model_decode_body<T: bincode::Decode>(
|
||||
data: Vec<u8>,
|
||||
) -> Result<T, bincode::error::DecodeError> {{
|
||||
{{
|
||||
bincode::decode_from_slice(&data, bincode::config::standard()).map(|(result, _)| result)
|
||||
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)]
|
||||
#[native_model(id = 1, version = 1, with = Bincode)]
|
||||
struct DotV1(u32, u32);
|
||||
|
||||
#[derive(Encode, Decode, PartialEq, Debug)]
|
||||
#[native_model(id = 1, version = 2, from = DotV1)]
|
||||
#[native_model(id = 1, version = 2, with = Bincode, from = DotV1)]
|
||||
struct DotV2 {{
|
||||
name: String,
|
||||
x: u64,
|
||||
@@ -56,17 +56,22 @@ fn main() {{
|
||||
```rust,skt-define-models
|
||||
use bincode::{{config, Decode, Encode}};
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn native_model_encode_body<T: Encode>(obj: &T) -> Result<Vec<u8>, bincode::error::EncodeError> {{
|
||||
bincode::encode_to_vec(obj, config::standard())
|
||||
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())
|
||||
}}
|
||||
}}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn native_model_decode_body<T: Decode>(data: Vec<u8>) -> Result<T, bincode::error::DecodeError> {{
|
||||
bincode::decode_from_slice(&data, config::standard()).map(|(result, _)| result)
|
||||
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)
|
||||
}}
|
||||
}}
|
||||
|
||||
|
||||
{}
|
||||
|
||||
impl From<DotV1> for DotV2 {{
|
||||
@@ -137,4 +142,11 @@ fn main() {{
|
||||
);
|
||||
}}
|
||||
|
||||
```
|
||||
|
||||
```rust,skt-define-serilization-format
|
||||
{}
|
||||
|
||||
fn main() {{
|
||||
}}
|
||||
```
|
||||
Reference in New Issue
Block a user