Skip to content

Commit 8b1ae22

Browse files
Rename StrWrite to StrConsumer
It really wasn't very much like io::Write, so best to avoid the misleading name
1 parent 5a56885 commit 8b1ae22

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/write/encoder_string_writer.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ use super::encoder::EncoderWriter;
4949
///
5050
/// Because it has to validate that the base64 is UTF-8, it is about 80% as fast as writing plain
5151
/// bytes to a `io::Write`.
52-
pub struct EncoderStringWriter<S: StrWrite> {
52+
pub struct EncoderStringWriter<S: StrConsumer> {
5353
encoder: EncoderWriter<Utf8SingleCodeUnitWriter<S>>,
5454
}
5555

56-
impl<S: StrWrite> EncoderStringWriter<S> {
57-
/// Create a EncoderStringWriter that will append to the provided `StrWrite`.
58-
pub fn from(str_writer: S, config: Config) -> Self {
59-
EncoderStringWriter { encoder: EncoderWriter::new(Utf8SingleCodeUnitWriter { str_writer }, config) }
56+
impl<S: StrConsumer> EncoderStringWriter<S> {
57+
/// Create a EncoderStringWriter that will append to the provided `StrConsumer`.
58+
pub fn from(str_consumer: S, config: Config) -> Self {
59+
EncoderStringWriter { encoder: EncoderWriter::new(Utf8SingleCodeUnitWriter { str_consumer }, config) }
6060
}
6161

6262
/// Encode all remaining buffered data, including any trailing incomplete input triples and
@@ -68,7 +68,7 @@ impl<S: StrWrite> EncoderStringWriter<S> {
6868
pub fn into_inner(mut self) -> S {
6969
self.encoder.finish()
7070
.expect("Writing to a Vec<u8> should never fail")
71-
.str_writer
71+
.str_consumer
7272
}
7373
}
7474

@@ -79,7 +79,7 @@ impl EncoderStringWriter<String> {
7979
}
8080
}
8181

82-
impl <S: StrWrite> Write for EncoderStringWriter<S> {
82+
impl <S: StrConsumer> Write for EncoderStringWriter<S> {
8383
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
8484
self.encoder.write(buf)
8585
}
@@ -89,40 +89,41 @@ impl <S: StrWrite> Write for EncoderStringWriter<S> {
8989
}
9090
}
9191

92-
/// An abstraction around infallible writes of `str`s.
93-
///
94-
/// Typically, this will just be String.
95-
pub trait StrWrite {
96-
/// The write must succeed, and must write the entire `buf`.
97-
fn write(&mut self, buf: &str);
92+
/// An abstraction around consuming `str`s produced by base64 encoding.
93+
pub trait StrConsumer {
94+
/// Consume the base64 encoded data in `buf`
95+
fn consume(&mut self, buf: &str);
9896
}
9997

100-
/// As for io::Write, StrWrite is implemented automatically for `&mut S`.
101-
impl<S: StrWrite + ?Sized> StrWrite for &mut S {
102-
fn write(&mut self, buf: &str) {
103-
(**self).write(buf)
98+
/// As for io::Write, `StrConsumer` is implemented automatically for `&mut S`.
99+
impl<S: StrConsumer + ?Sized> StrConsumer for &mut S {
100+
fn consume(&mut self, buf: &str) {
101+
(**self).consume(buf)
104102
}
105103
}
106104

107-
impl StrWrite for String {
108-
fn write(&mut self, buf: &str) {
105+
/// Pushes the str onto the end of the String
106+
impl StrConsumer for String {
107+
fn consume(&mut self, buf: &str) {
109108
self.push_str(buf)
110109
}
111110
}
112111

113112
/// A `Write` that only can handle bytes that are valid single-byte UTF-8 code units.
114113
///
115114
/// This is safe because we only use it when writing base64, which is always valid UTF-8.
116-
struct Utf8SingleCodeUnitWriter<S: StrWrite> {
117-
str_writer: S
115+
struct Utf8SingleCodeUnitWriter<S: StrConsumer> {
116+
str_consumer: S
118117
}
119118

120-
impl<S: StrWrite> io::Write for Utf8SingleCodeUnitWriter<S> {
119+
impl<S: StrConsumer> io::Write for Utf8SingleCodeUnitWriter<S> {
121120
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
121+
// Because we expect all input to be valid utf-8 individual bytes, we can encode any buffer
122+
// length
122123
let s = std::str::from_utf8(buf)
123124
.expect("Input must be valid UTF-8");
124125

125-
self.str_writer.write(s);
126+
self.str_consumer.consume(s);
126127

127128
Ok(buf.len())
128129
}

0 commit comments

Comments
 (0)