I have the following code in my lib.rs
extern crate serde;
extern crate serde_json;
extern crate build_script_file_gen;
use serde_json::Value;
use serde_json::from_str;
use std::fs::File;
use std::io::Read;
use std::collections::HashMap;
pub enum ConfigurationSource {
StringContent(String),
FileContent(String)
}
pub struct ConfigurationBuilder {
config: Value,
bundles: HashMap<String, Vec<ConfigurationSource>>
}
impl<'a> ConfigurationBuilder{
pub fn new(base_source: ConfigurationSource) -> ConfigurationBuilder{
let base_config: Value = from_str("{}").unwrap();
let mut config_builder = ConfigurationBuilder{
config: base_config,
bundles: HashMap::new()
};
config_builder.merge_source(&base_source);
return config_builder;
}
pub fn merge_sources(&mut self, config_sources: &Vec<ConfigurationSource>){
for source in config_sources{
self.merge_source(source);
}
}
pub fn merge_source(&mut self, config_source: &ConfigurationSource){
match config_source {
&ConfigurationSource::StringContent(ref content) => {
let config_override: Value = from_str(&content[..]).unwrap();
merge(&mut self.config, config_override);
//merge(&mut config, &json_override);
},
&ConfigurationSource::FileContent(ref path) => {
let mut config_file = File::open(path).unwrap();
let mut config_file_content = String::new();
config_file.read_to_string(&mut config_file_content).unwrap();
let config_override: Value = from_str(&config_file_content[..]).unwrap();
merge(&mut self.config, config_override);
}
}
}
pub fn merge_bundle(&mut self, bundle_key: &str){
let sources = self.bundles.get(bundle_key).unwrap();
self.merge_sources(sources);
}
}
fn merge(a: &mut Value, b: Value) {
match (a, b) {
(a @ &mut Value::Object(_), Value::Object(b)) => {
let a = a.as_object_mut().unwrap();
for (k, v) in b {
merge(a.entry(k).or_insert(Value::Null), v);
}
}
(a, b) => *a = b,
}
}
I get the following error when compiling
error[E0502]: cannot borrow `*self` as mutable because `self.bundles` is also borrowed as immutable
--> src/lib.rs:62:9
|
61 | let sources = self.bundles.get(bundle_key).unwrap();
| ------------ immutable borrow occurs here
62 | self.merge_sources(sources);
| ^^^^ mutable borrow occurs here
63 | }
| - immutable borrow ends here
- How is the statement
let sources = self.bundles.get(bundle_key).unwrap();
imply an immutable borrow? - How is
self
a mutable borrow? i.e.self
is not assigned to any variable here usinglet mut
- How do I avoid the aforementioned compile error. Note that if I comment out the whole
merge_bundle
method implementation, it compiles successfully.