Skip to content

Commit

Permalink
refactor: remove unused code from deserializer
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrik Wolff <[email protected]>
  • Loading branch information
hw0lff authored and rjzak committed Aug 3, 2024
1 parent bb03b26 commit 2a06161
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 77 deletions.
82 changes: 7 additions & 75 deletions ciborium/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use alloc::{string::String, vec::Vec};

use ciborium_io::Read;
use ciborium_ll::*;
use serde::{
de::{self, value::BytesDeserializer, Deserializer as _},
forward_to_deserialize_any,
};
use serde::de::{self, value::BytesDeserializer, Deserializer as _};

use crate::tag::TagAccess;

trait Expected<E: de::Error> {
fn expected(self, kind: &'static str) -> E;
Expand Down Expand Up @@ -182,20 +181,14 @@ where
Header::Tag(tag) => {
let _: Header = self.decoder.pull()?;

// Peek at the next item.
let header = self.decoder.pull()?;
self.decoder.push(header);

match tag {
tag::BIGPOS | tag::BIGNEG => {
let mut bytes = Vec::new();
let result =
match self.integer(Some(Header::Tag(tag)), true, |b| bytes.push(b))? {
(false, _) if !bytes.is_empty() => {
let access = crate::tag::TagAccess::new(
BytesDeserializer::new(&bytes),
Some(tag),
);
let access =
TagAccess::new(BytesDeserializer::new(&bytes), Some(tag));
return visitor.visit_enum(access);
}
(false, raw) => return visitor.visit_u128(raw),
Expand All @@ -209,7 +202,7 @@ where
}

_ => self.recurse(|me| {
let access = crate::tag::TagAccess::new(me, Some(tag));
let access = TagAccess::new(me, Some(tag));
visitor.visit_enum(access)
}),
}
Expand Down Expand Up @@ -608,7 +601,7 @@ where
};

return self.recurse(|me| {
let access = crate::tag::TagAccess::new(me, tag);
let access = TagAccess::new(me, tag);
visitor.visit_enum(access)
});
}
Expand Down Expand Up @@ -787,67 +780,6 @@ where
}
}

struct TagAccess<'a, 'b, R>(&'a mut Deserializer<'b, R>, usize);

impl<'de, 'a, 'b, R: Read> de::Deserializer<'de> for &mut TagAccess<'a, 'b, R>
where
R::Error: core::fmt::Debug,
{
type Error = Error<R::Error>;

#[inline]
fn deserialize_any<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
let offset = self.0.decoder.offset();

match self.0.decoder.pull()? {
Header::Tag(x) => visitor.visit_u64(x),
_ => Err(Error::semantic(offset, "expected tag")),
}
}

forward_to_deserialize_any! {
i8 i16 i32 i64 i128
u8 u16 u32 u64 u128
bool f32 f64
char str string
bytes byte_buf
seq map
struct tuple tuple_struct
identifier ignored_any
option unit unit_struct newtype_struct enum
}
}

impl<'de, 'a, 'b, R: Read> de::SeqAccess<'de> for TagAccess<'a, 'b, R>
where
R::Error: core::fmt::Debug,
{
type Error = Error<R::Error>;

#[inline]
fn next_element_seed<U: de::DeserializeSeed<'de>>(
&mut self,
seed: U,
) -> Result<Option<U::Value>, Self::Error> {
self.1 += 1;

match self.1 {
1 => seed.deserialize(self).map(Some),
2 => seed.deserialize(&mut *self.0).map(Some),
_ => Ok(None),
}
}

#[inline]
fn size_hint(&self) -> Option<usize> {
Some(match self.1 {
0 => 2,
1 => 1,
_ => 0,
})
}
}

/// Deserializes as CBOR from a type with [`impl
/// ciborium_io::Read`](ciborium_io::Read) using a 4KB buffer on the stack.
///
Expand Down
6 changes: 4 additions & 2 deletions ciborium/src/value/de.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::tag::TagAccess;

use super::{Error, Integer, Value};

use alloc::{boxed::Box, string::String, vec::Vec};
Expand Down Expand Up @@ -236,7 +238,7 @@ impl<'a, 'de> de::Deserializer<'de> for Deserializer<&'a Value> {

Value::Tag(t, v) => {
let parent: Deserializer<&Value> = Deserializer(v);
let access = crate::tag::TagAccess::new(parent, Some(*t));
let access = TagAccess::new(parent, Some(*t));
visitor.visit_enum(access)
}

Expand Down Expand Up @@ -489,7 +491,7 @@ impl<'a, 'de> de::Deserializer<'de> for Deserializer<&'a Value> {
};

let parent: Deserializer<&Value> = Deserializer(val);
let access = crate::tag::TagAccess::new(parent, tag);
let access = TagAccess::new(parent, tag);
return visitor.visit_enum(access);
}

Expand Down

0 comments on commit 2a06161

Please sign in to comment.