首页 文章

逐步构建引用相同数据的数据结构

提问于
浏览
0

我有一个 EntityMap 对象,它处理空间索引任何可以有边界框的东西 . 我已经以这样的方式实现它,它存储对象的引用而不是拥有的值(这可能不是最好的方法,但我认为将其更改为拥有的值只会改变我的问题) .

我目前正在尝试做的是将对象添加到向量中,使它们不会与之前添加到向量中的任何内容发生冲突 . 以下是我刚刚描述的伪锈:

let mut final_moves = Vec::new();
let mut move_map = EntityMap::new();
for m in moves.into_iter() {
    let close_moves = move_map.find_intersecting(m.bounding_box());
    let new_move = m.modify_until_not_intersecting(close_moves);
    final_moves.push(new_move);
    move_map.insert(final_moves.last().unwrap());
}

final_moves 是从函数返回的内容 . 这里的问题是我可以随意借用 final_moves ,但我希望在 move_map 中存储对其对象的引用 . 我无法弄清楚如何解决的冲突是我想要同时逐步构建 final_movesmove_map ,但这似乎要求它们都是可变的,然后阻止我从 move_map 借用数据 .

如何重构我的代码以实现我的目标?

1 回答

  • 0

    您可以将 move_mapfinal_moves 的类型更改为包含 Rc<Move> 而不仅仅是 Move .

    let mut final_moves = Vec::new();
    let mut move_map = EntityMap::new();
    for m in moves.into_iter() {
        let close_moves = move_map.find_intersecting(m.bounding_box());
        let new_move = Rc::new(m.modify_until_not_intersecting(close_moves));
        final_moves.push(Rc::clone(new_move));
        move_map.insert(new_move);
    }
    

相关问题