From c1b1cceaa14f5aa0465506920a013870d325a4a3 Mon Sep 17 00:00:00 2001 From: sergeych Date: Mon, 27 Nov 2023 23:55:48 +0300 Subject: [PATCH] ser refactored to use parametrized bipackSink (to use with any type of sink, e.g. network, nostd (rings, fixed buffers, etc.) --- src/bipack_source.rs | 27 +-------------------------- src/ser.rs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 36 deletions(-) diff --git a/src/bipack_source.rs b/src/bipack_source.rs index c22151f..22383e5 100644 --- a/src/bipack_source.rs +++ b/src/bipack_source.rs @@ -188,32 +188,7 @@ impl<'x> BipackSource for SliceSource<'x> { } } -pub struct VecSource { - data: Vec, - position: usize, -} - -impl VecSource { - pub fn from(src: Vec) -> VecSource { - VecSource { data: src, position: 0 } - } -} - -impl BipackSource for VecSource { - fn get_u8(self: &mut Self) -> Result { - 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 - } -} + diff --git a/src/ser.rs b/src/ser.rs index 45cf01f..82c58d9 100644 --- a/src/ser.rs +++ b/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 { // This string starts empty and JSON is appended as values are serialized. - output: Vec, + output: S, } pub fn to_bytes(value: &T) -> Result> @@ -16,7 +16,7 @@ pub fn to_bytes(value: &T) -> Result> Ok(serializer.output) } -impl<'a> ser::Serializer for &'a mut Serializer { +impl<'a, S: BipackSink> ser::Serializer for &'a mut Serializer { 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 { // 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 { 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 { 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 { 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 { 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 { 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 { type Ok = (); type Error = Error;