繁体   English   中英

无法推断出合适的生命周期

[英]cannot infer an appropriate lifetime for lifetime

我有这样的事情:

type G2d<'a> = GfxGraphics<'a, Resources, CommandBuffer>;

如何将其作为字段包含在结构中?

pub struct Dc
{
  g : G2d,
  c : Context,
}

但它给出:

expected named lifetime parameter

我试过了:


pub struct Dc
{
  g : &'a mut G2d<'a>,
  c : Context,
}

window.draw_2d( &event, | c, g, device |
{
  let mut dc = Dc { c, g };
});

但它给出:

图片|690x385

我将不胜感激任何帮助。

整个结构应该用生命周期注释:

pub struct Dc<'a>
{
  g : G2d<'a>,
  c : Context,
}

正确的解决办法是

pub struct Dc<'a, 'b>
{
  g : &'a mut G2d<'b>,
  c : Context,
}

在 G2d<'a> 内部创建引用的同时创建外部 &mut 借用在逻辑上是不正确的。 因此,在表达式 &'a mut G2d<'a> 中,这两个 'a 生命周期不可能意味着它们借用了相同的东西。 正确的解决方案是使用多个生命周期。

暂无
暂无

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

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