From 3ec85d54588ca93c5182f72cece76f37cce500ae Mon Sep 17 00:00:00 2001 From: Thomas Hurst Date: Fri, 13 Sep 2019 13:39:32 +0100 Subject: [PATCH] for_each: have the closure return an io::Result --- src/lib.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index edae58a..fc4fcd4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,22 +120,32 @@ impl LineReader { } } - /// Run the given closure for each line while the closure returns true. + /// Run the given closure for each line while while the closure returns `Ok(true)`. + /// + /// If either the reader or the closure return an error, iteration ends and the error is returned. /// /// ```no_run /// # use linereader::LineReader; /// # use std::fs::File; /// # use std::io; - /// # fn solve(x: &[u8]) { } /// # fn x() -> io::Result<()> { - /// # let mut reader = LineReader::new(File::open("myfile.txt")?); - /// reader.for_each(|line| { if line.len() == 42 { solve(line); true } else { false } })?; + /// let buf: &[u8] = b"foo\nbar\nbaz"; + /// let mut reader = LineReader::new(buf); + /// let mut lines = vec![]; + /// reader.for_each(|line| { + /// lines.push(line.to_vec()); + /// Ok(true) + /// })?; + /// assert_eq!(lines.len(), 3); + /// assert_eq!(lines[0], b"foo\n"); + /// assert_eq!(lines[1], b"bar\n"); + /// assert_eq!(lines[2], b"baz"); /// # Ok(()) /// # } /// ``` - pub fn for_each bool>(&mut self, mut f: F) -> io::Result<()> { + pub fn for_each io::Result>(&mut self, mut f: F) -> io::Result<()> { while let Some(line) = self.next_line() { - if !f(line?) { + if !f(line?)? { break; } } @@ -343,12 +353,12 @@ mod tests { let mut reader = LineReader::new(buf); let mut len = 2; - reader.for_each(|l| { assert_eq!(len, l.len()); len += 1; true }).unwrap(); + reader.for_each(|l| { assert_eq!(len, l.len()); len += 1; Ok(true) }).unwrap(); let buf: &[u8] = b"f\nba\nbaz\n"; let mut reader = LineReader::new(buf); - reader.for_each(|l| { assert_eq!(l.len(), 2); false }).unwrap(); + reader.for_each(|l| { assert_eq!(l.len(), 2); Ok(false) }).unwrap(); } extern crate rand;