简体   繁体   English

为什么此特性/实现不兼容-约束寿命与具体寿命

[英]Why is this trait/implementation incompatible - bound lifetime vs concrete lifetime

I'm struggling with this error rustc gives me: 我正在努力解决这个错误, rustc给了我:

error: method `create_shader_explicit` has an incompatible type for trait: expected bound lifetime parameter 'a, found concrete lifetime

My trait declaration is pretty much this: 我的特质声明几乎是这样的:

pub trait GraphicsContext<R: Resources> {

    /// Creates a shader object
    fn create_shader<'a>(&'a self, shader::Stage, source: &str) -> 
        Result<handle::Shader<R>, shader::CreateError>;

}

Here's my implementation, 这是我的实现,

pub struct OpenGLResources<'a> {
    phantom: PhantomData<&'a u32> 
}

impl<'a> Resources for OpenGLResources<'a> {
    type Shader = Shader<'a>;
}

impl<'z> GraphicsContext<OpenGLResources<'z>> for OpenGLGraphicsContext {

    /// Creates a shader object
    fn create_shader<'a>(&'a self, stage: shader::Stage, source: &str) -> 
        Result<handle::Shader<OpenGLResources>, shader::CreateError> {

        let shader = Shader::new(self, stage);
        try!(shader.compile_from_source(source));

        Ok(shader)
    }

}

In other questions on StackOverflow, they are missing things like <'a> between create_shader and () , however when I compare the fn definitions in mine they look identical. 在有关StackOverflow的其他问题中,它们缺少create_shader()之间的<'a>类的东西,但是当我比较自己的fn定义时,它们看起来是相同的。

EDIT: 编辑:

Changing the definition inside impl to the following fixes that issue impl的定义更改为以下修复此问题的方法

fn create_shader<'a>(&'a self, stage: shader::Stage, source: &str) ->     
    Result<handle::Shader<OpenGLResources**<'z>**>, shader::CreateError>

But then the issue is that 'a and 'z need to be the same lifetime. 但是,问题在于'a'z必须具有相同的生存期。 If I change it to this: 如果我将其更改为:

fn create_shader(**&'z** self, stage: shader::Stage, source: &str) -> 
    Result<handle::Shader<OpenGLResources<'z>>, shader::CreateError>

The impl block works, but then I need a way of specifying the 'z lifetime in the trait definition. impl块有效,但随后我需要一种在特征定义中指定'z生存期的方法。 I tried the following: 我尝试了以下方法:

pub trait<'z> GraphicsContext<R: Resources<'z>>

But it didn't work. 但这没有用。

When comparing things like this, you need to remember to expand all the generics so that you can actually compare it all. 在比较这样的事情时,您需要记住扩展所有泛型,以便您实际上可以对所有泛型进行比较。 In this case, you haven't expanded R . 在这种情况下,您没有扩展R If you do, the answer becomes obvious: R is OpenGLResources<'z> , linking the OpenGLResources to the impl block, whereas your method definition has elided the lifetime on OpenGLResources , causing it to be inferred as self 's lifetime, which is 'a . 如果这样做的话,答案很明显: ROpenGLResources<'z> ,将OpenGLResources链接到impl块,而您的方法定义已在OpenGLResourcesOpenGLResources了生存期,导致将其推断为self的生存期,即'a

Thanks the hints of @Chris Morgan I managed to implement this functionality and its now working fine. 感谢@Chris Morgan的提示,我设法实现了此功能,现在可以正常工作了。

If we start with the base trait with the 'a lifetime included: 如果我们从包含'a寿命'a的基本特征入手:

trait Resources<'a> {
    type Shader: Shader;
    type ShaderProgram: ShaderProgram;
}

Then implement it for OpenGL. 然后为OpenGL实现它。 (note the PhantomData struct) (注意PhantomData结构)

struct OpenGLResources<'a> {
   phantom: PhantomData<&'a u32> // 'a is the lifetime of the context reference
}

impl<'a> ResourcesTrait<'a> for Resources<'a> {
    type Shader = Shader<'a>;
    type ShaderProgram = ShaderProgram<'a>;
    type CommandBuffer = CommandBuffer;
    type CommandBufferBuilder = CommandBufferBuilder;
} 

Its a bit verbose, but the GraphicsContext trait works fine too now. 它有点冗长,但是GraphicsContext特征现在也可以正常工作。 The 'a lifetime goes in the type parameters part. 'a寿命变为在类型参数的一部分。

trait GraphicsContext<'a, R: Resources<'a>> {

    fn create_shader(&'a self, ty: Type, source: &str) -> Result<R::Shader, ShaderCreationError>

}

Finally this is the required code in the graphics context implementation. 最后,这是图形上下文实现中所需的代码。 It is extremely verbose with the 'a lifetimes sprinkled everywhere but at least it works! 这是极其冗长的'a到处洒寿命,但至少它的工程!

impl<'a> GraphicsContext<'a, Resources<'a>> for OpenGLGraphicsContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM