首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当使用作为闭包参数传递的引用调用可变方法时,无法推断适当的生存期

当使用作为闭包参数传递的引用调用可变方法时,无法推断适当的生存期
EN

Stack Overflow用户
提问于 2017-03-25 10:24:34
回答 1查看 138关注 0票数 1

我正试着在Rust做一个小游戏。我想使用类似于entity-component-system模式的东西来处理所有游戏对象。

我的总体想法是有一个GameLoop结构,它包含更新和绘制游戏所需的所有数据(屏幕、时间戳等)。

World结构应该包含所有游戏实体,并在dispatch函数中更新它们。它调用所有已注册的回调,这些回调也存储在World结构中(这些回调就是“系统”)。不过,它们在示例代码中是多余的。

我已经尝试尽可能多地分解代码,只包含相关部分。

代码语言:javascript
复制
use std::marker::PhantomData;

struct Something;

///The "somethings" are things like the display, a timestamp, ...
struct GameLoop {
    sth: Something,
    sth2: Something,
}

///C = Context
///The type that is passed to every entity to give it access to things like the delta time
struct World<C> {
    phantom: PhantomData<C>, //This is here so Rust doesn't complain about the type parameter not being used
}

///The data that is passed to the system functions every frame
struct TickData<'a> {
    delta: u64,
    sth: &'a Something,
    sth2: &'a mut Something,
}

impl GameLoop {
    fn new() -> GameLoop {
        GameLoop {
            sth: Something {},
            sth2: Something {},
        }
    }

    ///One game "tick" - Supposed to do things like calculating delta time, swapping buffers, ...
    ///Those are then passed to the callback
    fn update<F: FnMut(u64, &Something, &mut Something)>(&mut self, f: &mut F) {
        f(0, &self.sth, &mut self.sth2);
    }
}

impl<C> World<C> {
    fn new() -> World<C> {
        World { phantom: PhantomData }
    }

    ///Supposed to update all the game entities
    fn dispatch(&mut self, context: &mut C) {
        //...
    }
}

impl<'a> TickData<'a> {
    fn new<'b>(delta: u64, sth: &'b Something, sth2: &'b mut Something) -> TickData<'b> {
        TickData {
            delta: delta,
            sth: sth,
            sth2: sth2,
        }
    }
}

fn main() {
    let mut game_loop = GameLoop::new();
    let mut world = World::<TickData>::new();

    //The game update function, called once per frame
    let mut update_fnc = |delta: u64, sth: &Something, sth2: &mut Something| {
        let mut tick_data = TickData::new(delta, sth, sth2);

        &world.dispatch(&mut tick_data); //If this line is commented out, it compiles fine

        //...
    };

    loop {
        game_loop.update(&mut update_fnc); //Calculate the delta time, call the specified function and swap buffers
    }
}

似乎在借用/生命周期方面存在问题。编译器是除了冗长之外的所有东西。

问题似乎是游戏的更新函数中的&world.dispatch(&mut tick_data)调用,它应该更新所有游戏实体。如果我把它注释掉,程序就会编译,没有任何错误。

这是编译器告诉我的:

代码语言:javascript
复制
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'b in function call due to conflicting requirements
  --> src/main.rs:66:29
   |
66 |         let mut tick_data = TickData::new(delta, sth, sth2);
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 65:77...
  --> src/main.rs:65:78
   |
65 |       let mut update_fnc = |delta: u64, sth: &Something, sth2: &mut Something| {
   |  ______________________________________________________________________________^ starting here...
66 | |         let mut tick_data = TickData::new(delta, sth, sth2);
67 | |
68 | |         &world.dispatch(&mut tick_data); //If this line is commented out, it compiles fine
69 | |
70 | |         //...
71 | |     };
   | |_____^ ...ending here
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:66:55
   |
66 |         let mut tick_data = TickData::new(delta, sth, sth2);
   |                                                       ^^^^
note: but, the lifetime must be valid for the expression at 74:25...
  --> src/main.rs:74:26
   |
74 |         game_loop.update(&mut update_fnc); //Calculate the delta time, call the specified function and swap buffers
   |                          ^^^^^^^^^^^^^^^
note: ...so that reference is valid at the time of borrow
  --> src/main.rs:74:26
   |
74 |         game_loop.update(&mut update_fnc); //Calculate the delta time, call the specified function and swap buffers
   |                          ^^^^^^^^^^^^^^^

我根本找不到错误的原因。这些函数是以一种过程化的方式调用的,因为我只是借用了大部分数据,所以生命周期应该不会有问题。

当我从TickData结构中删除引用,使其只包含为Copy特征实现的值时,它也可以正常工作。

我通常不是那种发布一大堆代码并要求人们修复它的人,但我现在真的是一无所知。

EN

回答 1

Stack Overflow用户

发布于 2017-03-25 11:29:12

你的代码没有一个正确的解决方案。它看起来太复杂了,我不知道你为什么要做出一些你已经做出的设计决定。如果我说的都不适用,那么我向你道歉,你将不得不等待下一个回答者。

减少你的问题是正确的想法,但是为什么你停止了呢?它可以一直减少到

代码语言:javascript
复制
struct Something;

struct World<'a> {
    x: TickData<'a>,
}

impl<'a> World<'a> {
    fn dispatch(&mut self, context: &TickData<'a>) {}
}

struct TickData<'a>(&'a Something);

fn update_fnc(world: &mut World, sth: &Something) {
    let tick_data = TickData(sth);
    world.dispatch(&tick_data);
}

fn main() {}

通过反复试验,我们可以找到一些“解决方案”:

代码语言:javascript
复制
impl<'a> World<'a> {
    fn dispatch(&self, context: &TickData<'a>) {}
}

代码语言:javascript
复制
impl<'a> World<'a> {
    fn dispatch(&mut self, context: &TickData) {}
}

代码语言:javascript
复制
impl<'a> World<'a> {
    fn dispatch<'b>(&'b mut self, context: &'b TickData<'b>) {}
}

要获得有关此问题的非常全面的分析,请查看Why does linking lifetimes matter only with mutable references?

让我们看看另一个方面,回到你的main方法:

代码语言:javascript
复制
let mut world = World::<TickData>::new();

我们知道TickData有一个生命周期,那么在这个例子中它是什么呢?我们不能像指定类型一样指定它,它必须从用法中推断出来。那么它用在哪里呢?

一个可以参考的类比是Vec。我们创建一个Vec,然后将内容push到它上面。这些pushes告诉我们T的具体类型是什么。你的代码做了什么:

代码语言:javascript
复制
let mut world = World::<TickData>::new();
// ...
world.dispatch(&mut tick_data);

您创建了一个包含TickData的类型(这就是PhantomData所做的),然后调用一个“推”该类型的方法(fn dispatch(&mut self, context: &mut C)),因此第二个参数必须是所包含的类型,从而解析出最终的类型。

这就引出了另一个问题:不知道这些参数的生命周期有多长。

然而,简单地注释生命周期是不够的:

代码语言:javascript
复制
fn update<'a, F>(&'a mut self, mut f: F)
    where F: FnMut(u64, &'a Something, &'a mut Something)
{
    f(0, &self.sth, &mut self.sth2);
}

这进一步复杂化是因为我们将可变引用sth2传递给dispatchdispatch的定义允许它在其内部存储可变引用的-生命周期和类型匹配,并且它是一个&mut self

这可能会导致多个可变别名,这是不允许的。

我不知道您为什么要将World参数化,但是您可以只将C移到dispatch方法,完全删除PhantomData。这就消除了World存储C的能力。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43011827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档