From 2a061611a4bd3ff5f1360f83b7f6f259f8bc7175 Mon Sep 17 00:00:00 2001 From: Hendrik Wolff Date: Thu, 25 Jul 2024 00:47:46 +0200 Subject: [PATCH] refactor: remove unused code from deserializer Signed-off-by: Hendrik Wolff --- ciborium/src/de/mod.rs | 82 ++++------------------------------------ ciborium/src/value/de.rs | 6 ++- 2 files changed, 11 insertions(+), 77 deletions(-) diff --git a/ciborium/src/de/mod.rs b/ciborium/src/de/mod.rs index f0a1e4e..18742e4 100644 --- a/ciborium/src/de/mod.rs +++ b/ciborium/src/de/mod.rs @@ -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 { fn expected(self, kind: &'static str) -> E; @@ -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), @@ -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) }), } @@ -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) }); } @@ -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; - - #[inline] - fn deserialize_any>(self, visitor: V) -> Result { - 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; - - #[inline] - fn next_element_seed>( - &mut self, - seed: U, - ) -> Result, 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 { - 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. /// diff --git a/ciborium/src/value/de.rs b/ciborium/src/value/de.rs index 30e69ee..f58a017 100644 --- a/ciborium/src/value/de.rs +++ b/ciborium/src/value/de.rs @@ -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}; @@ -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) } @@ -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); }