Thanks for sharing this! I was trying to do precisely this type of thing to abstract over mutable/non-mutable references to resolve my initial question in DRYing nearly identical implementations for &T and &mut T and gave up. Gankro's trick of separating out a LifetimeToType<'a>
trait (effectively, separating out parametrization by T
and by 'a
into orthogonal entities) was the trick I failed to figure out (why that's needed is better seen by trying to implement it oneself). Nice!
Edit: Except, disappointingly, he still had 3 implementations — one for each Iter
type, whereas, I was trying to have exactly one for &T
and &mut T
that was parametrized over the Ref
type. This requires additional abstraction over, say, as_ref
for the former and as_mut
for the latter. But Gankro is demonstrating allowing other types of reference types with the same iterator trait — still very neat!