I am using hashmap under the following struct
#[derive(Default)]
pub struct CasbinGRPC {
enforcer_map: HashMap<i32, Enforcer>,
adapter_map: HashMap<i32, Box<dyn Adapter>>,
}
I want to use it under the following function:
pub fn get_enforcer(&self, handle: i32) -> Result<&Enforcer, &str> {
self.enforcer_map.get(&handle).ok_or("No enforcer found")
}
Here, self
refers to the above struct. get()
returns reference to the Enforcer value &Enforcer
, but I need Enforcer
as value. I am not sure how can I do it? Using get_mut()
I get following error:
22 | pub fn get_enforcer(&self, handle: i32) -> Result<&mut Enforcer, &str> {
| ----- help: consider changing this to be a mutable reference: `&mut self`
23 | self.enforcer_map.get_mut(&handle).ok_or("No enforcer found")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
I can't use &mut self
in above function, since its restricted by some other functions definition defined by .proto
file extracted using tonic_build
.
So the only thing I could do is to find a way so that hashmap can return dereferenced value Enforcer
without needing to have &mut self
. Also, I have tried using remove()
but that doesn't makes sense since we need to remove the value from the hashmap and that too requires to have &mut self
in function definition.
NOTE:
Enforcer is defined here: casbin-rs/enforcer.rs at master · casbin/casbin-rs · GitHub