A function generic over two lifetimes (one depending on the other), and generic over two types (each restricted by one of the lifetimes), compiles well.
But, if I add another generic type parameter bound by those two lifetimes, it fails to compile - even though the type is not used at all, and (to me) it only duplicates the existing bounds (OutCollection<'out, OutType>
defines bound OutType: 'out
, but this exact bound already exists for that function).
Any tips, please. (nightly
-only is fine - this is for a benchmarking-only scaffolding.)
pub trait OutCollection<'out, T>
where
T: 'out,
{
}
// COMPILES
pub fn caller_no_collection<'own: 'out, 'out, OwnType: 'own, OutType: 'out>(
own_items: Vec<OwnType>,
) {
called_no_collection::<'_, '_, OwnType, OutType>(&own_items);
}
pub fn called_no_collection<'own: 'out, 'out, OwnType: 'own, OutType: 'out>(
_own_items: &'own Vec<OwnType>,
) {
}
// FAILS TO COMPILE
pub fn caller_with_collection<
'own: 'out,
'out,
OwnType: 'own,
OutType: 'out,
OutCollectionType: OutCollection<'out, OutType> + 'own,
>(
own_items: Vec<OwnType>,
) {
called_with_collection::<'_, '_, OwnType, OutType, OutCollectionType>(&own_items);
// ^^^^^^^^^^
// |
// borrowed value does not live long enough
// argument requires that `own_items` is borrowed for `'out`
}
// - `own_items` dropped here while still borrowed
pub fn called_with_collection<
'own: 'out,
'out,
OwnType: 'own,
OutType: 'out,
OutCollectionType: OutCollection<'out, OutType> + 'own,
>(
_own_items: &'own Vec<OwnType>,
) {
}
Or, get it from https://github.com/peter-kehl/lifetime-sideffect-rs (or just lib.rs).
I'm also surprised that the error is not dropck-related (that's what I had in the related project earlier).
(The use case isn't obvious, but it's minimized from real world: cami-benches/benches/shared/lib_benches.rs at main ¡ cami-rs/cami-benches ¡ GitHub)
Thank you in advance.