I'm trying to define a trait that, in certain implementations (but not all), will have functions that take in references to buffer structs. I want to constrain the lifetimes of the references to the structs such that they can't outlive the lifetime of the buffer inside the struct. I don't have any issues doing that directly in an implementation, but when I add one more layer (Wrap
in my example code) I can't figure out how to properly constrain my lifetimes.
In my example code, I understand that for<'a,'ba>
is telling the compiler that it's supposed to be valid for any 'a
and any 'ba
, but I don't want to tell it that. I can't figure out any way to constrain the lifetimes introduced by for<>
(which is fair if it means what I think it does) nor can I figure out how else I could introduce the lifetimes in the Wrap
struct definition.
pub trait Foo<'a, A> {
fn bar(&self, input: &'a A) -> ();
}
pub struct Buffer<'a> {
a: &'a [u8],
}
pub struct FooImpl {}
impl<'a, 'ba:'a> Foo<'a, Buffer<'ba>> for FooImpl {
fn bar(&self, _: &'a Buffer<'ba>) -> () {}
}
pub struct Wrap<A>
where
A: for<'a,'ba> Foo<'a, Buffer<'ba>>
{
a: A,
}
pub fn main() -> () {
Wrap { a: FooImpl {} };
}
Compiling playground v0.0.1 (/playground)
error: implementation of `Foo` is not general enough
--> src/main.rs:22:3
|
22 | Wrap { a: FooImpl {} };
| ^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
|
= note: `FooImpl` must implement `Foo<'0, Buffer<'1>>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `Foo<'_, Buffer<'2>>`, for some specific lifetime `'2`
error: could not compile `playground` (bin "playground") due to 1 previous error
playground link: Rust Playground
I'm still learning Rust, so if someone could help me understand what specific documentation is relevant to this issue, that would be greatly appreciated!