Skip to content

Commit

Permalink
Add sync-to-async and async-to-sync conversion routines
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jul 31, 2022
1 parent 8ce2370 commit c00bc06
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/reader/async_tokio.rs
Expand Up @@ -183,6 +183,26 @@ impl<R: AsyncBufRead + Unpin> Reader<TokioAdapter<R>> {
}
}

/// Converts any synchronous reader to asynchronous one if inner reader supports that
impl<R: AsyncBufRead + Unpin> From<Reader<R>> for Reader<TokioAdapter<R>> {
fn from(reader: Reader<R>) -> Self {
Self {
reader: TokioAdapter(reader.reader),
parser: reader.parser,
}
}
}

/// Converts any asynchronous reader to a synchronous one if inner reader supports that
impl<R> From<Reader<TokioAdapter<R>>> for Reader<R> {
fn from(reader: Reader<TokioAdapter<R>>) -> Self {
Self {
reader: reader.reader.0,
parser: reader.parser,
}
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////

impl<R: AsyncBufRead + Unpin> NsReader<TokioAdapter<R>> {
Expand Down Expand Up @@ -396,6 +416,30 @@ impl<R: AsyncBufRead + Unpin> NsReader<TokioAdapter<R>> {
}
}

/// Converts any synchronous reader to asynchronous one if inner reader supports that
impl<R: AsyncBufRead + Unpin> From<NsReader<R>> for NsReader<TokioAdapter<R>> {
fn from(reader: NsReader<R>) -> Self {
Self {
reader: reader.reader.into(),
buffer: reader.buffer,
ns_resolver: reader.ns_resolver,
pending_pop: reader.pending_pop,
}
}
}

/// Converts any asynchronous reader to a synchronous one if inner reader supports that
impl<R> From<NsReader<TokioAdapter<R>>> for NsReader<R> {
fn from(reader: NsReader<TokioAdapter<R>>) -> Self {
Self {
reader: reader.reader.into(),
buffer: reader.buffer,
ns_resolver: reader.ns_resolver,
pending_pop: reader.pending_pop,
}
}
}

#[cfg(test)]
mod test {
use super::TokioAdapter;
Expand Down
6 changes: 3 additions & 3 deletions src/reader/ns_reader.rs
Expand Up @@ -22,13 +22,13 @@ pub struct NsReader<R> {
pub(super) reader: Reader<R>,
/// Buffer that contains names of namespace prefixes (the part between `xmlns:`
/// and an `=`) and namespace values.
buffer: Vec<u8>,
pub(super) buffer: Vec<u8>,
/// A buffer to manage namespaces
ns_resolver: NamespaceResolver,
pub(super) ns_resolver: NamespaceResolver,
/// We cannot pop data from the namespace stack until returned `Empty` or `End`
/// event will be processed by the user, so we only mark that we should that
/// in the next [`Self::read_event_impl()`] call.
pending_pop: bool,
pub(super) pending_pop: bool,
}

/// Builder methods
Expand Down

0 comments on commit c00bc06

Please sign in to comment.