1

I am working on a Rust crate that opens network interfaces using several available libraries. The project consists of:

  • Trait Library and and implementations LibraryA, LibraryB, LibraryC.
  • Trait Interface and its implementations InterfaceA, 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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    Perhaps you want `type LI<'a>: Interface<'a>;` / `Self::LI<'a>` (different `LI<'a>` for different `'a`) instead of `type LI: for<'a> Interface<'a>;` / `Self::LI` (the single `LI` implements `Interface<'a>` for all `'a`)? If so, this is still [under implementation](https://github.com/rust-lang/rust/issues/44265). – Masaki Hara Jan 26 '18 at 13:30

0 Answers0