ToDump as trait, fine tuning de/ser
This commit is contained in:
parent
352fe36b14
commit
4cd7ea75ca
16
src/de.rs
16
src/de.rs
@ -11,7 +11,7 @@ pub struct Deserializer<T: BipackSource> {
|
|||||||
input: T,
|
input: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_bytes<'de, T: Deserialize<'de>>(source: &[u8]) -> Result<T> {
|
pub fn from_bytes<'b, T: Deserialize<'b>>(source: &[u8]) -> Result<T> {
|
||||||
let mut des = Deserializer { input: SliceSource::from(&source) };
|
let mut des = Deserializer { input: SliceSource::from(&source) };
|
||||||
T::deserialize(&mut des)
|
T::deserialize(&mut des)
|
||||||
}
|
}
|
||||||
@ -346,15 +346,12 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn testeq<'a, T: Serialize + Deserialize<'a> + PartialEq + Debug>(x: & 'a T) {
|
fn testeq<T: Serialize + Deserialize<'static> + PartialEq + Debug>(x: &T) {
|
||||||
|
{
|
||||||
let packed = to_bytes(x).unwrap();
|
let packed = to_bytes(x).unwrap();
|
||||||
println!("packed {:?}:\n{}", x, to_dump(&packed));
|
println!("packed {:?}:\n{}", x, to_dump(&packed));
|
||||||
|
let z: T = from_bytes(&packed).unwrap();
|
||||||
assert_eq!(*x, from_bytes(&packed).unwrap());
|
assert_eq!(*x, from_bytes(&packed).unwrap());
|
||||||
|
|
||||||
let mut buffer = [0u8; 128];
|
|
||||||
let packed2 = to_buffer(x, & mut buffer).unwrap();
|
|
||||||
assert_eq!(*x, from_bytes(&packed2).unwrap());
|
|
||||||
|
|
||||||
if packed.len() > 1 {
|
if packed.len() > 1 {
|
||||||
let mut small_buffer = [0u8; 1];
|
let mut small_buffer = [0u8; 1];
|
||||||
let result = to_buffer(x, &mut small_buffer);
|
let result = to_buffer(x, &mut small_buffer);
|
||||||
@ -363,6 +360,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut buffer = [0u8; 128];
|
||||||
|
let packed2 = to_buffer(x, &mut buffer).unwrap();
|
||||||
|
assert_eq!(*x, from_bytes(&packed2).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_enum() -> Result<()> {
|
fn test_enum() -> Result<()> {
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
|
@ -126,6 +126,8 @@ pub mod contrail;
|
|||||||
pub mod fixint;
|
pub mod fixint;
|
||||||
pub mod buffer_sink;
|
pub mod buffer_sink;
|
||||||
|
|
||||||
|
pub use serde::{Deserialize,Serialize};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
|
19
src/tools.rs
19
src/tools.rs
@ -93,3 +93,22 @@ pub fn to_hex<T: AsRef<[u8]>>(source: T) -> Result<String,FromUtf8Error> {
|
|||||||
}
|
}
|
||||||
result.string()
|
result.string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait ToDump {
|
||||||
|
/// Same as [to_dump] function, but specified as a trait for
|
||||||
|
/// different dump-able formats.
|
||||||
|
fn to_dump(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToDump for &[u8] {
|
||||||
|
fn to_dump(&self) -> String{
|
||||||
|
to_dump(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToDump for Vec<u8> {
|
||||||
|
fn to_dump(&self) -> String {
|
||||||
|
to_dump(&self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user