ser refactored to use parametrized bipackSink (to use with any type of sink, e.g. network, nostd (rings, fixed buffers, etc.)
This commit is contained in:
parent
8bba1ef943
commit
c1b1cceaa1
@ -188,32 +188,7 @@ impl<'x> BipackSource for SliceSource<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VecSource {
|
||||
data: Vec<u8>,
|
||||
position: usize,
|
||||
}
|
||||
|
||||
impl VecSource {
|
||||
pub fn from(src: Vec<u8>) -> VecSource {
|
||||
VecSource { data: src, position: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl BipackSource for VecSource {
|
||||
fn get_u8(self: &mut Self) -> Result<u8> {
|
||||
if self.position >= self.data.len() {
|
||||
Err(NoDataError)
|
||||
} else {
|
||||
let result = self.data[self.position];
|
||||
self.position += 1;
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn eof(self: &Self) -> bool {
|
||||
self.data.len() <= self.position
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
20
src/ser.rs
20
src/ser.rs
@ -3,9 +3,9 @@ use serde::{ser, Serialize};
|
||||
use crate::bipack_sink::{BipackSink, IntoU64};
|
||||
use crate::error::{Error, Result};
|
||||
|
||||
pub struct Serializer {
|
||||
pub struct Serializer<S: BipackSink> {
|
||||
// This string starts empty and JSON is appended as values are serialized.
|
||||
output: Vec<u8>,
|
||||
output: S,
|
||||
}
|
||||
|
||||
pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
|
||||
@ -16,7 +16,7 @@ pub fn to_bytes<T>(value: &T) -> Result<Vec<u8>>
|
||||
Ok(serializer.output)
|
||||
}
|
||||
|
||||
impl<'a> ser::Serializer for &'a mut Serializer {
|
||||
impl<'a, S: BipackSink> ser::Serializer for &'a mut Serializer<S> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
type SerializeSeq = Self;
|
||||
@ -202,7 +202,7 @@ impl<'a> ser::Serializer for &'a mut Serializer {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeSeq for &'a mut Serializer {
|
||||
impl<'a, S: BipackSink> ser::SerializeSeq for &'a mut Serializer<S> {
|
||||
// Must match the `Ok` type of the serializer.
|
||||
type Ok = ();
|
||||
// Must match the `Error` type of the serializer.
|
||||
@ -222,7 +222,7 @@ impl<'a> ser::SerializeSeq for &'a mut Serializer {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeTuple for &'a mut Serializer {
|
||||
impl<'a, S: BipackSink> ser::SerializeTuple for &'a mut Serializer<S> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
@ -238,7 +238,7 @@ impl<'a> ser::SerializeTuple for &'a mut Serializer {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeTupleStruct for &'a mut Serializer {
|
||||
impl<'a, S: BipackSink> ser::SerializeTupleStruct for &'a mut Serializer<S> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
@ -254,7 +254,7 @@ impl<'a> ser::SerializeTupleStruct for &'a mut Serializer {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeTupleVariant for &'a mut Serializer {
|
||||
impl<'a, S: BipackSink> ser::SerializeTupleVariant for &'a mut Serializer<S> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
@ -270,7 +270,7 @@ impl<'a> ser::SerializeTupleVariant for &'a mut Serializer {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeMap for &'a mut Serializer {
|
||||
impl<'a, S: BipackSink> ser::SerializeMap for &'a mut Serializer<S> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
@ -306,7 +306,7 @@ impl<'a> ser::SerializeMap for &'a mut Serializer {
|
||||
|
||||
// Structs are like maps in which the keys are constrained to be compile-time
|
||||
// constant strings.
|
||||
impl<'a> ser::SerializeStruct for &'a mut Serializer {
|
||||
impl<'a, S: BipackSink> ser::SerializeStruct for &'a mut Serializer<S> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
@ -322,7 +322,7 @@ impl<'a> ser::SerializeStruct for &'a mut Serializer {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ser::SerializeStructVariant for &'a mut Serializer {
|
||||
impl<'a, S: BipackSink> ser::SerializeStructVariant for &'a mut Serializer<S> {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user