I have a clonable struct, called GenICam
. This struct has a HashMap
of Rc<dyn Node>
trait objects, and a HashMap
of Rc<Category>
structs which implement the Node
trait. The keys are NodeName
which is an alias type for String
.
Each category has a list of feature names, which are represented in the nodes
HashMap
of GenICam
. This list should be used to populate the node_list
field with references to the nodes using the functions below:
use std::{collections::HashMap, rc::Rc};
type NodeName = String;
#[derive(Clone)]
pub struct GenICam {
nodes: HashMap<NodeName, Rc<Node>>,
categories: HashMap<NodeName, Rc<Category>>,
}
impl GenICam {
fn get_categories(&self) -> Vec<Rc<Category>> {
let mut collect = Vec::new();
for i in &self.categories {
collect.push(i.1.clone());
}
collect
}
/// Fills all categories with references to the features if available
fn populate_categories(&mut self) {
let mut cats = self.get_categories();
for cat in cats {
let mut mutcat = cat;
mutcat.populate(&self);
}
}
}
#[derive(Clone)]
pub struct Category {
pub name: NodeName,
features: Vec<String>,
pub node_list: HashMap<NodeName, Rc<Node>>,
}
impl Category {
pub fn populate(&mut self, genicam: &GenICam) {
for feat in &self.clone().features {
let result = genicam.node(feat.to_string());
match result {
None => (),
Some(x) => self.add_to_node_list(x),
};
}
}
/// populate the node hashmap with the given node
pub fn add_to_node_list(&mut self, node: &Rc<Node>) {
println!("Succes: {:?}", node.name());
self.node_list.insert(node.name(), node.clone());
}
}
I get the following error:
error[E0596]: cannot borrow immutable borrowed content as mutable
--> src/genicam.rs:174:4
|
| mutcat.populate(&self);
| ^^^^^^ cannot borrow as mutable
I cannot wrap my head around why mutcat
is immutable, since it is defined as let mut
.