I wrote this method that checks if a BTreeMap has an entry, inserts a default value if it isn't there, and then returns it:
fn select_storage(&mut self, stream_config: &StreamConfig) -> &mut Box<dyn storage::Storage> {
let sk = StreamKey::from(&stream_config);
if let Some(result) = self.sorted_storages.get_mut(&sk) {
result
} else {
self.sorted_storages.insert(sk.clone(), self.create_storage_for_stream(&stream_config));
self.sorted_storages.get_mut(&sk).unwrap()
}
}
The compilation fails with this error:
error[E0499]: cannot borrow `self.sorted_storages` as mutable more than once at a time
--> uplink/src/base/serializer/mod.rs:309:13
|
299 | fn select_storage(&mut self, stream_config: &StreamConfig) -> &mut Box<dyn storage::Storage> {
| - let's call the lifetime of this reference `'1`
...
306 | if let Some(result) = self.sorted_storages.get_mut(&sk) {
| - -------------------- first mutable borrow occurs here
| _________|
| |
307 | | result
308 | | } else {
309 | | self.sorted_storages.insert(sk.clone(), self.create_storage_for_stream(&stream_config));
| | ^^^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
310 | | self.sorted_storages.get_mut(&sk).unwrap()
311 | | }
| |_________- returning this value requires that `self.sorted_storages` is borrowed for `'1`
Even though I'm not using the result of the get_mut
call in the else
branch. Shouldn't the lifetime of that borrow be limited to the if let
branch? How do I make it work?