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

Add EncoderStringWriter #142

Merged
merged 5 commits into from Sep 28, 2020
Merged

Add EncoderStringWriter #142

merged 5 commits into from Sep 28, 2020

Conversation

marshallpierce
Copy link
Owner

No description provided.

@kevincox
Copy link

A better way to do this is probably to leave it as a ref and you can call String.as_mut_vec() to make the EncoderStringWriter. This way you don't need to own the value and don't break backwards compatibility.

@marshallpierce
Copy link
Owner Author

Surprisingly (to me), it looks like the new API for EncoderWriter is actually compatible, and thus a superset -- note that tests using &mut delegate didn't have to change.

@kevincox
Copy link

kevincox commented Sep 24, 2020

Hmm, that being mostly compatible does make sense now that you mention it. I'll continue the review then.

src/write/encoder.rs Show resolved Hide resolved
src/write/encoder_string_writer.rs Show resolved Hide resolved
src/write/encoder_string_writer.rs Outdated Show resolved Hide resolved
src/write/encoder_string_writer.rs Outdated Show resolved Hide resolved
Copy link
Owner Author

@marshallpierce marshallpierce left a comment

Choose a reason for hiding this comment

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

Thanks for the careful look!

src/write/encoder.rs Show resolved Hide resolved
src/write/encoder_string_writer.rs Show resolved Hide resolved
src/write/encoder_string_writer.rs Outdated Show resolved Hide resolved
src/write/encoder_string_writer.rs Show resolved Hide resolved
src/write/encoder_string_writer.rs Outdated Show resolved Hide resolved
/// An abstraction around infallible writes of `str`s.
///
/// Typically, this will just be String.
pub trait StrWrite {
Copy link
Owner Author

Choose a reason for hiding this comment

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

better names?

Choose a reason for hiding this comment

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

It reminds me of https://doc.rust-lang.org/std/iter/trait.Extend.html. I don't think you can use Extend directly though because I don't think it is implemented for &mut String.

You also might be able to use AsMut<String>? I think that would let you use String or &String?

Copy link
Owner Author

Choose a reason for hiding this comment

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

You also might be able to use AsMut<String>? I think that would let you use String or &String?

Do you mean as a way to avoid implementing StrWrite for &mut StrWrite? I think I want that no matter what though so that it's not only available for String and &mut String.

Choose a reason for hiding this comment

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

No, I think it may avoid the need for the trait overall. Because you can use AsMut to convert &mut String and &mut mut String to &mut String which will just allow you to call String.push_str directly.

Choose a reason for hiding this comment

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

Hmm, I'm having trouble getting the this to work.

Choose a reason for hiding this comment

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

Ah, I had the wrong trait. You can use BorrowMut.

https://rust.godbolt.org/z/bPd9z9

Copy link
Owner Author

Choose a reason for hiding this comment

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

Good to know. I think I'll keep the StrWrite though so it's a step closer to being no_std friendly -- currently all that's needed to finish that effort would be to introduce an abstraction other than Write for EncoderWriter, AFAICT.

/// An abstraction around infallible writes of `str`s.
///
/// Typically, this will just be String.
pub trait StrWrite {

Choose a reason for hiding this comment

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

It reminds me of https://doc.rust-lang.org/std/iter/trait.Extend.html. I don't think you can use Extend directly though because I don't think it is implemented for &mut String.

You also might be able to use AsMut<String>? I think that would let you use String or &String?

src/write/encoder_string_writer.rs Outdated Show resolved Hide resolved
@kevincox
Copy link

kevincox commented Sep 26, 2020 via email

@marshallpierce
Copy link
Owner Author

There wouldn't be String certainly but someone could have a non-String StrWrite, I would imagine.

I'm still not totally happy with StrWrite as a name because it's mirroring Write, and yet offers rather different semantics (no errors). What about StrSink or StrConsumer?

@kevincox
Copy link

Ah, I see what you mean. In that case it makes sense but I think you would have to drop the &str argument.

However if you wanted to make something that is useful for core you are probably better off making an API that works with bytes again.

🤷‍♀️

@marshallpierce
Copy link
Owner Author

Is &str not available without std? I thought it would be, since it doesn't mandate allocation, etc.

@kevincox
Copy link

I missed it originally because it isn't in core but str is actually a primitive type so it is available without std so you are right.

It really wasn't very much like io::Write, so best to avoid the misleading name
@marshallpierce marshallpierce merged commit 5b40e0c into master Sep 28, 2020
bors bot added a commit to comit-network/xmr-btc-swap that referenced this pull request Mar 17, 2021
332: Bump base64 from 0.12.3 to 0.13.0 r=thomaseizinger a=dependabot[bot]

Bumps [base64](https://github.com/marshallpierce/rust-base64) from 0.12.3 to 0.13.0.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/marshallpierce/rust-base64/blob/master/RELEASE-NOTES.md">base64's changelog</a>.</em></p>
<blockquote>
<h1>0.13.0</h1>
<ul>
<li>Config methods are const</li>
<li>Added <code>EncoderStringWriter</code> to allow encoding directly to a String</li>
<li><code>EncoderWriter</code> now owns its delegate writer rather than keeping a reference to it (though refs still work)
<ul>
<li>As a consequence, it is now possible to extract the delegate writer from an <code>EncoderWriter</code> via <code>finish()</code>, which returns <code>Result&lt;W&gt;</code> instead of <code>Result&lt;()&gt;</code>. If you were calling <code>finish()</code> explicitly, you will now need to use <code>let _ = foo.finish()</code> instead of just <code>foo.finish()</code> to avoid a warning about the unused value.</li>
</ul>
</li>
<li>When decoding input that has both an invalid length and an invalid symbol as the last byte, <code>InvalidByte</code> will be emitted instead of <code>InvalidLength</code> to make the problem more obvious.</li>
</ul>
<h1>0.12.2</h1>
<ul>
<li>Add <code>BinHex</code> alphabet</li>
</ul>
<h1>0.12.1</h1>
<ul>
<li>Add <code>Bcrypt</code> alphabet</li>
</ul>
<h1>0.12.0</h1>
<ul>
<li>A <code>Read</code> implementation (<code>DecoderReader</code>) to let users transparently decoded data from a b64 input source</li>
<li>IMAP's modified b64 alphabet</li>
<li>Relaxed type restrictions to just <code>AsRef&lt;[ut8]&gt;</code> for main <code>encode*</code>/<code>decode*</code> functions</li>
<li>A minor performance improvement in encoding</li>
</ul>
<h1>0.11.0</h1>
<ul>
<li>Minimum rust version 1.34.0</li>
<li><code>no_std</code> is now supported via the two new features <code>alloc</code> and <code>std</code>.</li>
</ul>
<h1>0.10.1</h1>
<ul>
<li>Minimum rust version 1.27.2</li>
<li>Fix bug in streaming encoding (<a href="https://github.com/marshallpierce/rust-base64/pull/90">#90</a>): if the underlying writer didn't write all the bytes given to it, the remaining bytes would not be retried later. See the docs on <code>EncoderWriter::write</code>.</li>
<li>Make it configurable whether or not to return an error when decoding detects excess trailing bits.</li>
</ul>
<h1>0.10.0</h1>
<ul>
<li>Remove line wrapping. Line wrapping was never a great conceptual fit in this library, and other features (streaming encoding, etc) either couldn't support it or could support only special cases of it with a great increase in complexity. Line wrapping has been pulled out into a <a href="https://crates.io/crates/line-wrap">line-wrap</a> crate, so it's still available if you need it.
<ul>
<li><code>Base64Display</code> creation no longer uses a <code>Result</code> because it can't fail, which means its helper methods for common
configs that <code>unwrap()</code> for you are no longer needed</li>
</ul>
</li>
<li>Add a streaming encoder <code>Write</code> impl to transparently base64 as you write.</li>
<li>Remove the remaining <code>unsafe</code> code.</li>
<li>Remove whitespace stripping to simplify <code>no_std</code> support. No out of the box configs use it, and it's trivial to do yourself if needed: <code>filter(|b| !b&quot; \n\t\r\x0b\x0c&quot;.contains(b)</code>.</li>
<li>Detect invalid trailing symbols when decoding and return an error rather than silently ignoring them.</li>
</ul>
<h1>0.9.3</h1>
<ul>
<li>Update safemem</li>
</ul>
<h1>0.9.2</h1>
<ul>
<li>Derive <code>Clone</code> for <code>DecodeError</code>.</li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/b4fc91325ec985e2a18e83e95a3c08eebd636af4"><code>b4fc913</code></a> v0.13.0</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/bba4c5d11e3b5f0974d7d126342e0ca67bb4cc1f"><code>bba4c5d</code></a> Merge pull request <a href="https://github.com/marshallpierce/rust-base64/issues/145">#145</a> from marshallpierce/mp/cleanup</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/42967320b3c675c8ae5d3d46dd6e6aea4a7fc970"><code>4296732</code></a> Add docs and other cleanup</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/6bb35566333bd4c2cc74b8b820af5bfcda12a1b8"><code>6bb3556</code></a> Merge pull request <a href="https://github.com/marshallpierce/rust-base64/issues/144">#144</a> from untitaker/invalid-bytes-not-length</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/5b40e0c04ed522d10cfeaae0f0b343dd3e6f1d4d"><code>5b40e0c</code></a> Merge pull request <a href="https://github.com/marshallpierce/rust-base64/issues/142">#142</a> from marshallpierce/mp/string-writer</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/8b1ae22babb33a5c7cee84f0445434bff6880c76"><code>8b1ae22</code></a> Rename StrWrite to StrConsumer</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/27ccb6591e22dbe81840268feec0917feaaebd97"><code>27ccb65</code></a> fix tests</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/d15cd384e1a91fa73a7c57f30d4b76df2149df68"><code>d15cd38</code></a> Give better error messages when decoding data with trailing newlines</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/5a56885c655deb54ccb206165e8351b0476c78b8"><code>5a56885</code></a> Introduce StrWriter to allow ESW to wrap both a String and a &amp;mut String</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/2dc0296d2a0c01387f6361c4c710f7d0c9e9aee2"><code>2dc0296</code></a> Merge pull request <a href="https://github.com/marshallpierce/rust-base64/issues/143">#143</a> from marshallpierce/mp/invalid-length-doc</li>
<li>Additional commits viewable in <a href="https://github.com/marshallpierce/rust-base64/compare/v0.12.3...v0.13.0">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=base64&package-manager=cargo&previous-version=0.12.3&new-version=0.13.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
abraham-nixon added a commit to abraham-nixon/xmr-btc-swap that referenced this pull request Feb 15, 2022
332: Bump base64 from 0.12.3 to 0.13.0 r=thomaseizinger a=dependabot[bot]

Bumps [base64](https://github.com/marshallpierce/rust-base64) from 0.12.3 to 0.13.0.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/marshallpierce/rust-base64/blob/master/RELEASE-NOTES.md">base64's changelog</a>.</em></p>
<blockquote>
<h1>0.13.0</h1>
<ul>
<li>Config methods are const</li>
<li>Added <code>EncoderStringWriter</code> to allow encoding directly to a String</li>
<li><code>EncoderWriter</code> now owns its delegate writer rather than keeping a reference to it (though refs still work)
<ul>
<li>As a consequence, it is now possible to extract the delegate writer from an <code>EncoderWriter</code> via <code>finish()</code>, which returns <code>Result&lt;W&gt;</code> instead of <code>Result&lt;()&gt;</code>. If you were calling <code>finish()</code> explicitly, you will now need to use <code>let _ = foo.finish()</code> instead of just <code>foo.finish()</code> to avoid a warning about the unused value.</li>
</ul>
</li>
<li>When decoding input that has both an invalid length and an invalid symbol as the last byte, <code>InvalidByte</code> will be emitted instead of <code>InvalidLength</code> to make the problem more obvious.</li>
</ul>
<h1>0.12.2</h1>
<ul>
<li>Add <code>BinHex</code> alphabet</li>
</ul>
<h1>0.12.1</h1>
<ul>
<li>Add <code>Bcrypt</code> alphabet</li>
</ul>
<h1>0.12.0</h1>
<ul>
<li>A <code>Read</code> implementation (<code>DecoderReader</code>) to let users transparently decoded data from a b64 input source</li>
<li>IMAP's modified b64 alphabet</li>
<li>Relaxed type restrictions to just <code>AsRef&lt;[ut8]&gt;</code> for main <code>encode*</code>/<code>decode*</code> functions</li>
<li>A minor performance improvement in encoding</li>
</ul>
<h1>0.11.0</h1>
<ul>
<li>Minimum rust version 1.34.0</li>
<li><code>no_std</code> is now supported via the two new features <code>alloc</code> and <code>std</code>.</li>
</ul>
<h1>0.10.1</h1>
<ul>
<li>Minimum rust version 1.27.2</li>
<li>Fix bug in streaming encoding (<a href="https://github.com/marshallpierce/rust-base64/pull/90">#90</a>): if the underlying writer didn't write all the bytes given to it, the remaining bytes would not be retried later. See the docs on <code>EncoderWriter::write</code>.</li>
<li>Make it configurable whether or not to return an error when decoding detects excess trailing bits.</li>
</ul>
<h1>0.10.0</h1>
<ul>
<li>Remove line wrapping. Line wrapping was never a great conceptual fit in this library, and other features (streaming encoding, etc) either couldn't support it or could support only special cases of it with a great increase in complexity. Line wrapping has been pulled out into a <a href="https://crates.io/crates/line-wrap">line-wrap</a> crate, so it's still available if you need it.
<ul>
<li><code>Base64Display</code> creation no longer uses a <code>Result</code> because it can't fail, which means its helper methods for common
configs that <code>unwrap()</code> for you are no longer needed</li>
</ul>
</li>
<li>Add a streaming encoder <code>Write</code> impl to transparently base64 as you write.</li>
<li>Remove the remaining <code>unsafe</code> code.</li>
<li>Remove whitespace stripping to simplify <code>no_std</code> support. No out of the box configs use it, and it's trivial to do yourself if needed: <code>filter(|b| !b&quot; \n\t\r\x0b\x0c&quot;.contains(b)</code>.</li>
<li>Detect invalid trailing symbols when decoding and return an error rather than silently ignoring them.</li>
</ul>
<h1>0.9.3</h1>
<ul>
<li>Update safemem</li>
</ul>
<h1>0.9.2</h1>
<ul>
<li>Derive <code>Clone</code> for <code>DecodeError</code>.</li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/b4fc91325ec985e2a18e83e95a3c08eebd636af4"><code>b4fc913</code></a> v0.13.0</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/bba4c5d11e3b5f0974d7d126342e0ca67bb4cc1f"><code>bba4c5d</code></a> Merge pull request <a href="https://github.com/marshallpierce/rust-base64/issues/145">#145</a> from marshallpierce/mp/cleanup</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/42967320b3c675c8ae5d3d46dd6e6aea4a7fc970"><code>4296732</code></a> Add docs and other cleanup</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/6bb35566333bd4c2cc74b8b820af5bfcda12a1b8"><code>6bb3556</code></a> Merge pull request <a href="https://github.com/marshallpierce/rust-base64/issues/144">#144</a> from untitaker/invalid-bytes-not-length</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/5b40e0c04ed522d10cfeaae0f0b343dd3e6f1d4d"><code>5b40e0c</code></a> Merge pull request <a href="https://github.com/marshallpierce/rust-base64/issues/142">#142</a> from marshallpierce/mp/string-writer</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/8b1ae22babb33a5c7cee84f0445434bff6880c76"><code>8b1ae22</code></a> Rename StrWrite to StrConsumer</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/27ccb6591e22dbe81840268feec0917feaaebd97"><code>27ccb65</code></a> fix tests</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/d15cd384e1a91fa73a7c57f30d4b76df2149df68"><code>d15cd38</code></a> Give better error messages when decoding data with trailing newlines</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/5a56885c655deb54ccb206165e8351b0476c78b8"><code>5a56885</code></a> Introduce StrWriter to allow ESW to wrap both a String and a &amp;mut String</li>
<li><a href="https://github.com/marshallpierce/rust-base64/commit/2dc0296d2a0c01387f6361c4c710f7d0c9e9aee2"><code>2dc0296</code></a> Merge pull request <a href="https://github.com/marshallpierce/rust-base64/issues/143">#143</a> from marshallpierce/mp/invalid-length-doc</li>
<li>Additional commits viewable in <a href="https://github.com/marshallpierce/rust-base64/compare/v0.12.3...v0.13.0">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=base64&package-manager=cargo&previous-version=0.12.3&new-version=0.13.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

None yet

3 participants