是否可以借用结构的一部分作为可变部分而其他部分作为不可变部分?
Is it possible to borrow parts of a struct as mutable and other parts as immutable?
是否可以借用结构的一部分作为可变部分,而另一部分作为不可变部分 - 如果结构的字段是私有的。
fn main() {
let mut ecs = EntityComponentSystem::new();
for e_id in ecs.get_entities_with_component::<Velocity>().unwrap() {
let components = ecs.get_mut_components(e_id);
...
}
impl EntityComponentSystem {
...
pub fn get_entities_with_component<K: Key>(&self) -> Option<&HashSet<u64>> {
self.entities_with_components.get(&TypeId::of::<K>())
}
pub fn get_mut_components(&mut self, entity_id: u64) -> &mut TypeMap {
let entity = self.entities.get_mut(&entity_id).unwrap();
&mut entity.components
}
}
pub struct EntityComponentSystem {
entities: HashMap<u64, Entity>, <------- I would like to modify this.
entities_with_components: HashMap<TypeId, HashSet<u64>>, <---- while reading from this!
}
编译器给我:
error[E0502]: cannot borrow `*ecs` as mutable because it is also borrowed as immutable
--> physics.rs:19:26
|
18 | for e_id in ecs.get_entities_with_component::<Velocity>() {
| --- immutable borrow occurs here
19 | let components = ecs.get_mut_components(*e_id);
| ^^^ mutable borrow occurs here
...
26 | }
| - immutable borrow ends here
我没有理解的是,在我们基本上返回了 entities_with_components
字段的一部分之后,get_entities_with_component
中的 &self
引用是如何被借用的。
不应该只借那部分吗?有什么办法可以强制执行吗?
不,函数不能 return 对结构的一部分的引用并留下部分借用的结构。
但是你可以 return 像这样借用不可变和可变的元组
#[derive(Debug)]
struct AB(u32, u32);
impl AB {
fn borrow_parts(&mut self) -> (&u32, &mut u32) {
(&self.0, &mut self.1)
}
}
fn main() {
let mut ab = AB(0, 2);
{
let (a, b) = ab.borrow_parts();
*b = *a;
}
println!("{:?}", ab);
}
你只能借用整个结构作为不可变或可变的,没有只借用它的一部分的概念。当这成为一个问题时,您可以使用 interior mutability in the form of a RefCell
:
pub struct EntityComponentSystem {
entities: RefCell<HashMap<u64, Entity>>,
entities_with_components: HashMap<TypeId, HashSet<u64>>,
}
现在您可以将整个结构作为不可变借用,并将 RefCell
的内容独立作为可变借用:
pub fn get_mut_components(&self, entity_id: u64) -> &mut TypeMap {
let mut entities = self.entities.borrow_mut();
let entity = entities.get_mut(&entity_id).unwrap();
&mut entity.components
}
是否可以借用结构的一部分作为可变部分,而另一部分作为不可变部分 - 如果结构的字段是私有的。
fn main() {
let mut ecs = EntityComponentSystem::new();
for e_id in ecs.get_entities_with_component::<Velocity>().unwrap() {
let components = ecs.get_mut_components(e_id);
...
}
impl EntityComponentSystem {
...
pub fn get_entities_with_component<K: Key>(&self) -> Option<&HashSet<u64>> {
self.entities_with_components.get(&TypeId::of::<K>())
}
pub fn get_mut_components(&mut self, entity_id: u64) -> &mut TypeMap {
let entity = self.entities.get_mut(&entity_id).unwrap();
&mut entity.components
}
}
pub struct EntityComponentSystem {
entities: HashMap<u64, Entity>, <------- I would like to modify this.
entities_with_components: HashMap<TypeId, HashSet<u64>>, <---- while reading from this!
}
编译器给我:
error[E0502]: cannot borrow `*ecs` as mutable because it is also borrowed as immutable
--> physics.rs:19:26
|
18 | for e_id in ecs.get_entities_with_component::<Velocity>() {
| --- immutable borrow occurs here
19 | let components = ecs.get_mut_components(*e_id);
| ^^^ mutable borrow occurs here
...
26 | }
| - immutable borrow ends here
我没有理解的是,在我们基本上返回了 entities_with_components
字段的一部分之后,get_entities_with_component
中的 &self
引用是如何被借用的。
不应该只借那部分吗?有什么办法可以强制执行吗?
不,函数不能 return 对结构的一部分的引用并留下部分借用的结构。
但是你可以 return 像这样借用不可变和可变的元组
#[derive(Debug)]
struct AB(u32, u32);
impl AB {
fn borrow_parts(&mut self) -> (&u32, &mut u32) {
(&self.0, &mut self.1)
}
}
fn main() {
let mut ab = AB(0, 2);
{
let (a, b) = ab.borrow_parts();
*b = *a;
}
println!("{:?}", ab);
}
你只能借用整个结构作为不可变或可变的,没有只借用它的一部分的概念。当这成为一个问题时,您可以使用 interior mutability in the form of a RefCell
:
pub struct EntityComponentSystem {
entities: RefCell<HashMap<u64, Entity>>,
entities_with_components: HashMap<TypeId, HashSet<u64>>,
}
现在您可以将整个结构作为不可变借用,并将 RefCell
的内容独立作为可变借用:
pub fn get_mut_components(&self, entity_id: u64) -> &mut TypeMap {
let mut entities = self.entities.borrow_mut();
let entity = entities.get_mut(&entity_id).unwrap();
&mut entity.components
}