首页 > 解决方案 > 没有为 `CasbinActor 实现特征`Actor``

问题描述

error[E0277]: the trait bound `CasbinActor<CachedEnforcer>: Actor` is not satisfied
  --> actix-middleware-example/src/api/user.rs:34:12
   |
34 |     actor: web::Data<Addr<CasbinActor<CachedEnforcer>>>,
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Actor` is not implemented for `CasbinActor<CachedEnforcer>`
   | 
  ::: /home/samarpan/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-0.11.1/src/address/mod.rs:77:20
   |
77 | pub struct Addr<A: Actor> {
   |                    ----- required by this bound in `Addr`

error[E0277]: the trait bound `CasbinActor<CachedEnforcer>: Actor` is not satisfied
  --> actix-middleware-example/src/services/account_service.rs:24:12
   |
24 |     actor: web::Data<Addr<CasbinActor<CachedEnforcer>>>,
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Actor` is not implemented for `CasbinActor<CachedEnforcer>`
   | 
  ::: /home/samarpan/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-0.11.1/src/address/mod.rs:77:20
   |
77 | pub struct Addr<A: Actor> {
   |                    ----- required by this bound in `Addr`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `actix-middleware-example`

To learn more, run the command again with --verbose.

这是我运行时的错误cargo build

Actor trait 是为 CasbinActor 实现的。


pub struct CasbinActor<T: IEnforcer + 'static> {
    pub enforcer: Option<Arc<RwLock<T>>>,
}

impl<T: IEnforcer + 'static> CasbinActor<T> {
    pub async fn new<M: TryIntoModel, A: TryIntoAdapter>(
        m: M,
        a: A,
    ) -> Result<Addr<CasbinActor<T>>> {
        let enforcer = T::new(m, a).await?;
        Ok(Supervisor::start(|_| CasbinActor {
            enforcer: Some(Arc::new(RwLock::new(enforcer))),
        }))
    }

    pub fn set_enforcer(e: Arc<RwLock<T>>) -> Result<CasbinActor<T>> {
        Ok(CasbinActor { enforcer: Some(e) })
    }

    pub fn get_enforcer(&mut self) -> Option<Arc<RwLock<T>>> {
        self.enforcer.as_ref().map(|x| Arc::clone(x))
    }
}

impl<T: IEnforcer + 'static> Actor for CasbinActor<T> {
    type Context = Context<Self>;
}

解决方法是什么?我认为问题可能是因为 CasbinActor在isActor时实现了一个特征。也许必须做类似的事情,但我究竟在哪里添加代码来解决这个问题?TIEnforcerCachedEnforcer

这是回购 - https://github.com/casbin-rs/examples/tree/master/actix-middleware-example

标签: actix-webrust-actixcasbin

解决方案


推荐阅读