I'm trying to understand lifetimes and am unable to think through this one.
This works:
trait SecretTrait {
type Pub: for<'a> PublicTrait<'a>; // -------- (0)
fn public(&self) -> &Self::Pub;
fn foo(&self) -> <Self::Pub as PublicTrait>::Sig; // --- (1)
}
trait PublicTrait<'a>: Clone {
type Sig: Clone;
type SigIter: IntoIterator<Item = &'a u8>;
fn sig_to_bytes(&self, sig: &'a Self::Sig) -> Self::SigIter;
}
fn main() {}
But this does not:
trait Base<'a> {
type Sig: Clone;
type SigIter: IntoIterator<Item = &'a u8>;
fn sig_to_bytes(&self, sig: &'a Self::Sig) -> Self::SigIter;
}
trait SecretTrait: for<'a> Base<'a> {
type Pub: for<'a> PublicTrait<Sig = <Self as Base<'a>>::Sig>; // --- (2)
fn public(&self) -> &Self::Pub;
}
trait PublicTrait: for<'a> Base<'a> + Clone {}
fn main() {}
For the 1st set the for<'a>
syntax at (0)
works and (1)
does not seem to need lifetime annotation for PublicTrait
though it needs it, but for the 2nd set, (2)
fails saying that:
error[E0582]: binding for associated type `Sig` references lifetime `'a`, which
does not appear in the trait input types
I don't properly understand what for<'a>
is doing and why one works and one does not.