Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to read an event and not consume it? #414

Open
ImJeremyHe opened this issue Jul 9, 2022 · 1 comment
Open

Is there any way to read an event and not consume it? #414

ImJeremyHe opened this issue Jul 9, 2022 · 1 comment

Comments

@ImJeremyHe
Copy link

just like peek() in the iterators

@Mingun
Copy link
Collaborator

Mingun commented Jul 9, 2022

Reader does not buffer events that it reads, so you cannot peek them, but you can create your own wrapper around the reader that would store the last readed event. That approach is used in the Deserializer:

quick-xml/src/de/mod.rs

Lines 473 to 486 in 2eecf00

#[cfg(not(feature = "overlapped-lists"))]
fn peek(&mut self) -> Result<&DeEvent<'de>, DeError> {
if self.peek.is_none() {
self.peek = Some(self.reader.next()?);
}
match self.peek.as_ref() {
Some(v) => Ok(v),
// SAFETY: a `None` variant for `self.peek` would have been replaced
// by a `Some` variant in the code above.
// TODO: Can be replaced with `unsafe { std::hint::unreachable_unchecked() }`
// if unsafe code will be allowed
None => unreachable!(),
}
}

We could introduce a PeekableReader that will have that logic. PR is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants