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/multi_error.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/multi_error.rs') 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