in the code below, I want TraitA::get_b
to return some generic that may refrence self
. the function dofoo
needs to be able to accept anything with TraitA
where the type returned by get_b
has an arbitrary constraint (in this case, TraitB
).
this, from what I can tell, can not be done without making dofoo
require that T
be static. this is becouse the trait bounds for<'b> T::BT<'b>: TraitB
means that T::BT
needs to have TraitB
for all lifetimes, including 'static
. because T
must outlive 'T::BT' and T::BT
must be valid for 'static
, T
needs to be valid for 'static
.
is there any way to constrain T::BT
without makeing T
static? this code compiles if you remove the call to dofoo
from main
or constrain BT
from within TraitA
(eg define BT
with type BT<'b>: TraitB where Self: 'b
). neither of these solutions work for my use case.
if this really cant be done yet in rust, is there an issue open I can fallow or link to so I can be notified when support is added?
trait TraitA {
type BT<'b>
where Self: 'b;
fn get_b<'b>(&'b self) -> Self::BT<'b>;
}
trait TraitB {
fn foo(&self) {}
}
struct DataA<'a>(&'a i32);
impl TraitA for DataA<'_> {
type BT<'b> = DataB<'b>
where Self: 'b;
fn get_b<'b>(&'b self) -> Self::BT<'b> {
DataB(&self.0)
}
}
struct DataB<'b>(&'b i32);
impl TraitB for DataB<'_> {}
fn dofoo<T>(a: T)
where T: TraitA,
for<'b> T::BT<'b>: TraitB {
let b = a.get_b();
b.foo();
a.get_b().foo();
}
fn main() {
let data: i32 = 8;
let a = DataA(&data);
let b = a.get_b();
b.foo();
a.get_b().foo();
//dofoo(a);
}