From 551fed9383070757b98f3a677e4b224cc4da7484 Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 31 Jul 2022 18:22:36 +0500 Subject: [PATCH] Add sync-to-async and async-to-sync conversion routines --- src/reader/async_tokio.rs | 44 +++++++++++++++++++++++++++++++++++++++ src/reader/ns_reader.rs | 6 +++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/reader/async_tokio.rs b/src/reader/async_tokio.rs index d850dfee..6d04300f 100644 --- a/src/reader/async_tokio.rs +++ b/src/reader/async_tokio.rs @@ -183,6 +183,26 @@ impl Reader> { } } +/// Converts any synchronous reader to asynchronous one if inner reader supports that +impl From> for Reader> { + fn from(reader: Reader) -> Self { + Self { + reader: TokioAdapter(reader.reader), + parser: reader.parser, + } + } +} + +/// Converts any asynchronous reader to a synchronous one if inner reader supports that +impl From>> for Reader { + fn from(reader: Reader>) -> Self { + Self { + reader: reader.reader.0, + parser: reader.parser, + } + } +} + //////////////////////////////////////////////////////////////////////////////////////////////////// impl NsReader> { @@ -396,6 +416,30 @@ impl NsReader> { } } +/// Converts any synchronous reader to asynchronous one if inner reader supports that +impl From> for NsReader> { + fn from(reader: NsReader) -> 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 From>> for NsReader { + fn from(reader: NsReader>) -> 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; diff --git a/src/reader/ns_reader.rs b/src/reader/ns_reader.rs index 1c2148be..6c6a4178 100644 --- a/src/reader/ns_reader.rs +++ b/src/reader/ns_reader.rs @@ -22,13 +22,13 @@ pub struct NsReader { pub(super) reader: Reader, /// Buffer that contains names of namespace prefixes (the part between `xmlns:` /// and an `=`) and namespace values. - buffer: Vec, + pub(super) buffer: Vec, /// 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