Is there any way to enforce that a associated type does not contain an explicit lifetime i.e. in terms of a reference? In the example below, the compiler (correctly) rejects compiling with E0597:
/// A result which may outlive its creator.
trait Result {}
impl Result for () {}
trait FactoryTrait {
// How do I annotate that the type must not contain a borrow of Self?
type Result: Result;
fn get(self) -> Self::Result;
}
struct Factory<'a, T>(&'a T)
where
Self: FactoryTrait;
trait FactoryCreator: Default
where
for<'a> Factory<'a, Self>: FactoryTrait,
{
fn create<'a>(&'a self) -> Factory<'a, Self>;
}
#[derive(Default)]
struct StructA {}
impl FactoryCreator for StructA {
fn create<'a>(&'a self) -> Factory<'a, Self> {
Factory(self)
}
}
impl<'a> FactoryTrait for Factory<'a, StructA> {
type Result = ();
fn get(self) -> Self::Result {
()
}
}
fn spawn<T: 'static + FactoryCreator>() -> impl Result
where
for<'a> Factory<'a, T>: FactoryTrait,
{
let factory_creator = T::default();
let factory = factory_creator.create();
factory.get()
}
However, as long FactoryTrait::Result
does not depend on Factory<'_, StructA>
that should be a safe operation. Is there any way to enforce this and satisfy the compiler?