【问题标题】:Application architecture: Problem with Mutable/Immutable reference应用程序架构:可变/不可变引用的问题
【发布时间】:2018-09-22 16:19:13
【问题描述】:

我尝试编写一个用户可以从模板创建对象的应用程序。

在我的梦里,代码是这样的:

struct DataTemplate {
    tmp: u32,
}

struct Data<'a> {
    name: &'a str,
    tmp: u32,
}

struct Consumer<'a> {
    items: Vec<Data<'a>>,
}

struct Library {
    items: Vec<DataTemplate>,
}

struct Application<'a> {
    library: Library,
    consumer: Consumer<'a>,
}

impl DataTemplate {
    fn new(data: u32) -> Self {
        DataTemplate { tmp: data }
    }
}

impl<'a> Data<'a> {
    fn new(name: &'a str, tmp: u32) -> Self {
        Data { name, tmp }
    }
    fn from_template(template: &DataTemplate, name: &'a str) -> Self {
        Data::new(name, template.tmp)
    }
}

impl<'a> Consumer<'a> {
    fn new() -> Self {
        Consumer { items: vec![] }
    }
    fn consume(&mut self, data: Data<'a>) {
        self.items.push(data);
    }
}

impl Library {
    fn new() -> Self {
        Library { items: vec![] }
    }
    fn add(&mut self, d: DataTemplate) {
        self.items.push(d);
    }
    fn get(&self, index: usize) -> &DataTemplate {
        &self.items[index]
    }
}

impl<'a> Application<'a> {
    fn new() -> Self {
        Application {
            library: Library::new(),
            consumer: Consumer::new(),
        }
    }
    fn get_library(&self) -> &Library {
        &self.library
    }
    fn get_library_mut(&mut self) -> &mut Library {
        &mut self.library
    }
    fn get_consumer_mut(&mut self) -> &mut Consumer<'a> {
        &mut self.consumer
    }
}

fn main() {
    let mut app = Application::new();

    use_it(&mut app);
}

fn use_it(app: &mut Application) {
    app.get_library_mut().add(DataTemplate::new(1));
    app.get_library_mut().add(DataTemplate::new(2));
    app.get_library_mut().add(DataTemplate::new(3));

    let item = app.get_library().get(1);
    app.get_consumer_mut()
        .consume(Data::from_template(item, "hi"));
}

问题出在use_it函数中:

error[E0502]: cannot borrow `*app` as mutable because it is also borrowed as immutable
  --> src/main.rs:89:5
   |
88 |     let item = app.get_library().get(1);
   |                --- immutable borrow occurs here
89 |     app.get_consumer_mut()
   |     ^^^ mutable borrow occurs here
90 |         .consume(Data::from_template(item, "hi"));
91 | }
   | - immutable borrow ends here

我需要获取应用程序的不可变引用 (let item = app.get_library()) 来获取模板,然后从模板创建数据项后,我必须将其添加到应用程序中的使用者,现在当然是可变的(app.get_consumer_mut())。

对于这样的问题是否有任何通用的解决方案,或者这整个想法不是 Rust 方式?

【问题讨论】:

  • app 的消费者不知道也不可能知道app.get_library() 和app.get_consumer_mut() 最终指向的不是同一个引用。
  • 那么将应用程序分成两部分,一个可变部分和一个不可变部分是否有意义?

标签: architecture rust immutability mutability


【解决方案1】:

在尝试访问使用者之前,您需要确保删除对库的引用。例如:

let data = Data::from_template(app.get_library().get(1), "hi");
app.get_consumer_mut().consume(data);

【讨论】:

  • 谢谢 - 我的演示代码遗漏了一个重要信息 :) 但是对于给出的代码,这是正确的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
相关资源
最近更新 更多