diff options
author | Teddy Wing | 2021-06-13 14:53:47 +0200 |
---|---|---|
committer | Teddy Wing | 2021-06-13 14:53:47 +0200 |
commit | bdb52392162500ed1b55e68527668c3b41386c2a (patch) | |
tree | ff721db6ba7036441351b8dc7da4424e202854ea /src | |
parent | 8c247b5c2881e91a54622eb90dad8ca777db592e (diff) | |
download | reflectub-bdb52392162500ed1b55e68527668c3b41386c2a.tar.bz2 |
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<Target = [(K, V)]> and just treat your Bucket<K, V>
> 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<T> 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/)
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 12 | ||||
-rw-r--r-- | src/multi_error.rs | 26 |
2 files changed, 37 insertions, 1 deletions
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<Vec<anyhow::Error>> for MultiError { MultiError { errors: errors } } } + +// impl Iterator for MultiError { +// type Item = anyhow::Error; +// +// fn next(&mut self) -> Option<Self::Item> { +// self.errors.next() +// } +// } + +impl IntoIterator for MultiError { + type Item = anyhow::Error; + type IntoIter = std::vec::IntoIter<Self::Item>; + + 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 +// } +// } |