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

Implement basic async writer #573

Merged
merged 5 commits into from
Mar 11, 2023
Merged

Implement basic async writer #573

merged 5 commits into from
Mar 11, 2023

Conversation

vilunov
Copy link
Contributor

@vilunov vilunov commented Mar 5, 2023

This adds support for async byte writers in the already existing Writer type. I have not added support for indentation here, that's why only the new construct has loosened type bounds, but it can be added in a follow-up merge request.

@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2023

Codecov Report

Merging #573 (5adef14) into master (9b220f1) will decrease coverage by 0.06%.
The diff coverage is 98.85%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master     #573      +/-   ##
==========================================
- Coverage   64.21%   64.15%   -0.06%     
==========================================
  Files          33       34       +1     
  Lines       16583    16598      +15     
==========================================
+ Hits        10648    10649       +1     
- Misses       5935     5949      +14     
Flag Coverage Δ
unittests 64.15% <98.85%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/writer/async_tokio.rs 98.76% <98.76%> (ø)
src/writer.rs 88.71% <100.00%> (ø)
src/de/mod.rs 69.33% <0.00%> (-1.31%) ⬇️
src/escapei.rs 13.24% <0.00%> (-0.18%) ⬇️
src/reader/mod.rs 94.93% <0.00%> (-0.11%) ⬇️
src/lib.rs 20.20% <0.00%> (+0.06%) ⬆️
src/de/map.rs 83.93% <0.00%> (+0.51%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Collaborator

@Mingun Mingun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation for Writer on line 12 became outdated -- now we can write to AsyncWrite as well. Could you update it accordingly?

It seems that implementing writing with indent should be not so hard -- it looks that you just need to copy existing code and replace all sync write methods with async counterparts. Did you try that? If there are some difficulties, then it can be postponed to other PR.

use crate::Writer;

impl<W: AsyncWrite + Unpin> Writer<W> {
/// Writes the given event to the underlying writer. Async version of [Writer::write_event].
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Writes the given event to the underlying writer. Async version of [Writer::write_event].
/// Writes the given event to the underlying writer. Async version of [`Writer::write_event`].

Comment on lines 53 to 54
#[tokio::test]
async fn xml_header() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add tests for other events as well? Then all paths will be covered

@vilunov
Copy link
Contributor Author

vilunov commented Mar 5, 2023

It seems that implementing writing with indent should be not so hard

It seems easy indeed, but here I'm just submitting changes which I've already made and tested in my own project, and since I don't really use indentation support there, I can't claim that what I'd make would work correctly. I'd rather implement it in a separate PR.

@dralley
Copy link
Collaborator

dralley commented Mar 5, 2023

Just out of personal curiosity, do you have a particular use case in mind where this is needed?

@vilunov
Copy link
Contributor Author

vilunov commented Mar 5, 2023

I'm implementing an XML-based duplex stream API. I'm going this the fully async way, that means tokio, its TcpSockets, most of the bufferized but sometimes direct. That means I need to write my data structures into that kind of byte streams. For some time I've been using the sync API and writing it into an intermediate buffer, but that means that most of the time I have two buffers: one in BufWriter and one my own Vec<u8>. So I worked around that by implementing async APIs and it worked well since then.

A typical (desugared) serialization looks like this:

pub struct Message {
    body: String,
}
impl Message {
    pub async fn write_xml(&self, writer: &mut Writer<impl AsyncWrite + Unpin>) -> Result<()> {
        writer.write_event_async(Event::Start(BytesStart::new(r#"message"#))).await?;
        writer.write_event_async(Event::Text(BytesText::new(&self.body))).await?;
        writer.write_event_async(Event::End(BytesEnd::new("message"))).await?;
        Ok(())
    }
}

@Mingun Mingun merged commit f822669 into tafia:master Mar 11, 2023
crapStone added a commit to Calciumdibromid/CaBr2 that referenced this pull request Mar 16, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [quick-xml](https://github.com/tafia/quick-xml) | dependencies | minor | `0.27.1` -> `0.28.0` |

---

### Release Notes

<details>
<summary>tafia/quick-xml</summary>

### [`v0.28.0`](https://github.com/tafia/quick-xml/blob/HEAD/Changelog.md#&#8203;0280----2023-03-13)

[Compare Source](tafia/quick-xml@v0.27.1...v0.28.0)

##### New Features

-   [#&#8203;541]: (De)serialize specially named `$text` enum variant in [externally tagged]
    enums to / from textual content
-   [#&#8203;556]: `to_writer` and `to_string` now accept `?Sized` types
-   [#&#8203;556]: Add new `to_writer_with_root` and `to_string_with_root` helper functions
-   [#&#8203;520]: Add methods `BytesText::inplace_trim_start` and `BytesText::inplace_trim_end`
    to trim leading and trailing spaces from text events
-   [#&#8203;565]: Allow deserialize special field names `$value` and `$text` into borrowed
    fields when use serde deserializer
-   [#&#8203;568]: Rename `Writter::inner` into `Writter::get_mut`
-   [#&#8203;568]: Add method `Writter::get_ref`
-   [#&#8203;569]: Rewrite the `Reader::read_event_into_async` as an async fn, making the future `Send` if possible.
-   [#&#8203;571]: Borrow element names (`<element>`) when deserialize with serde.
    This change allow to deserialize into `HashMap<&str, T>`, for example
-   [#&#8203;573]: Add basic support for async byte writers via tokio's `AsyncWrite`.

##### Bug Fixes

-   [#&#8203;537]: Restore ability to deserialize attributes that represents XML namespace
    mappings (`xmlns:xxx`) that was broken since [#&#8203;490]
-   [#&#8203;510]: Fix an error of deserialization of `Option<T>` fields where `T` is some
    sequence type (for example, `Vec` or tuple)
-   [#&#8203;540]: Fix a compilation error (probably a rustc bug) in some circumstances.
    `Serializer::new` and `Serializer::with_root` now accepts only references to `Write`r.
-   [#&#8203;520]: Merge consequent (delimited only by comments and processing instructions)
    texts and CDATA when deserialize using serde deserializer. `DeEvent::Text` and
    `DeEvent::CData` events was replaced by `DeEvent::Text` with merged content.
    The same behavior for the `Reader` does not implemented (yet?) and should be
    implemented manually
-   [#&#8203;562]: Correctly set minimum required version of memchr dependency to 2.1
-   [#&#8203;565]: Correctly set minimum required version of tokio dependency to 1.10
-   [#&#8203;565]: Fix compilation error when build with serde <1.0.139

[externally tagged]: https://serde.rs/enum-representations.html#externally-tagged

[#&#8203;490]: tafia/quick-xml#490

[#&#8203;510]: tafia/quick-xml#510

[#&#8203;520]: tafia/quick-xml#520

[#&#8203;537]: tafia/quick-xml#537

[#&#8203;540]: tafia/quick-xml#540

[#&#8203;541]: tafia/quick-xml#541

[#&#8203;556]: tafia/quick-xml#556

[#&#8203;562]: tafia/quick-xml#562

[#&#8203;565]: tafia/quick-xml#565

[#&#8203;568]: tafia/quick-xml#568

[#&#8203;569]: tafia/quick-xml#569

[#&#8203;571]: tafia/quick-xml#571

[#&#8203;573]: tafia/quick-xml#573

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42LjAiLCJ1cGRhdGVkSW5WZXIiOiIzNS42LjAifQ==-->

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1818
Reviewed-by: crapStone <crapstone@noreply.codeberg.org>
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants