Skip to content

Commit c00bc06

Browse files
committed
Add sync-to-async and async-to-sync conversion routines
1 parent 8ce2370 commit c00bc06

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/reader/async_tokio.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ impl<R: AsyncBufRead + Unpin> Reader<TokioAdapter<R>> {
183183
}
184184
}
185185

186+
/// Converts any synchronous reader to asynchronous one if inner reader supports that
187+
impl<R: AsyncBufRead + Unpin> From<Reader<R>> for Reader<TokioAdapter<R>> {
188+
fn from(reader: Reader<R>) -> Self {
189+
Self {
190+
reader: TokioAdapter(reader.reader),
191+
parser: reader.parser,
192+
}
193+
}
194+
}
195+
196+
/// Converts any asynchronous reader to a synchronous one if inner reader supports that
197+
impl<R> From<Reader<TokioAdapter<R>>> for Reader<R> {
198+
fn from(reader: Reader<TokioAdapter<R>>) -> Self {
199+
Self {
200+
reader: reader.reader.0,
201+
parser: reader.parser,
202+
}
203+
}
204+
}
205+
186206
////////////////////////////////////////////////////////////////////////////////////////////////////
187207

188208
impl<R: AsyncBufRead + Unpin> NsReader<TokioAdapter<R>> {
@@ -396,6 +416,30 @@ impl<R: AsyncBufRead + Unpin> NsReader<TokioAdapter<R>> {
396416
}
397417
}
398418

419+
/// Converts any synchronous reader to asynchronous one if inner reader supports that
420+
impl<R: AsyncBufRead + Unpin> From<NsReader<R>> for NsReader<TokioAdapter<R>> {
421+
fn from(reader: NsReader<R>) -> Self {
422+
Self {
423+
reader: reader.reader.into(),
424+
buffer: reader.buffer,
425+
ns_resolver: reader.ns_resolver,
426+
pending_pop: reader.pending_pop,
427+
}
428+
}
429+
}
430+
431+
/// Converts any asynchronous reader to a synchronous one if inner reader supports that
432+
impl<R> From<NsReader<TokioAdapter<R>>> for NsReader<R> {
433+
fn from(reader: NsReader<TokioAdapter<R>>) -> Self {
434+
Self {
435+
reader: reader.reader.into(),
436+
buffer: reader.buffer,
437+
ns_resolver: reader.ns_resolver,
438+
pending_pop: reader.pending_pop,
439+
}
440+
}
441+
}
442+
399443
#[cfg(test)]
400444
mod test {
401445
use super::TokioAdapter;

src/reader/ns_reader.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ pub struct NsReader<R> {
2222
pub(super) reader: Reader<R>,
2323
/// Buffer that contains names of namespace prefixes (the part between `xmlns:`
2424
/// and an `=`) and namespace values.
25-
buffer: Vec<u8>,
25+
pub(super) buffer: Vec<u8>,
2626
/// A buffer to manage namespaces
27-
ns_resolver: NamespaceResolver,
27+
pub(super) ns_resolver: NamespaceResolver,
2828
/// We cannot pop data from the namespace stack until returned `Empty` or `End`
2929
/// event will be processed by the user, so we only mark that we should that
3030
/// in the next [`Self::read_event_impl()`] call.
31-
pending_pop: bool,
31+
pub(super) pending_pop: bool,
3232
}
3333

3434
/// Builder methods

0 commit comments

Comments
 (0)