Skip to content

Commit dc9be85

Browse files
committed
Return the number of bytes used in the deserialization process
1 parent 238c8fe commit dc9be85

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

@@ -678,19 +678,20 @@ impl fmt::Display for Error {
678678
}
679679

680680
/// Deserializes an instance of type `T` from bytes of JSON text
681-
pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<T>
681+
/// Returns the value and the number of bytes consumed in the process
682+
pub fn from_slice<'a, T>(v: &'a [u8]) -> Result<(T, usize)>
682683
where
683684
T: de::Deserialize<'a>,
684685
{
685686
let mut de = Deserializer::new(v);
686687
let value = de::Deserialize::deserialize(&mut de)?;
687-
de.end()?;
688+
let length = de.end()?;
688689

689-
Ok(value)
690+
Ok((value, length))
690691
}
691692

692693
/// Deserializes an instance of type T from a string of JSON text
693-
pub fn from_str<'a, T>(s: &'a str) -> Result<T>
694+
pub fn from_str<'a, T>(s: &'a str) -> Result<(T, usize)>
694695
where
695696
T: de::Deserialize<'a>,
696697
{

0 commit comments

Comments
 (0)