Skip to content

Commit

Permalink
ESW into_inner cannot fail, so no need for Result
Browse files Browse the repository at this point in the history
Other tidying as well
  • Loading branch information
marshallpierce committed Sep 24, 2020
1 parent acf7518 commit a047b53
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
- Config methods are const
- Added `EncoderStringWriter` to allow encoding directly to a String
- `EncoderWriter` now owns its delegate writer rather than keeping a reference to it (though refs still work)
- As a consequence, it is now possible to extract the delegate writer from an `EncoderWriter` via `finish()`
- As a consequence, it is now possible to extract the delegate writer from an `EncoderWriter` via `finish()`, which returns `Result<W>` instead of `Result<()>`.

# 0.12.2

Expand Down
3 changes: 3 additions & 0 deletions src/write/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ impl<W: Write> Write for EncoderWriter<W> {

/// Because this is usually treated as OK to call multiple times, it will *not* flush any
/// incomplete chunks of input or write padding.
/// # Errors
///
/// The first error that is not of [`ErrorKind::Interrupted`] will be returned.
fn flush(&mut self) -> Result<()> {
self.write_all_encoded_output()?;
self.delegate
Expand Down
25 changes: 13 additions & 12 deletions src/write/encoder_string_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io::Write;
use super::encoder::EncoderWriter;

/// A `Write` implementation that base64-encodes data using the provided config and accumulates the
/// resulting base64 in memory, which is then exposed as a String via `finish()`.
/// resulting base64 in memory, which is then exposed as a String via `into_inner()`.
///
/// # Examples
///
Expand All @@ -16,7 +16,7 @@ use super::encoder::EncoderWriter;
/// enc.write_all(b"asdf").unwrap();
///
/// // get the resulting String
/// let b64_string = enc.finish().unwrap();
/// let b64_string = enc.into_inner();
///
/// assert_eq!("YXNkZg==", &b64_string);
/// ```
Expand All @@ -36,7 +36,12 @@ pub struct EncoderStringWriter {
impl EncoderStringWriter {
/// Create a new EncoderStringWriter that will encode with the provided config.
pub fn new(config: Config) -> EncoderStringWriter {
EncoderStringWriter { encoder: EncoderWriter::new(Vec::new(), config) }
EncoderStringWriter::from(String::new(), config)
}

/// Create a new EncoderStringWriter that will append to the provided string.
pub fn from(s: String, config: Config) -> EncoderStringWriter {
EncoderStringWriter { encoder: EncoderWriter::new(s.into_bytes(), config) }
}

/// Encode all remaining buffered data, including any trailing incomplete input triples and
Expand All @@ -45,15 +50,11 @@ impl EncoderStringWriter {
/// Once this succeeds, no further writes or calls to this method are allowed.
///
/// Returns the base64-encoded form of the accumulated written data.
///
/// # Errors
///
/// The first error that is not of `ErrorKind::Interrupted` will be returned.
pub fn finish(&mut self) -> io::Result<String> {
let buf = self.encoder.finish()?;
pub fn into_inner(mut self) -> String {
let buf = self.encoder.finish()
.expect("Writing to a Vec<u8> should never fail");

let str = String::from_utf8(buf).expect("Base64 should always be valid UTF-8");
Ok(str)
String::from_utf8(buf).expect("Base64 should always be valid UTF-8")
}
}

Expand Down Expand Up @@ -99,7 +100,7 @@ mod tests {
stream_encoder.write_all(&orig_data[0..i]).unwrap();
stream_encoder.write_all(&orig_data[i..]).unwrap();

let stream_encoded = stream_encoder.finish().unwrap();
let stream_encoded = stream_encoder.into_inner();

assert_eq!(normal_encoded, stream_encoded);
}
Expand Down

0 comments on commit a047b53

Please sign in to comment.