Skip to content

Commit cc67a84

Browse files
committed
refactor(reader): Remove allocations from Display
1 parent 9f7b2ba commit cc67a84

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

src/reader.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,22 +217,23 @@ pub enum ReadUntil {
217217

218218
impl fmt::Display for ReadUntil {
219219
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220-
let printable = match self {
221-
ReadUntil::String(s) if s == "\n" => "\\n (newline)".to_owned(),
222-
ReadUntil::String(s) if s == "\r" => "\\r (carriage return)".to_owned(),
223-
ReadUntil::String(s) => format!("\"{s}\""),
224-
ReadUntil::Regex(r) => format!("Regex: \"{r}\""),
225-
ReadUntil::NBytes(n) => format!("reading {n} bytes"),
226-
ReadUntil::EOF => "EOF (End of File)".to_owned(),
220+
match self {
221+
ReadUntil::String(s) if s == "\n" => write!(f, "\\n (newline)"),
222+
ReadUntil::String(s) if s == "\r" => write!(f, "\\r (carriage return)"),
223+
ReadUntil::String(s) => write!(f, "\"{s}\""),
224+
ReadUntil::Regex(r) => write!(f, "Regex: \"{r}\""),
225+
ReadUntil::NBytes(n) => write!(f, "reading {n} bytes"),
226+
ReadUntil::EOF => write!(f, "EOF (End of File)"),
227227
ReadUntil::Any(v) => {
228-
let mut res = Vec::new();
229-
for r in v {
230-
res.push(r.to_string());
228+
for (i, r) in v.iter().enumerate() {
229+
if i != 0 {
230+
write!(f, ", ")?;
231+
}
232+
write!(f, "{r}")?;
231233
}
232-
res.join(", ")
234+
Ok(())
233235
}
234-
};
235-
write!(f, "{printable}")
236+
}
236237
}
237238
}
238239

0 commit comments

Comments
 (0)