I am working on a Rust crate that opens network interfaces using several available libraries. The project consists of:
- Trait
Library
and and implementationsLibraryA
,LibraryB
,LibraryC
. - Trait
Interface
and its implementationsInterfaceA
,InterfaceB
,InterfaceC
. All interfaces have references to their libraries.
trait Interface<'a> {
fn do_something();
}
trait Library {
type LI: for<'a> Interface<'a>;
fn open_interface(&self) -> Self::LI;
}
struct LibraryA {
abc: i32,
}
struct InterfaceA<'a> {
abc: &'a i32,
}
impl Library for LibraryA {
type LI = InterfaceA<'a>;
fn open_interface(&self) -> Self::LI {
unimplemented!()
}
}
impl<'a> Interface<'a> for InterfaceA<'a> {
fn do_something() {
unimplemented!()
}
}
This does not compile because I don't know how to define the associated type LI
:
error[E0261]: use of undeclared lifetime name `'a`
--> src/main.rs:19:26
|
19 | type LI = InterfaceA<'a>;
| ^^ undeclared lifetime
Here it gets tricky - LibraryA
does not have any real lifetime (no references to external objects) and I couldn't find any way to define a lifetime where the LI
type gets defined.