diff --git a/Cargo.toml b/Cargo.toml index eab34d5..43c2b4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bipack_ru" -version = "0.3.2" +version = "0.3.3" edition = "2021" license = "Apache-2.0" description = "binary size-effective format used in Divan smart contracts, wasm bindings, network protocols, etc." diff --git a/src/bipack_source.rs b/src/bipack_source.rs index 08554d2..c22151f 100644 --- a/src/bipack_source.rs +++ b/src/bipack_source.rs @@ -167,7 +167,7 @@ pub struct SliceSource<'a> { } impl<'a> SliceSource<'a> { - pub fn from(src: &'a [u8]) -> SliceSource { + pub fn from(src: &'a [u8]) -> SliceSource<'a> { SliceSource { data: src, position: 0 } } } diff --git a/src/de.rs b/src/de.rs index ace9875..631cfce 100644 --- a/src/de.rs +++ b/src/de.rs @@ -2,23 +2,21 @@ use serde::de::{self, DeserializeSeed, IntoDeserializer, MapAccess, SeqAccess, V use serde::de::value::U32Deserializer; use serde::Deserialize; -use crate::bipack_source::{BipackSource, VecSource}; +use crate::bipack_source::{BipackSource, SliceSource}; use crate::error::{Error, Result}; -pub struct Deserializer { +pub struct Deserializer { // This string starts with the input data and characters are truncated off // the beginning as data is parsed. - input: VecSource, + input: T, } pub fn from_bytes<'de, T: Deserialize<'de>>(source: &[u8]) -> Result { - let mut des = Deserializer { input: VecSource::from(source.to_vec()) }; + let mut des = Deserializer { input: SliceSource::from(&source) }; T::deserialize(&mut des) } -impl Deserializer {} - -impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer { +impl<'de, 'a, T: BipackSource> de::Deserializer<'de> for &'a mut Deserializer { type Error = Error; fn deserialize_any(self, visitor: V) -> std::result::Result where V: Visitor<'de> { @@ -163,7 +161,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer { } } -impl<'de, 'a> serde::de::EnumAccess<'de> for &'a mut Deserializer { +impl<'de, 'a, T: BipackSource> serde::de::EnumAccess<'de> for &'a mut Deserializer { type Error = Error; type Variant = Self; @@ -177,7 +175,7 @@ impl<'de, 'a> serde::de::EnumAccess<'de> for &'a mut Deserializer { } } -impl<'de, 'a> serde::de::VariantAccess<'de> for &'a mut Deserializer { +impl<'de, 'a, T: BipackSource> serde::de::VariantAccess<'de> for &'a mut Deserializer { type Error = Error; #[inline] @@ -206,13 +204,13 @@ impl<'de, 'a> serde::de::VariantAccess<'de> for &'a mut Deserializer { } -struct SimpleSeq<'a> { - de: &'a mut Deserializer, +struct SimpleSeq<'a, T: BipackSource> { + de: &'a mut Deserializer, size: usize, } -impl<'a> SimpleSeq<'a> { - fn new(de: &'a mut Deserializer, size: usize) -> Self { +impl<'a, T: BipackSource> SimpleSeq<'a, T> { + fn new(de: &'a mut Deserializer, size: usize) -> Self { SimpleSeq { de, size: size, @@ -222,12 +220,12 @@ impl<'a> SimpleSeq<'a> { // `SeqAccess` is provided to the `Visitor` to give it the ability to iterate // through elements of the sequence. -impl<'de, 'a> SeqAccess<'de> for SimpleSeq<'a> { +impl<'de, 'a, T: BipackSource> SeqAccess<'de> for SimpleSeq<'a, T> { type Error = Error; - fn next_element_seed(&mut self, seed: T) -> Result> + fn next_element_seed(&mut self, seed: S) -> Result> where - T: DeserializeSeed<'de>, + S: DeserializeSeed<'de>, { if self.size < 1 { return Ok(None); @@ -238,12 +236,12 @@ impl<'de, 'a> SeqAccess<'de> for SimpleSeq<'a> { } -struct SimpleMap<'a> { - de: &'a mut Deserializer, +struct SimpleMap<'a, T: BipackSource> { + de: &'a mut Deserializer, size: usize, } -impl<'de, 'a> MapAccess<'de> for SimpleMap<'a> { +impl<'de, 'a, T: BipackSource> MapAccess<'de> for SimpleMap<'a,T> { type Error = Error; fn next_key_seed(&mut self, seed: K) -> std::result::Result, Self::Error> where K: DeserializeSeed<'de> {