rust - 不能在 Rc 中借用为可变的

标签 rust borrowing actix-web

首先我是 Rust 的新手 :-)

问题: 我想创建一个名为 RestServer 的模块,其中包含添加路由和启动服务器的方法 (actix-web)。

struct Route
{
   url: String,
   request: String,
   handler: Box<dyn Fn(HttpRequest) -> HttpResponse>
}


impl PartialEq for Route {
   fn eq(&self, other: &Self) -> bool {
     self.url == other.url
   }
}

impl Eq for Route {}

impl Hash for Route {
   fn hash<H: Hasher>(&self, hasher: &mut H) {
      self.url.hash(hasher);
   }
}

这是路由结构,这个结构包含路由 url、请求类型(GET、POST 等)和 hanlder 是必须捕获请求并返回 HTTPResponse 的函数

pub struct RestServer
{
   scopes: HashMap<String, Rc<HashSet<Route>>>,
   routes: HashSet<Route>,
   host: String,
}

impl RestServer {

   pub fn add_route(self, req: &str, funct: impl Fn(HttpRequest) -> HttpResponse + 'static,
                 route: &str, scope: Option<&str>) -> RestServer
   {
       let mut routes_end = self.routes;
       let mut scopes_end = self.scopes;
       let url = self.host;
       let route = Route {
          url: String::from(route),
          request: String::from(req),
          handler: Box::new(funct)
    };

    if let Some(x) = scope {
        if let Some(y) = scopes_end.get(x) {
            let mut cloned_y = Rc::clone(y);
            cloned_y.insert(route);
            scopes_end.insert(String::from(x), cloned_y);
        }else {
            let mut hash_scopes = HashSet::new();
            hash_scopes.insert(route);
            scopes_end.insert(String::from(x), Rc::new(hash_scopes));
        }
    } else {
        routes_end.insert(route);
    }

    RestServer {
        scopes: scopes_end,
        routes: routes_end,
        host: String::from(url)
    }
  }

最新的代码是RestServer的实现。 最重要的部分是add_route 函数,该函数接收作为参数的字符串路由、函数处理程序、请求字符串和范围。 首先我创建路由对象。 我检查范围是否存在于 HashMap 中,如果是,我必须采用实际范围并更新 HashSet。

当我构建代码时出现以下错误

   error[E0596]: cannot borrow data in an `Rc` as mutable
   --> interface/src/rest/mod.rs:60:17
   |
60 |                 cloned_y.insert(route);
   |                 ^^^^^^^^ cannot borrow as mutable
   |
   = help: trait `DerefMut` is required to modify through a dereference, but it is not 
     implemented for `std::rc::Rc<std::collections::HashSet<rest::Route>>`

我知道编译器会给我一些帮助,但老实说,我不知道该怎么做,或者我是否可以使用一些简单的解决方案。 在 google 中进行大量搜索后,我在 RefCell 中找到了解决方案,但不是很清楚

预先感谢您的帮助

最佳答案

你不能借用一个可变的引用计数指针;这是因为它提供的保证之一只有在结构是只读的情况下才有可能。

不过,您可以绕过它,但这需要对签名进行一些更改。

输入内部可变性

内部可变性是您可能从其他编程语言以互斥、原子和同步原语的形式了解到的一个概念。实际上,这些结构允许您暂时保证您是给定变量的唯一访问者。

在 Rust 中,这特别好,因为它允许我们从结构中提取对内部成员的可变引用,该结构只需要对自身的不可变引用即可运行。非常适合 Rc

根据您的需要,您会找到 CellRefCell结构正是你所需要的。这些不是线程安全的,但话又说回来,Rc 也不是,所以它不完全是一个交易破坏者。

在实践中,它的工作原理非常简单:

let data = Rc::new(RefCell::new(true));
{
  let mut reference = data.borrow_mut();
  *reference = false;
}
println!("{:?}", data);

playground

(如果您想要线程版本,Arc 替换 RcMutexRwLock 替换 单元格/RefCell)

关于rust - 不能在 Rc 中借用为可变的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58599539/

相关文章:

rust - 尽管已实现,但尚未实现特征Serialize?

json - 是否可以使用 json 的 json_serde::from_str() 获取值,其中字符串周围没有“”?

rust - 如何匹配Arc内的选项?

rust - 实现树形数据结构

rust - 内部可变性与数据隐藏以固定可变借用的所指对象

rust - 如何将生命周期设置为闭包中捕获的值?

asynchronous - 始终返回 Ok HttpResponse 然后在 actix-web 处理程序中工作

rust - 如何在 actix-web 中接收多个具有相同名称的查询参数?

rust - 如何将 &Vec<Box<dyn Child>> 转换为 &Vec<Box<dyn Base>> where trait Child : Base {}?

rust - 为什么声明的泛型类型多于它使用的泛型类型的函数需要类型注释?