Hello, I have the following code:
struct MustWrapStatic<'a, T: 'static>(&'a mut T);
struct OnlyContainsStaticFields<T: TraitItselfIsNotStatic>{
static_field: T::Static
}
trait TraitItselfIsNotStatic{
type Static: 'static;
}
trait Foo<'a, T: TraitItselfIsNotStatic>{
type Bar;
}
impl<'a, T: TraitItselfIsNotStatic> Foo<'a, T> for (){
type Bar = MustWrapStatic<'a, OnlyContainsStaticFields<T>>; //error
}
Which produces this error:
error[E0309]: the parameter type
T
may not live long enough
While suggesting to bound T
with : 'static
. That is not possible in my use case, also it should not be required as OnlyContainsStaticFields
only contains a static field.
The user Globi on the Rust Discord suggested this fix which works:
impl<'a, T: TraitItselfIsNotStatic> Foo<'a, T> for ()
where OnlyContainsStaticFields<T>: 'static{
type Bar = MustWrapStatic<'a, OnlyContainsStaticFields<T>>;
}
However, I currently experiment with GATs. Foo
and it's impl would change to this:
trait Foo{
type Bar<'a, T: TraitItselfIsNotStatic>;
}
impl Foo for (){
type Bar<'a, T: TraitItselfIsNotStatic> =
MustWrapStatic<'a, OnlyContainsStaticFields<T>>; //error
}
And for that this does not work as I can't bound the impl to a GAT.
I could even do...
struct OnlyContainsStaticFields<T: TraitItselfIsNotStatic>
where Self: 'static
...or...
struct OnlyContainsStaticFields<T: TraitItselfIsNotStatic>
where OnlyContainsStaticFields<T>: 'static
...which gets entirely ignored by the compiler.
Does someone have a suggestion how to fix it in this case?