Skip to content

Commit 24ca190

Browse files
ESW into_inner cannot fail, so no need for Result
Other tidying as well
1 parent acf7518 commit 24ca190

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- Config methods are const
44
- Added `EncoderStringWriter` to allow encoding directly to a String
55
- `EncoderWriter` now owns its delegate writer rather than keeping a reference to it (though refs still work)
6-
- As a consequence, it is now possible to extract the delegate writer from an `EncoderWriter` via `finish()`
6+
- As a consequence, it is now possible to extract the delegate writer from an `EncoderWriter` via `finish()`, which returns `Result<W>` instead of `Result<()>`.
77

88
# 0.12.2
99

benches/benchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn do_encode_bench_string_stream(b: &mut Bencher, &size: &usize) {
131131
let mut stream_enc = write::EncoderStringWriter::new(TEST_CONFIG);
132132
stream_enc.write_all(&v).unwrap();
133133
stream_enc.flush().unwrap();
134-
let _ = stream_enc.finish().unwrap();
134+
let _ = stream_enc.into_inner();
135135
});
136136
}
137137

src/write/encoder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ impl<W: Write> Write for EncoderWriter<W> {
359359

360360
/// Because this is usually treated as OK to call multiple times, it will *not* flush any
361361
/// incomplete chunks of input or write padding.
362+
/// # Errors
363+
///
364+
/// The first error that is not of [`ErrorKind::Interrupted`] will be returned.
362365
fn flush(&mut self) -> Result<()> {
363366
self.write_all_encoded_output()?;
364367
self.delegate

src/write/encoder_string_writer.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::Write;
44
use super::encoder::EncoderWriter;
55

66
/// A `Write` implementation that base64-encodes data using the provided config and accumulates the
7-
/// resulting base64 in memory, which is then exposed as a String via `finish()`.
7+
/// resulting base64 in memory, which is then exposed as a String via `into_inner()`.
88
///
99
/// # Examples
1010
///
@@ -16,7 +16,7 @@ use super::encoder::EncoderWriter;
1616
/// enc.write_all(b"asdf").unwrap();
1717
///
1818
/// // get the resulting String
19-
/// let b64_string = enc.finish().unwrap();
19+
/// let b64_string = enc.into_inner();
2020
///
2121
/// assert_eq!("YXNkZg==", &b64_string);
2222
/// ```
@@ -36,7 +36,12 @@ pub struct EncoderStringWriter {
3636
impl EncoderStringWriter {
3737
/// Create a new EncoderStringWriter that will encode with the provided config.
3838
pub fn new(config: Config) -> EncoderStringWriter {
39-
EncoderStringWriter { encoder: EncoderWriter::new(Vec::new(), config) }
39+
EncoderStringWriter::from(String::new(), config)
40+
}
41+
42+
/// Create a new EncoderStringWriter that will append to the provided string.
43+
pub fn from(s: String, config: Config) -> EncoderStringWriter {
44+
EncoderStringWriter { encoder: EncoderWriter::new(s.into_bytes(), config) }
4045
}
4146

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

55-
let str = String::from_utf8(buf).expect("Base64 should always be valid UTF-8");
56-
Ok(str)
57+
String::from_utf8(buf).expect("Base64 should always be valid UTF-8")
5758
}
5859
}
5960

@@ -99,7 +100,7 @@ mod tests {
99100
stream_encoder.write_all(&orig_data[0..i]).unwrap();
100101
stream_encoder.write_all(&orig_data[i..]).unwrap();
101102

102-
let stream_encoded = stream_encoder.finish().unwrap();
103+
let stream_encoded = stream_encoder.into_inner();
103104

104105
assert_eq!(normal_encoded, stream_encoded);
105106
}

0 commit comments

Comments
 (0)