The specific case where I ran into this was in using OpenGL, writing struct
s for a VertexBuffer
and VertexArray
. Each struct is, in e
This is one of the primary use cases for the PhantomData type, as demonstrated there in an example.
Applied to this case, you’ll end up with something like this:
use std::marker::PhantomData;
struct VertexArray<'a> {
id: GLuint,
vbo_lifetime: PhantomData<&'a VertexBuffer>,
}
And instantiation will be something like this:
fn make<'a>(&'a self) -> VertexArray<'a> {
VertexArray {
id: …,
vbo_lifetime: PhantomData,
}
}
(This is eliding the generic type, allowing it to be inferred; you could also write PhantomData::<&'a VertexBuffer>
.)