apache / apache/casbin-rust-casbin-grpc

Setting `new_enforcer` function

Open
#13 5 comments 0 reactions 1 assignee Claimed by @hsluoyz View on GitHub
question
Dominant language
Rust
Stars
7
Forks
4
PR merge metrics
No merged PRs in 30d

Description

I am trying to draft the `new_enforcer`, which is as below:

```rust
async fn new_enforcer(
&self,
i: Request,
) -> Result, Status> {
let mut a: Option> = None;
let e: CachedEnforcer;

let get_inner = i.into_inner();

if get_inner.adapter_handle != -1 {
a = match self.get_adapter(get_inner.adapter_handle).await {
Ok(&v) => Some(v),
Err(_) => return Ok(Response::new(casbin_proto::NewEnforcerReply { handler: 0 })),
};
}

if get_inner.model_text == String::from("") {
let cfg = adapter::load_configuration("config/connection_config.json").await?;
let data = match std::fs::read_to_string(cfg.enforcer.as_str()) {
Ok(v) => v,
Err(_) => return Ok(Response::new(casbin_proto::NewEnforcerReply { handler: 0 })),
};
}

if a.is_none() {
let m = match DefaultModel::from_str(get_inner.model_text.as_str()).await {
Ok(v) => v,
Err(_) => return Ok(Response::new(casbin_proto::NewEnforcerReply { handler: 0 })),
};
e = match casbin::CachedEnforcer::new(m, ()).await {
Ok(v) => v,
Err(_) => return Ok(Response::new(casbin_proto::NewEnforcerReply { handler: 0 })),
};
} else {
let m = match DefaultModel::from_str(get_inner.model_text.as_str()).await {
Ok(v) => v,
Err(_) => return Ok(Response::new(casbin_proto::NewEnforcerReply { handler: 0 })),
};

e = match casbin::CachedEnforcer::new(m, a).await {
Ok(v) => v,
Err(er) => return Ok(Response::new(casbin_proto::NewEnforcerReply { handler: 0 })),
};
}

let epass = Arc::new(Mutex::new(e));
let h = self.add_enforcer(epass);
Ok(Response::new(casbin_proto::NewEnforcerReply { handler: h }))
}
```

Here I am getting error in `self.get_adapter()` and `self.add_enforcer()`function, the error message is as follows:

```error
error[E0507]: cannot move out of a shared reference
--> src/server/rpc_calls.rs:355:23
|
355 | a = match self.get_adapter(get_inner.adapter_handle).await {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
356 | Ok(&v) => Some(v),
| -
| |
| data moved here
| move occurs because `v` has type `Box`, which does not implement the `Copy` trait
```

```error
error[E0596]: cannot borrow `*__self` as mutable, as it is behind a `&` reference
--> src/server/rpc_calls.rs:391:17
|
346 | &self,
| ---- help: consider changing this to be a mutable reference: `&mut CasbinGRPC`
...
391 | let h = self.add_enforcer(epass);
| ^^^^^^^^^^^^^^^^^^^^^^^^ `__self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
```

After the initial research I was able to figure out that this is due to the & reference from the `get_adapter` function, which is as below:

```rust
pub async fn get_adapter(&self, handle: i32) -> Result<&Box, &str> {
self.adapter_map.get(&handle).ok_or("No adapter found")
}
```
Here, `adapter_map` is a hashmap:

```rust
pub struct CasbinGRPC {
enforcer_map: HashMap>>,
adapter_map: HashMap>,
}
```

When we take any value from the hashmap, we get the reference of that value rather than the copy of the value. In short, when we do `self.adapter_map.get(&handle)`, we get `&Box` and not `Box`. Since Copy trait is not implemented for Adapter in the `casbin-rs`, we can't use methods like `to_owned` to drop the `&`.
I tried to send the `Box` wrapped in `Arc>>` but then in `convert.rs` of casbin-rs we can't call TryIntoAdapter for it.

I am unable to figure out how can I resolve the above errors.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.