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 25, 2020
1 parent acf7518 commit 24ca190
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 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
2 changes: 1 addition & 1 deletion benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn do_encode_bench_string_stream(b: &mut Bencher, &size: &usize) {
let mut stream_enc = write::EncoderStringWriter::new(TEST_CONFIG);
stream_enc.write_all(&v).unwrap();
stream_enc.flush().unwrap();
let _ = stream_enc.finish().unwrap();
let _ = stream_enc.into_inner();
});
}

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 24ca190

Please sign in to comment.