Skip to content

Commit 0f9d486

Browse files
committed
Use a direct check for the absence of unconsumed data instead of hackish second attempt to deserialize
1 parent b7787b0 commit 0f9d486

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/de/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,11 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
21152115
}
21162116
}
21172117

2118+
/// Returns `true` if all events was consumed
2119+
fn is_empty(&self) -> bool {
2120+
matches!(self.lookahead, Ok(PayloadEvent::Eof))
2121+
}
2122+
21182123
/// Read next event and put it in lookahead, return the current lookahead
21192124
#[inline(always)]
21202125
fn next_impl(&mut self) -> Result<PayloadEvent<'i>, DeError> {
@@ -2374,6 +2379,19 @@ where
23742379
}
23752380
}
23762381

2382+
/// Returns `true` if all events was consumed.
2383+
pub fn is_empty(&self) -> bool {
2384+
#[cfg(feature = "overlapped-lists")]
2385+
if self.read.is_empty() {
2386+
return self.reader.is_empty();
2387+
}
2388+
#[cfg(not(feature = "overlapped-lists"))]
2389+
if self.peek.is_none() {
2390+
return self.reader.is_empty();
2391+
}
2392+
false
2393+
}
2394+
23772395
/// Set the maximum number of events that could be skipped during deserialization
23782396
/// of sequences.
23792397
///
@@ -2895,7 +2913,11 @@ where
28952913
T: DeserializeSeed<'de>,
28962914
{
28972915
match self.peek()? {
2898-
DeEvent::Eof => Ok(None),
2916+
DeEvent::Eof => {
2917+
// We need to consume event in order to self.is_empty() worked
2918+
self.next()?;
2919+
Ok(None)
2920+
}
28992921

29002922
// Start(tag), End(tag), Text
29012923
_ => seed.deserialize(&mut **self).map(Some),

tests/helpers/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ where
1717

1818
// If type was deserialized, the whole XML document should be consumed
1919
if let Ok(_) = result {
20-
match <()>::deserialize(&mut de) {
21-
Err(DeError::UnexpectedEof) => (),
22-
e => panic!("Expected end `UnexpectedEof`, but got {:?}", e),
23-
}
20+
assert!(de.is_empty(), "the whole XML document should be consumed");
2421
}
2522

2623
result

0 commit comments

Comments
 (0)