Skip to content

Commit

Permalink
Bubble up I/O errors in encode_stream_ascii
Browse files Browse the repository at this point in the history
  • Loading branch information
qsantos committed Jun 19, 2024
1 parent 3185ae7 commit e44764a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn ascii_benchmark(c: &mut Criterion) {
group.bench_function("stream", |b| {
b.iter(|| {
f.rewind().unwrap();
encode_stream_ascii(&mut f, &mut devnull);
encode_stream_ascii(&mut f, &mut devnull).unwrap();
})
});

Expand Down
12 changes: 8 additions & 4 deletions src/encode_ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,30 @@ pub fn encode_string_ascii(input: &[u8]) -> String {
/// ripmors::encode_stream_ascii(&mut stdin, &mut stdout);
/// }
/// ```
pub fn encode_stream_ascii(input: &mut impl Read, output: &mut impl Write) {
pub fn encode_stream_ascii(
input: &mut impl Read,
output: &mut impl Write,
) -> Result<(), std::io::Error> {
let mut input_buf = vec![0u8; 1 << 15];
let mut output_buf = Vec::new();
loop {
let bytes_read = input.read(&mut input_buf).unwrap();
let bytes_read = input.read(&mut input_buf)?;
if bytes_read == 0 {
break;
}
encode_buffer_ascii(&input_buf[..bytes_read], &mut output_buf);
if output_buf.is_empty() {
} else if output_buf.last() == Some(&b' ') {
output_buf.pop();
output.write_all(&output_buf).unwrap();
output.write_all(&output_buf)?;
output_buf.clear();
output_buf.push(b' ');
} else {
output.write_all(&output_buf).unwrap();
output.write_all(&output_buf)?;
output_buf.clear();
}
}
Ok(())
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() {
};
decode_stream(&mut stdin, &mut stdout, char_decode);
} else if args.encode == Some(EncodeVariant::Ascii) {
encode_stream_ascii(&mut stdin, &mut stdout);
encode_stream_ascii(&mut stdin, &mut stdout).unwrap();
} else {
encode_stream(&mut stdin, &mut stdout);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn compare_output_to_oracle(writer: BufWriter<Vec<u8>>, expected_filename: &str)
fn test_encode_stream_ascii() {
let mut f = std::fs::File::open("1-original.txt").unwrap();
let mut writer = BufWriter::new(Vec::new());
encode_stream_ascii(&mut f, &mut writer);
encode_stream_ascii(&mut f, &mut writer).unwrap();
compare_output_to_oracle(writer, "2-encoded.txt");
}

Expand Down

0 comments on commit e44764a

Please sign in to comment.