I'm writing an extension trait on Iterator
to show progress on the console:
for item in long_iterator.with_progress("Aligning dilithium matrix...") {
...
}
My goal is that if the Iterator
also implements ExactSizeIterator
, it will show a progress bar with a percentage; if not, it shows an indeterminate spinner instead.
I hoped I could just do this, and have the compiler figure out that the more "derived" implementation should take precedence, but alas:
pub struct ProgressBarIterator<I: Iterator> {
inner: I,
...
}
impl<I: Iterator> Iterator for ProgressIterator<I> {
...
}
pub trait WithProgress<I: Iterator> {
fn with_progress(self, message: &str) -> impl Iterator<Item = I::Item>;
}
impl<I: ExactSizeIterator> WithProgress<I> for I {
fn with_progress(self, message: &str) -> impl Iterator<Item = I::Item> {
ProgressBarIterator { ... }
}
}
impl<I: Iterator> WithProgress<I> for I {
fn with_progress(self, message: &str) -> impl Iterator<Item = I::Item> {
ProgressBarIterator { ... }
}
}
But alas, that's not how Rust works:
error[E0119]: conflicting implementations of trait `progress::WithProgress<_>`
Is there a way to make this work? Or am I best off with my current workaround of having two unrelated traits with differently named methods?