From ad32bbe26e19d90bcdbed94c109d4201b64cc0d9 Mon Sep 17 00:00:00 2001 From: sergeych Date: Wed, 6 Dec 2023 00:39:22 +0300 Subject: [PATCH] v0.4.3: tuple struct serialization bug --- Cargo.toml | 2 +- src/de.rs | 34 +++++++++++++++++++++++++++++++--- src/ser.rs | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 323247f..bbdc1de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bipack_ru" -version = "0.4.2" +version = "0.4.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/de.rs b/src/de.rs index c0ee031..ff88dbb 100644 --- a/src/de.rs +++ b/src/de.rs @@ -3,7 +3,7 @@ use serde::de::value::U32Deserializer; use serde::Deserialize; use crate::bipack_source::{BipackError, BipackSource, SliceSource}; -use crate::error::{Result}; +use crate::error::Result; pub struct Deserializer { // This string starts with the input data and characters are truncated off @@ -264,12 +264,12 @@ mod tests { use std::fmt::Debug; use serde::{Deserialize, Serialize}; - use crate::bipack_source::BipackError; + use crate::bipack_source::BipackError; use crate::de::from_bytes; use crate::error::Result; use crate::ser::{to_buffer, to_bytes}; - use crate::tools::to_dump; + use crate::tools::{to_dump}; #[test] fn test_ints() -> Result<()> { @@ -406,4 +406,32 @@ mod tests { assert_eq!(6, to_bytes(&z).unwrap().len()); assert_eq!(z, to_bytes(&z).unwrap()[1..]); } + + // This is real life sample bug: + #[derive(Debug,Serialize,Deserialize,Eq, PartialEq)] + pub enum LogLevel { + Debug, + Info, + Warning, + Error, + } + + #[derive(Serialize,Deserialize,Debug,PartialEq)] + pub struct LogArgs( + pub LogLevel, + pub String + ); + + #[test] + fn test_logargs() { + let x = LogArgs(LogLevel::Debug,"hello".to_string()); + let packed = to_bytes(&x).unwrap(); + // println!("{}", packed.to_dump()); + let y: LogArgs = from_bytes(&packed).unwrap(); + // println!("{:?}", y); + assert_eq!(LogLevel::Debug, y.0); + assert_eq!("hello", y.1); + assert_eq!(7, packed.len()); + } + } \ No newline at end of file diff --git a/src/ser.rs b/src/ser.rs index b7d8483..80f058d 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -172,7 +172,7 @@ impl<'a, S: BipackSink> ser::Serializer for &'a mut Serializer { _name: &'static str, len: usize, ) -> Result { - self.serialize_seq(Some(len)) + Ok(self) } fn serialize_tuple_variant(