Implement IntoIter based on iter()/iter_mut()

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!

No, such a blanket implementation is not possible. If a downstream crate implements MyCollectionLikeTrait for its own type, then your crate would hold an impl of IntoIterator for that downstream type, but you own neither the trait nor the type, so the impl is not allowed by the orphan rules.

You can add where bounds that require that the appropriate IntoIterator implementations exist:

pub trait MyCollectionLikeTrait<T>
where
    Self: iter::IntoIterator,
    for<'a> &'a Self: iter::IntoIterator,
    for<'a> &'a mut Self: iter::IntoIterator,
{
    fn iter(&self) -> slice::Iter<T>;
    fn iter_mut(&mut self) -> slice::IterMut<T>;
}

but each struct still has to provide all three implementations.

2 Likes

That makes thanks to me, thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.