Skip to content

Commit a0bc7e9

Browse files
authored
Merge pull request #26 from MabezDev/return-consumed-by-de
Return the number of bytes used in the deserialization process
2 parents 2e62c36 + dc9be85 commit a0bc7e9

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/de/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ impl<'a> Deserializer<'a> {
9999
self.index += 1;
100100
}
101101

102-
fn end(&mut self) -> Result<()> {
102+
fn end(&mut self) -> Result<usize> {
103103
match self.parse_whitespace() {
104104
Some(_) => Err(Error::TrailingCharacters),
105-
None => Ok(()),
105+
None => Ok(self.index),
106106
}
107107
}
108108

@@ -705,19 +705,20 @@ impl fmt::Display for Error {
705705
}
706706

707707
/// Deserializes an instance of type `T` from bytes of JSON text
708-
pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T>
708+
/// Returns the value and the number of bytes consumed in the process
709+
pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<(T, usize)>
709710
where
710711
T: de::Deserialize<'a>,
711712
{
712713
let mut de = Deserializer::new(v);
713714
let value = de::Deserialize::deserialize(&mut de)?;
714-
de.end()?;
715+
let length = de.end()?;
715716

716-
Ok(value)
717+
Ok((value, length))
717718
}
718719

719720
/// Deserializes an instance of type T from a string of JSON text
720-
pub fn from_str<'a, T>(s: &'a str) -> Result<T>
721+
pub fn from_str<'a, T>(s: &'a str) -> Result<(T, usize)>
721722
where
722723
T: de::Deserialize<'a>,
723724
{

0 commit comments

Comments
 (0)