I'm trying to create a struct that is generic, with a bound that the generic implement a trait. The trait is itself generic. This is in Rust 1.49.0.
If I do this:
trait Foo<T> {}
struct Baz<F: Foo<T>> {
x: F,
}
I get a compilation error, because T
is not defined. But if I define it:
trait Foo<T> {}
struct Baz<T, F: Foo<T>> {
x: F,
}
then I get a compiler error because T
is unused.
The only option seems to be to include a PhantomData<T>
field, but if my generic dependence becomes more complicated, this starts to get more unwieldy:
use std::marker::PhantomData;
trait Foo<T> {}
struct Baz<T, U, F: Foo<T>, G: Foo<U>> {
phantom_t: PhantomData<T>,
phantom_u: PhantomData<U>,
x: F,
y: G,
}
Half of my fields are phantoms! The struct is practically haunted.
My question is: Is the example at the end that compiles really idiomatic Rust? And if so, why can Rust not detect that the T
in Baz<T, Foo<T>>
is actually used?