Skip to content

Commit 9f435a1

Browse files
authored
fixing the issue regarding serialization and deserialization of nested enums would not match. (#5)
1 parent 937ca76 commit 9f435a1

File tree

5 files changed

+67
-27
lines changed

5 files changed

+67
-27
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bcs"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
authors = ["Diem <[email protected]>"]
55
description = "Binary Canonical Serialization (BCS)"
66
repository = "https://github.com/diem/bcs"

src/de.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -369,22 +369,22 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
369369
self.leave_named_container();
370370
r
371371
}
372-
372+
#[allow(clippy::needless_borrow)]
373373
fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value>
374374
where
375375
V: Visitor<'de>,
376376
{
377377
let len = self.parse_length()?;
378378
visitor.visit_seq(SeqDeserializer::new(&mut self, len))
379379
}
380-
380+
#[allow(clippy::needless_borrow)]
381381
fn deserialize_tuple<V>(mut self, len: usize, visitor: V) -> Result<V::Value>
382382
where
383383
V: Visitor<'de>,
384384
{
385385
visitor.visit_seq(SeqDeserializer::new(&mut self, len))
386386
}
387-
387+
#[allow(clippy::needless_borrow)]
388388
fn deserialize_tuple_struct<V>(
389389
mut self,
390390
name: &'static str,
@@ -399,15 +399,15 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
399399
self.leave_named_container();
400400
r
401401
}
402-
402+
#[allow(clippy::needless_borrow)]
403403
fn deserialize_map<V>(mut self, visitor: V) -> Result<V::Value>
404404
where
405405
V: Visitor<'de>,
406406
{
407407
let len = self.parse_length()?;
408408
visitor.visit_map(MapDeserializer::new(&mut self, len))
409409
}
410-
410+
#[allow(clippy::needless_borrow)]
411411
fn deserialize_struct<V>(
412412
mut self,
413413
name: &'static str,
@@ -464,7 +464,7 @@ struct SeqDeserializer<'a, 'de: 'a> {
464464
de: &'a mut Deserializer<'de>,
465465
remaining: usize,
466466
}
467-
467+
#[allow(clippy::needless_borrow)]
468468
impl<'a, 'de> SeqDeserializer<'a, 'de> {
469469
fn new(de: &'a mut Deserializer<'de>, remaining: usize) -> Self {
470470
Self { de, remaining }

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fmt;
66
use thiserror::Error;
77

88
pub type Result<T, E = Error> = std::result::Result<T, E>;
9-
9+
#[allow(clippy::derive_partial_eq_without_eq)]
1010
#[derive(Clone, Debug, Error, PartialEq)]
1111
pub enum Error {
1212
#[error("unexpected end of input")]

src/ser.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,11 @@ where
260260

261261
fn serialize_unit_variant(
262262
mut self,
263-
_name: &'static str,
263+
name: &'static str,
264264
variant_index: u32,
265265
_variant: &'static str,
266266
) -> Result<()> {
267+
self.enter_named_container(name)?;
267268
self.output_variant_index(variant_index)
268269
}
269270

tests/serde.rs

+57-18
Original file line numberDiff line numberDiff line change
@@ -570,26 +570,38 @@ fn serde_known_vector() {
570570
}
571571

572572
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
573-
struct List {
574-
next: Option<(usize, Box<List>)>,
573+
struct List<T> {
574+
value: T,
575+
next: Option<Box<List<T>>>,
575576
}
576-
577-
impl List {
578-
fn empty() -> Self {
579-
Self { next: None }
577+
impl<T> List<T> {
578+
fn head(value: T) -> Self {
579+
Self { value, next: None }
580580
}
581581

582-
fn cons(value: usize, tail: List) -> Self {
582+
fn cons(value: T, tail: List<T>) -> Self {
583583
Self {
584-
next: Some((value, Box::new(tail))),
584+
value,
585+
next: Some(Box::new(tail)),
586+
}
587+
}
588+
}
589+
impl<T: Clone> List<T> {
590+
fn repeat(len: usize, value: T) -> Self {
591+
if len == 0 {
592+
Self::head(value)
593+
} else {
594+
Self::cons(value.clone(), Self::repeat(len - 1, value))
585595
}
586596
}
597+
}
587598

599+
impl List<usize> {
588600
fn integers(len: usize) -> Self {
589601
if len == 0 {
590-
Self::empty()
602+
Self::head(0)
591603
} else {
592-
Self::cons(len - 1, Self::integers(len - 1))
604+
Self::cons(len, Self::integers(len - 1))
593605
}
594606
}
595607
}
@@ -601,31 +613,30 @@ fn test_recursion_limit() {
601613
assert_eq!(
602614
b1,
603615
vec![
604-
1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
605-
0, 0, 0, 0, 0, 0, 0, 0
616+
4, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
617+
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
606618
]
607619
);
608-
assert_eq!(from_bytes::<List>(&b1).unwrap(), l1);
620+
assert_eq!(from_bytes::<List<_>>(&b1).unwrap(), l1);
609621

610622
let l2 = List::integers(MAX_CONTAINER_DEPTH - 1);
611623
let b2 = to_bytes(&l2).unwrap();
612-
assert_eq!(from_bytes::<List>(&b2).unwrap(), l2);
613-
624+
assert_eq!(from_bytes::<List<_>>(&b2).unwrap(), l2);
614625
let l3 = List::integers(MAX_CONTAINER_DEPTH);
615626
assert_eq!(
616627
to_bytes(&l3),
617628
Err(Error::ExceededContainerDepthLimit("List"))
618629
);
619-
let mut b3 = vec![1, 243, 1, 0, 0, 0, 0, 0, 0];
630+
let mut b3 = vec![244, 1, 0, 0, 0, 0, 0, 0, 1];
620631
b3.extend(b2);
621632
assert_eq!(
622-
from_bytes::<List>(&b3),
633+
from_bytes::<List<usize>>(&b3),
623634
Err(Error::ExceededContainerDepthLimit("List"))
624635
);
625636

626637
let b2_pair = to_bytes(&(&l2, &l2)).unwrap();
627638
assert_eq!(
628-
from_bytes::<(List, List)>(&b2_pair).unwrap(),
639+
from_bytes::<(List<_>, List<_>)>(&b2_pair).unwrap(),
629640
(l2.clone(), l2.clone())
630641
);
631642
assert_eq!(
@@ -641,3 +652,31 @@ fn test_recursion_limit() {
641652
Err(Error::ExceededContainerDepthLimit("List"))
642653
);
643654
}
655+
#[derive(Deserialize, Serialize, Clone, PartialEq, Eq, Debug)]
656+
enum EnumA {
657+
ValueA,
658+
}
659+
660+
#[test]
661+
fn test_recursion_limit_enum() {
662+
let l1 = List::repeat(6, EnumA::ValueA);
663+
let b1 = to_bytes(&l1).unwrap();
664+
assert_eq!(b1, vec![0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0],);
665+
assert_eq!(from_bytes::<List<_>>(&b1).unwrap(), l1);
666+
667+
let l2 = List::repeat(MAX_CONTAINER_DEPTH - 2, EnumA::ValueA);
668+
let b2 = to_bytes(&l2).unwrap();
669+
assert_eq!(from_bytes::<List<_>>(&b2).unwrap(), l2);
670+
671+
let l3 = List::repeat(MAX_CONTAINER_DEPTH - 1, EnumA::ValueA);
672+
assert_eq!(
673+
to_bytes(&l3),
674+
Err(Error::ExceededContainerDepthLimit("EnumA"))
675+
);
676+
let mut b3 = vec![0, 1];
677+
b3.extend(b2);
678+
assert_eq!(
679+
from_bytes::<List<EnumA>>(&b3),
680+
Err(Error::ExceededContainerDepthLimit("EnumA"))
681+
);
682+
}

0 commit comments

Comments
 (0)