//! Extends the BufReader Lines to support summing based on newlines. use std::io::BufRead; use std::io::Lines; pub struct Accumulator { lines: Lines, current: u32, } pub trait Elvish { fn elf(self) -> Accumulator; } impl Elvish for Lines { fn elf(self) -> Accumulator { Accumulator { lines: self, current: 0, } } } impl Iterator for Accumulator { type Item = u32; fn next(&mut self) -> Option { while let Some(Ok(line)) = self.lines.next() { let line = line.trim(); if line.is_empty() { let result = self.current; self.current = 0; return Some(result); } else { self.current += line.parse::().unwrap(); } } None } }