From: Scott Worley Date: Thu, 29 Aug 2024 09:34:41 +0000 (-0700) Subject: Move the trim_end() down X-Git-Tag: v0.3.0~18 X-Git-Url: http://git.scottworley.com/tablify/commitdiff_plain/8bf0d5b1daaf4841b1c593630d3c862e72985dda?ds=sidebyside Move the trim_end() down We don't want to covert the String to a &str prematurely. --- diff --git a/src/lib.rs b/src/lib.rs index 199b8ab..7292f02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -117,18 +117,13 @@ impl<'a, Input: Iterator>> Iterator for Re type Item = Result, std::io::Error>; fn next(&mut self) -> Option { loop { - match self - .input - .next() - // TODO: Don't leak - .map(|(n, r)| (n, r.map(|line| String::from(line).leak().trim_end()))) - { + match self.input.next() { None => return Ok(std::mem::take(&mut self.row)).transpose(), Some((_, Err(e))) => return Some(Err(e)), - Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => { + Some((_, Ok(line))) if line.trim_end().is_empty() && self.row.is_some() => { return Ok(std::mem::take(&mut self.row)).transpose() } - Some((_, Ok(line))) if line.is_empty() => {} + Some((_, Ok(line))) if line.trim_end().is_empty() => {} Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row { None => { return Some(Err(std::io::Error::other(format!( @@ -136,12 +131,14 @@ impl<'a, Input: Iterator>> Iterator for Re n + 1 )))) } - Some(ref mut row) => row.entries.push(Entry::from(line.trim())), + // TODO: Don't leak + Some(ref mut row) => row.entries.push(Entry::from(line.leak().trim())), }, Some((_, Ok(line))) => { let prev = std::mem::take(&mut self.row); self.row = Some(RowInput { - label: line, + // TODO: Don't leak + label: line.leak().trim_end(), entries: vec![], }); if prev.is_some() {