In Rust, how do you explicitly tie the lifetimes of two objects together, without referencing eachother?

◇◆丶佛笑我妖孽 提交于 2019-12-01 22:21:03
Chris Morgan

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>.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!