应用程序体系结构:可变/不可变引用存在问题

时间:2018-09-22 16:19:14

标签: architecture rust immutability mutability

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

在我的梦中,代码看起来像这样:

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的方法?

1 个答案:

答案 0 :(得分:1)

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

Added the secrets in Openshift.

Create environmental variables based on the secrets.

i have tried to refer the enviornments in tomcat context.xml using {$env.MY_KEY} , but i am unable to read it.