简体   繁体   中英

Access two mutable references from a Global hashmap at once in Rust

Say we have a globally accessible hashmap of trait objects we make with lazy_static : MY_ANIMALS: Mutex<HashMap<i32, AnimalBox>> , where type AnimalBox = Box<dyn AnimalExt+Send>

Now, we want the animals in this global hashmap to interact with each other. For example, one AnimalBox can AnimalExt::eat(&mut self, prey: &mut AnimalBox) another.

The problem is that our eat() function requires both a mutable reference to self, as well as a mutable reference to the pray (because we want the pray to AnimalExt::perish(&mut self) when it's eaten.

However, getting two mutable references to our hashmap causes a WouldBlock Error:

use lazy_static::lazy_static;
use std::sync::Mutex;
use std::collections::HashMap;

//type alias for our boxed animals
type AnimalBox = Box<dyn AnimalExt+Send>;

//globally accessible hashmap for keeping track of our animals throughout the scope of our application
lazy_static! {
    static ref MY_ANIMALS: Mutex<HashMap<i32, AnimalBox>> = Mutex::new(HashMap::new());
}

//simple trait for our animals
trait AnimalExt{
    //eat() function requires a mutable reference to another AnimalBox
    fn eat(&mut self, pray: &mut AnimalBox);
    fn perish(&mut self);
    fn energy(&self)->i32;
    fn id(&self)->i32;
}

struct Wolf{
    id: i32,
    energy: i32,
    alive: bool,
}
impl AnimalExt for Wolf{
    fn id(&self)->i32{
        self.id
    }
    fn eat(&mut self, pray: &mut AnimalBox) {
        pray.perish();
        self.energy+= pray.energy()
    }
    fn energy(&self) ->i32 {
        self.energy
    }
    fn perish(&mut self){
        self.alive = false; 
    }
}
impl Wolf{
    pub fn new(id: i32)->Self{
        Wolf{
            id: id,
            energy: 50,
            alive: true,
        }
    }
}
struct Cow{
    id: i32,
    energy: i32,
    alive: bool,
}
impl Cow{
    pub fn new(id: i32)->Self{
        Cow{
            id: id,
            energy: 100,
            alive: true,
        }
    }
}
impl AnimalExt for Cow{
    fn id(&self)->i32{
        self.id
    }
    fn eat(&mut self, pray: &mut AnimalBox) {
        pray.perish();
        self.energy+= pray.energy()
    }
    fn energy(&self) ->i32 {
        self.energy
    }
    fn perish(&mut self){
        self.alive = false; 
    }
}
fn main() {
    println!("Hello, world!");
    //define our animals
    let cow1 = Box::new(Cow::new(1)) as AnimalBox;
    let cow2 = Box::new(Cow::new(2)) as AnimalBox;
    let wolf1 = Box::new(Wolf::new(3)) as AnimalBox;
    let wolf2 = Box::new(Wolf::new(4)) as AnimalBox;

    //insert them into the global hashmap
    MY_ANIMALS.lock().unwrap().insert(cow1.id(), cow1);
    MY_ANIMALS.lock().unwrap().insert(cow2.id(), cow2);
    MY_ANIMALS.lock().unwrap().insert(wolf1.id(), wolf1);
    MY_ANIMALS.lock().unwrap().insert(wolf2.id(), wolf2);

    //getting one animal to eat() another causes a WouldBlock error
    match (MY_ANIMALS.try_lock().unwrap().get_mut(&0), MY_ANIMALS.try_lock().unwrap().get_mut(&1)){
        (Some(a1), Some(a2))=>{
            a1.eat(a2);
        }
        _=>()
    }
}

Is there a good work around for this? Or is there no safe way to do this with a hashmap? I've seen this answer to a similar question, but the suggested answer suggests using RefCell , which is incompatible with lazy_static's Send trait requirement.

I ended up using the muti_mut crate, which provides several methods for having multiple mutable references to a HashMap or BTreeMap.

use lazy_static::lazy_static;
use std::sync::Mutex;
use std::collections::HashMap;
use multi_mut::{HashMapMultiMut, HashMapMutWrapper};

//type alias for our boxed animals
type AnimalBox = Box<dyn AnimalExt+Send>;

//globally accessible Hashmap for keeping track of our animals throughout the scope of our application
lazy_static! {
    static ref MY_ANIMALS: Mutex<HashMap<i32, AnimalBox>> = Mutex::new(HashMap::new());
}

//simple trait
trait AnimalExt{
    //eat() function requires a mutable reference to another AnimalBox
    fn eat(&mut self, pray: &mut AnimalBox);
    fn perish(&mut self);
    fn energy(&self)->i32;
    fn id(&self)->i32;
}

struct Wolf{
    id: i32,
    energy: i32,
    alive: bool,
}
impl AnimalExt for Wolf{
    fn id(&self)->i32{
        self.id
    }
    fn eat(&mut self, pray: &mut AnimalBox) {
        pray.perish();
        self.energy+= pray.energy()
    }
    fn energy(&self) ->i32 {
        self.energy
    }
    fn perish(&mut self){
        self.alive = false; 
    }
}
impl Wolf{
    pub fn new(id: i32)->Self{
        Wolf{
            id: id,
            energy: 50,
            alive: true,
        }
    }
}
struct Cow{
    id: i32,
    energy: i32,
    alive: bool,
}
impl Cow{
    pub fn new(id: i32)->Self{
        Cow{
            id: id,
            energy: 100,
            alive: true,
        }
    }
}
impl AnimalExt for Cow{
    fn id(&self)->i32{
        self.id
    }
    fn eat(&mut self, pray: &mut AnimalBox) {
        pray.perish();
        self.energy+= pray.energy()
    }
    fn energy(&self) ->i32 {
        self.energy
    }
    fn perish(&mut self){
        self.alive = false; 
    }
}
fn main() {
    println!("Hello, world!");
    //define our animals
    let cow1 = Box::new(Cow::new(1)) as AnimalBox;
    let wolf1 = Box::new(Wolf::new(2)) as AnimalBox;
    let before_eating_cow = wolf1.energy();
    //insert them into the global hashmap
    MY_ANIMALS.lock().unwrap().insert(cow1.id(), cow1);
    MY_ANIMALS.lock().unwrap().insert(wolf1.id(), wolf1);


    //use get_pair_mut method from the multi_mut crate
    match MY_ANIMALS.try_lock().unwrap().get_pair_mut(&1, &2){
        Some((hunter, prey))=>{
            dbg!("hunter eats prey");
            hunter.eat(prey);
        }
        None=>()
    }
    let after_eating_cow = MY_ANIMALS.lock().unwrap().get(&1).unwrap().energy();
    assert_ne!(before_eating_cow, after_eating_cow);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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