From dc9be8576b5b2af4b84ad10b5aec7fddec6d6334 Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Fri, 21 Feb 2020 12:27:16 +0000 Subject: [PATCH] Return the number of bytes used in the deserialization process --- src/de/mod.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/de/mod.rs b/src/de/mod.rs index c838c881a..b5bb33be5 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -99,10 +99,10 @@ impl<'a> Deserializer<'a> { self.index += 1; } - fn end(&mut self) -> Result<()> { + fn end(&mut self) -> Result { match self.parse_whitespace() { Some(_) => Err(Error::TrailingCharacters), - None => Ok(()), + None => Ok(self.index), } } @@ -678,19 +678,20 @@ impl fmt::Display for Error { } /// Deserializes an instance of type `T` from bytes of JSON text -pub fn from_slice<'a, T>(v: &'a [u8]) -> Result +/// Returns the value and the number of bytes consumed in the process +pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<(T, usize)> where T: de::Deserialize<'a>, { let mut de = Deserializer::new(v); let value = de::Deserialize::deserialize(&mut de)?; - de.end()?; + let length = de.end()?; - Ok(value) + Ok((value, length)) } /// Deserializes an instance of type T from a string of JSON text -pub fn from_str<'a, T>(s: &'a str) -> Result +pub fn from_str<'a, T>(s: &'a str) -> Result<(T, usize)> where T: de::Deserialize<'a>, {