#![cfg(feature = "serde")]
use std::alloc::System;
use serde::{Deserialize, Serialize};
use stats_alloc::{StatsAlloc, INSTRUMENTED_SYSTEM};
use abin::{NewStr, Str, StrFactory};
use utils::*;
#[global_allocator]
static GLOBAL: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;
pub mod utils;
#[test]
fn serialize_deserialize() {
deserialize_serialize_small();
deserialize_serialize_large();
}
fn deserialize_serialize_small() {
let short = "short";
let entity = mem_scoped(
&GLOBAL,
&MaAnd(&[
&MaExactNumberOfAllocations(0),
&MaExactNumberOfReAllocations(0),
&MaExactNumberOfDeAllocations(0),
]),
|| {
Entity {
id: 45,
string_a: NewStr::from_static(short),
string_b: NewStr::from_static(""),
}
},
);
let as_vec = serde_cbor::to_vec(&entity).unwrap();
mem_scoped(
&GLOBAL,
&MaAnd(&[
&MaExactNumberOfAllocations(0),
&MaExactNumberOfReAllocations(0),
&MaExactNumberOfDeAllocations(0),
]),
|| {
let restored: Entity = serde_cbor::from_slice(as_vec.as_slice()).unwrap();
assert_eq!(entity, restored);
},
);
}
fn deserialize_serialize_large() {
let entity = mem_scoped(
&GLOBAL,
&MaAnd(&[
&MaExactNumberOfAllocations(0),
&MaExactNumberOfReAllocations(0),
&MaExactNumberOfDeAllocations(0),
]),
|| Entity {
id: 45,
string_a: NewStr::from_static(
"This is somewhat longer; this will not fit \
on stack - longer - even longer.",
),
string_b: NewStr::from_static(
"Longer and longer and longer and longer and \
even longer... again, even longer. Longer and longer.",
),
},
);
let as_vec = serde_cbor::to_vec(&entity).unwrap();
mem_scoped(
&GLOBAL,
&MaAnd(&[
&MaExactNumberOfAllocations(2),
&MaExactNumberOfReAllocations(0),
&MaExactNumberOfDeAllocations(2),
]),
|| {
let restored: Entity = serde_cbor::from_slice(as_vec.as_slice()).unwrap();
assert_eq!(entity, restored);
},
);
}
#[derive(Deserialize, Serialize, Eq, PartialEq, Clone, Debug)]
pub struct Entity {
pub id: u64,
pub string_a: Str,
pub string_b: Str,
}