I'm trying to make this code work:
use std::marker::PhantomData;
trait LuaRead<L> {
}
struct InsideCallback;
struct LuaMutex<'a, T> {
lua: &'a InsideCallback,
marker: PhantomData<*const T>,
}
impl<'a, T> LuaRead<&'a InsideCallback> for LuaMutex<'a, T> {
}
fn foo<T>() where T: for<'i> LuaRead<&'i InsideCallback>
{
}
fn main() {
foo::<LuaMutex<i32>>()
}
The error is when we call foo
:
the trait bound `for<'i> LuaMutex<'_, i32>: LuaRead<&'i InsideCallback>` is not satisfied
(Note: Before you ask, yes I do need this exact design, please don't question it. If you want to take a look at the real code, it is here, here and here. I would be happy to obtain any feedback on a different design in the real code, but I avoided you this pain by simplifying the problem.)
Basically I'd like the type parameter of T
to implement LuaRead
with a reference to a InsideCallback
created within the body of foo
(not shown here).
Satisfying this requirement is exactly the purpose of the line impl<'a, T> LuaRead<&'a InsideCallback> for LuaMutex<'a, T>
.
If you compare this line to the error message, it's exactly that. Yet the compiler says that the trait is not satisfied.
I opened an issue in which someone suggested to use two different lifetimes for the &'a InsideCallback
and the LuaMutex<'a>
.
...Which gives me this code:
use std::marker::PhantomData;
trait LuaRead<L> {
}
struct InsideCallback;
struct LuaMutex<'a, T> {
lua: &'a InsideCallback,
marker: PhantomData<*const T>,
}
impl<'a: 'm, 'm, T> LuaRead<&'a InsideCallback> for LuaMutex<'m, T>
{
}
fn foo<T>() where T: for<'i> LuaRead<&'i InsideCallback>
{
}
fn main() {
foo::<LuaMutex<i32>>()
}
For which the error is even harder to understand:
the requirement `for<'i> 'i : ` is not satisfied (`expected bound lifetime parameter 'i, found concrete lifetime`)
I don't even understand which "concrete lifetime" the compiler is referring to. The inferred lifetime at foo::<LuaMutex<i32>>()
is equivalent to 'm
in the trait impl, and the for<'i>
is equivalent to 'a
in the trait impl.
If I change the code to do foo::<LuaMutex<'static, i32>>()
, the error message remains the same! If the "concrete lifetime" was this inferred lifetime here, then I feel that the error message would be different.
Any explanation for this error or any work-around would be appreciated.