I'm trying to update some older code I wrote that basically looks like:
trait Foo<T>{}
struct Bar<A, B: Foo<A>>{
b: B
}
This used to work totally fine, but now I am getting a compile error:
src/test.rs:19:12: 19:13 error: parameter `A` is never used
src/test.rs:19 struct Bar<A, B: Foo<A>> {
^
src/test.rs:19:12: 19:13 help: consider removing `A` or using a marker such as `core::marker::PhantomData`
So I can try to remove the type parameter and get something like this:
struct Bar<A>{
b: Foo<A>
}
however this is not really what I want. In my original code B
resolves to a sized type, but now Foo<A>
is unsized.
The other suggested solution is to try using this PhantomData the error mentions, resulting in:
struct Bar<A, B: Foo<A>> {
b: B,
marker: PhantomData<A>
}
but this seems really messy to me. Reading the docs for PhantomData
seem to indicate this is meant to be used with unsafe code, but I am not working with unsafe code anywhere here. All I want is for Bar
to contain an instance some type that implements Foo
.
Is this really the only way to handle this situation now, or am I missing something?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…