Hello,
I want to provide a blankt (or blanket-like?) implementation to enable
usage of for
loops based on iter()
/iter_mut()
for a trait I
defined that consumes a type parameter T
. For the sake of
example, I demand those functions in a trait called
MyCollectionLikeTrait<T>
, and implement them for MyStruct<T>
.
From what I understand, I have to implement IntoIterator
for
&MyStruct<T>
and &mut MyStruct<T>
, and from there refer to the
iter()
/iter_mut()
implementations.
I feel like rustc pushes me to type that out for every implementation
of MyCollectionLikeTrait<T>
. This seems like boilerplate. So, hence
my question:
Is there a way to generalize the implementation of IntoIterator
for
&MyStruct
into a blanket implementation associated to the trait?
I.e. sth. like impl<U: MyCollectionLikeTrait<T>> IntoIterator for &U {...}
?
This is the snippet I am trying to generalize, i.e. remove any mention of MyStruct
:
(Rust Playground for context)
impl<'a, T> IntoIterator for &'a MyStruct<T> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, T> IntoIterator for &'a mut MyStruct<T> {
type Item = &'a mut T;
type IntoIter = slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
Thanks!