Where-clause-only lifetime parameters in struct definitions?


#1

I’m using hlua for a library I’m writing. Most of hlua uses a 'lua lifetime parameter, so my code looks like:

pub struct Foo<L> {
  lua: L,
}

...

impl<'lua, L> Foo<L> where L: AsMutLua<'lua> {
  ...
}

Now I want to add a custom Drop implementation to this, so I started off by writing

impl<'lua, L> Drop for Foo<L> where L: AsMutLua<'lua> {
  ...
}

But this fails because the Drop implementation has an extra type constraint that the struct doesn’t have. (This restriction is understandable.)

How do I add this where clause to the struct without adding an extra lifetime parameter to the type?

I could do

pub struct Foo<'lua, L> where L: AsMutLua<'lua> {
  lua: L,
  _marker: PhantomData<'lua>,
}

but that’s really leaky.


#2

The usage of the lifetime parameter in AsLua and AsMutLua traits is quite weird. I’m not sure what it’s intended to mean. Also, it seems that most of implementors implement these traits for any lifetime 'lua anyway, with the only exception being the Lua struct. But the Lua::new function has the following signature:

fn new() -> Lua<'lua>

Which means that compiler is free to choose any lifetime parameter for any Lua, so probably all of the structures will have AsMutLua<'x> implemented for any 'x.

So I don’t actually know the answer to your exact question, but my guess would be that you can make your struct work without the lifetime parameter using the following bound (on both struct and impl):

where for<'lua> L: AsMutLua<'lua>

or maybe even just

where L: AsMutLua<'static>