pub trait StringWriter {
// Required methods
fn write_char(&mut self, c: char);
fn write_str(&mut self, s: &str);
// Provided methods
fn writer_hint(&mut self, _expectedlen: usize) { ... }
fn as_mut_string(&mut self) -> Option<&mut String> { ... }
}
Expand description
String writer used by decoders. In most cases this will be an owned string.
Required Methods§
Sourcefn write_char(&mut self, c: char)
fn write_char(&mut self, c: char)
Writes a single character.
Provided Methods§
Sourcefn writer_hint(&mut self, _expectedlen: usize)
fn writer_hint(&mut self, _expectedlen: usize)
Hints an expected lower bound on the length (in bytes) of the output
until the next call to writer_hint
,
so that the writer can reserve the memory for writing.
RawDecoder
s are recommended but not required to call this method
with an appropriate estimate.
By default this method does nothing.
Examples found in repository?
examples/rot13.rs (line 91)
86 fn raw_feed(
87 &mut self,
88 input: &[u8],
89 output: &mut dyn StringWriter,
90 ) -> (usize, Option<CodecError>) {
91 output.writer_hint(input.len());
92 let string = match from_utf8(input) {
93 Ok(s) => s,
94 Err(_e) => {
95 return (
96 0,
97 Some(CodecError {
98 upto: 0, // Utf8Error::valid_up_to is unstable at the time of writing,
99 // therefore we can’t quite do partial parsing yet
100 cause: "input is not valid utf-8".into(),
101 }),
102 );
103 }
104 };
105 for ch in string.chars() {
106 match ch {
107 'a'..='z' | 'A'..='Z' => output.write_char(rotate_byte(ch as u8) as char),
108 _ => output.write_char(ch),
109 }
110 }
111 (input.len(), None)
112 }
Sourcefn as_mut_string(&mut self) -> Option<&mut String>
fn as_mut_string(&mut self) -> Option<&mut String>
If this StringWriter
is a String
, returns a mutable reference to
self
as Some(&mut String)
. Returns None
otherwise.