From: Ian Jackson Date: Tue, 1 Jun 2021 19:57:49 +0000 (+0100) Subject: utils: Introduce anyhow::Error::for_each X-Git-Tag: otter-0.7.0~132 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=672ee029570deed8d7d320e0b78338e27cb77577;p=otter.git utils: Introduce anyhow::Error::for_each Signed-off-by: Ian Jackson --- diff --git a/src/utils.rs b/src/utils.rs index 9ce62a28..7a9f5e27 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -643,9 +643,19 @@ impl IndexVec where I: index_vec::Idx { } } - #[ext(pub)] impl anyhow::Error { + fn for_each(&self, f: &mut dyn FnMut(&str) -> fmt::Result) -> fmt::Result { + let mut done = String::new(); + for e in self.chain() { + let s = e.to_string(); + if done.contains(&s) { continue } + f(&s)?; + done = s; + } + Ok(()) + } + fn end_process(self, estatus: u8) -> ! { #[derive(Default,Debug)] struct Sol { any: bool } impl Sol { @@ -659,17 +669,14 @@ impl anyhow::Error { } } let mut sol: Sol = default(); - let mut printed = String::new(); - for e in self.chain() { - let s = e.to_string(); - if printed.contains(&s) { continue } + self.for_each(&mut |s|{ let long = s.len() > 80; if long && sol.any { sol.nl() } sol.head(); eprint!(": {}", &s); if long { sol.nl() } - printed = s; - } + Ok::<_,fmt::Error>(()) + }).unwrap(); sol.nl(); assert!(estatus > 0); exit(estatus.into());