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.