From bdb52392162500ed1b55e68527668c3b41386c2a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 13 Jun 2021 14:53:47 +0200 Subject: main(): Print "error: " in front of each error line Prefix each error line with the text "error: " to make it clear that's what it is, and that it's separate from errors printed on other lines. Worked out how to set up an `Iterator` for `MultiError` based on a comment by 'chris-morgan' (https://old.reddit.com/user/chris-morgan) on Reddit /r/rust: > 1. Implement your own iterator type which wraps existing iterator > types (std::slice::Iter, and std::vec::IntoIter if you want a > consuming iterator). > Advantages: most flexible, ensures API stability if you > need to change internal details. > Disadvantages: a lot more effort, if you want to do it properly > (which involves implementing about ten traits on your iterator > wrapper type); and if slices or their iterators add something new, > you don’t get it unless you implement a wrapper yourself. > > 2. Have your iter() functions and IntoIterator implementations use the > standard iterator types directly. > Advantages: easier, gets you all the other trait implementations on > std::slice::Iter for free—AsRef, Clone, FusedIterator, > ExactSizeIterator, Debug, Send, DoubleEndedIterator, TrustedLen, > Sync). > Disadvantages: if you need to restructure things so that this is no > longer an option (e.g. store things in a different type of vector > and thus need to map it before presenting it to the user) it’s a > breaking change. > > 3. Implement Deref and just treat your Bucket > as a &[(K, V)]. (Read-only; implement DerefMut if you want to allow > mutations of values.) > Advantages: easy, and lets you simply treat the whole thing as a > slice (this is what Vec does). > Disadvantages: there really aren’t any, if it matches your purpose. > (If not, it’s useless.) (https://old.reddit.com/r/rust/comments/7a0slp/questionimplementing_iterator_for_a_struct_with_a/) --- src/main.rs | 12 +++++++++++- src/multi_error.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 792b45a..9117674 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,17 @@ fn main() { match run() { Ok(_) => (), Err(e) => { - eprintln!("error: {}", e); + // format!("{}", e) + // .lines() + // .for_each(|l| eprintln!("error: {}", e)); + // eprintln!(); + + // let errors = *e; + // errors.for_each(|e| eprintln!("error: {}", e)); + + e + .into_iter() + .for_each(|e| eprintln!("error: {:#}", e)); process::exit(exitcode::SOFTWARE); }, diff --git a/src/multi_error.rs b/src/multi_error.rs index 6163f40..d3fdba5 100644 --- a/src/multi_error.rs +++ b/src/multi_error.rs @@ -17,6 +17,7 @@ use std::fmt; +// use std::ops::Deref; /// Wraps a list of errors. @@ -50,3 +51,28 @@ impl From> for MultiError { MultiError { errors: errors } } } + +// impl Iterator for MultiError { +// type Item = anyhow::Error; +// +// fn next(&mut self) -> Option { +// self.errors.next() +// } +// } + +impl IntoIterator for MultiError { + type Item = anyhow::Error; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.errors.into_iter() + } +} + +// impl Deref for MultiError { +// type Target = [anyhow::Error]; +// +// fn deref(&self) -> &Self::Target { +// &self.errors +// } +// } -- cgit v1.2.3