如何捕获可变变量?

时间:2013-08-09 02:49:51

标签: rust

struct Level{
    i_vec: ~[int]
}
pub struct GameManager{
    lvl: Level
}
impl GameManager {
    pub fn new() -> GameManager{
        GameManager {lvl: Level{i_vec: ~[]}}
    }
    pub fn new_game(f: ~fn()) {
        do spawn {
            f();
        }
    }
    pub fn default_game_loop(lvl: &Level ,f: &fn() ){
        loop {
            f();
            break;
        }
    }
}
fn main() {
    let mut gm = GameManager::new();
    do GameManager::new_game(){
        // I know I could move "gm" here, but I would like
        // to know how to capture mutable variables.
        do GameManager::default_game_loop(&gm.lvl){

        }
    }

}
/*
/home/maik/source/test.rs:28:43: 28:45 error: mutable variables cannot be implicitly captured
/home/maik/source/test.rs:28         do GameManager::default_game_loop(&gm.lvl){
                                                                        ^~
error: aborting due to previous error
[Finished in 0.2s with exit code 101]
*/

如何捕获可变变量?

我也尝试制作那些函数方法,但之后一切都破了,因为它试图将自己移动到闭包中,如

do gm.default_game_loop(){
    let level = &gm.lvl;    
}

我可以使用自我参数吗?因为gm本身应该在闭包中可用,如

do gm.default_game_loop(){
        let level = self.lvl;    
}

1 个答案:

答案 0 :(得分:1)

您遇到的问题是您无法直接在两个任务之间共享内存。处理此问题的传统方法是将数据移动到线程。这是一个例子:

use std::task;

struct Level {
    i_vec: ~[int]
}
pub struct GameManager {
    lvl: Level
}
impl GameManager {
    pub fn new() -> GameManager {
        GameManager { lvl: Level { i_vec: ~[] } }
    }
    pub fn new_game<A: Send>(a: A, f: ~fn(A)) {
        task::spawn_with(a, f)
    }
    pub fn default_game_loop(lvl: &Level, f: &fn()) {
        loop {
            f();
            break;
        }
    }
}
fn main() {
    let mut gm = GameManager::new();
    do GameManager::new_game(gm) |gm| {
        // I know I could move "gm" here, but I would like
        // to know how to capture mutable variables.
        do GameManager::default_game_loop(&gm.lvl) {

        }
    }
}

如果你需要以某种方式在多个线程之间共享可变状态,那就有点棘手了。为此,您可以设置一个拥有可变状态的任务,并通过std::comm::stream个频道或通过受互斥锁保护的extra::arc::RWArc进行修改。