3 ms·
So I have a question about GATs: since there was a lot of concern about the complexity cost they add, was it not possible to just define traits with generic par
by davidatbu 4y ago
So I have a question about GATs: since there was a lot of concern about the complexity cost they add, was it not possible to just define traits with generic params (instead of generic associated types, GATs) to achieve everything GATs could? To demonstrate, doesn't something like this work for lending iterators?
trait LendingIterator<'a, T> {
fn next(&'a mut self) -> Option<&'a T>;
}
impl<'a, T> LendingIterator<'a, T> for Whatever {
fn next(&'a mut self) -> Option<&'a T> {
todo!()
}
}
And of course, you'd modify the for loop syntax to desugar appropriately if the the iterator implements LendingIterator.
- xavxav 4y agoAn immediate issue which could be addressed with further complexity is that for loop desugaring is done very early in Rust's compilation pipeline, way before typing is actually performed. This would also be weird in the case that you have a for loop over some generic iterator, the monomorphized code could vary quite a bit based on what trait your type implements.
- retrocat 4y agoAs far as I can tell, this can't actually work, because the return type of `fn next(&'a mut self)` should actually live for some lifetime `'b` that is shorter than `'a`, instead of at least as long as `'a`; mostly because `next` borrows `self` mutably.