Help using a collection trait for a trait with an associated type implemented on tuples

Here is a link to the playground that demonstrates my problem; C<(&str, &str)> doesn't implement Foo. The error message is really confusing as well. I have no idea how to solve this.

Well, I have some luck with impl Foo on &C(T) instead of owned C(T). The lifetimes are bound by reference lifetime, instead of higer-ranked "forall". Perhaps more experienced rustaceans can crack this down on owned.

unfortunately I need it to be implemented for the owned C<T> in my case.

I think this is impossible as written/desired for the same reasons discussed in issue 32330. That is,

  • Your impl for Collection is on &'a (A, B) and not for<'a> &'a (A, B)
    • Necessary since you want to output a reference, analogous to the issue above
  • But your trait bound for C<T> is on for<'a> &'a T: Collection
    • Because you want to implement for the owned C<T>, which has no lifetime to bind

So the reason C<(&str, &str)> does not impl Foo is because the higher-ranked type for<'a> &'a(&str, &str) does not (and cannot) impl Collection (even though the bound lifetime type &'a (&str, &str) does impl Collection).

I'm hoping someone else here can confirm this. Unfortunately the documentation around higher-ranked trait bounds (HRTB) and higher-rank types also seems to be incomplete, non-existent, or embedded in github issues which largely assume contextual knowledge. But here are a couple more things I found while poking at this in case they are helpful:

  • Issue 57362 improved a related error message
  • Issue 56105 discusses some (shifting) interactions involving &T and for<'a> &'a T
1 Like

Thanks for the response. I'll try to find a workaround.

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.