Why does an object passed to a trait object method argument must outlive the trait object itself?

I recalled from elsewhere that it does work,[1] so I also poked at this until I figured it out. You can use precise capturing but the errors aren't great. Applying this change fixes that older playground:

 impl<'a, 'i, N: NodeView + 'i> Selector<'i, N> for TypeSelector<'a> {
     type Err = N::Err;
-    type Iter = impl Iterator<Item = Result<N, Self::Err>> + use<N>;
+    type Iter = impl Iterator<Item = Result<N, N::Err>> + use<N>;

Because otherwise you have a

impl Iterator<Item = Result<N, <TypeSelector<'a> as Selector<'i, N>>::Err>>
//                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

which apparently isn't normalized, so it captures all the generics.

So you can probably get rid of some TAITs.


  1. to at least some degree â†Šī¸Ž