From 563393596407e9a2a0a7ced7229b21d78d294db1 Mon Sep 17 00:00:00 2001 From: boxdot Date: Mon, 3 Sep 2018 11:40:25 +0200 Subject: [PATCH] Introduce `Config::deserialize_seed` for exposing serde's statefull deser. --- src/config.rs | 6 ++++++ src/internal.rs | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/config.rs b/src/config.rs index cebd0be..73a4423 100644 --- a/src/config.rs +++ b/src/config.rs @@ -266,6 +266,12 @@ impl Config { config_map!(self, opts => ::internal::deserialize_in_place(reader, opts, place)) } + /// Deserializes a slice of bytes with state `seed` using this configuration. + #[inline(always)] + pub fn deserialize_seed<'a, T: serde::de::DeserializeSeed<'a>>(&self, seed: T, bytes: &'a [u8]) -> Result { + config_map!(self, opts => ::internal::deserialize_seed(seed, bytes, opts)) + } + /// Deserializes an object directly from a `Read`er using this configuration /// /// If this returns an `Error`, `reader` may be in an invalid state. diff --git a/src/internal.rs b/src/internal.rs index bab4a28..4241784 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -115,6 +115,17 @@ where } +pub(crate) fn deserialize_seed<'a, T, O>(seed: T, bytes: &'a [u8], options: O) -> Result +where + T: serde::de::DeserializeSeed<'a>, + O: Options, +{ + let reader = ::de::read::SliceReader::new(bytes); + let options = ::config::WithOtherLimit::new(options, Infinite); + let mut deserializer = ::de::Deserializer::new(reader, options); + seed.deserialize(&mut deserializer) +} + pub(crate) trait SizeLimit: Clone { /// Tells the SizeLimit that a certain number of bytes has been /// read or written. Returns Err if the limit has been exceeded.