So I have some code like this:
use std::borrow::Borrow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ops::Index;
use std::rc::Rc;
struct Data {
// ...
}
struct TableTreeNode {
father: Option<Rc<RefCell<TableTreeNode>>>,
table: HashMap<String, Data>,
}
impl Index<&str> for TableTreeNode {
type Output = Data;
fn index(&self, index: &str) -> &Self::Output {
if self.table.contains_key(index) {
self.table.index(index)
} else {
//recursively goes up
let father: &RefCell<TableTreeNode> = self.father.as_ref().expect("not found");
father.borrow().index(index)
}
}
}
It fails on compile:
error[E0515]: cannot return reference to temporary value
--> xxx.rs:25:13
|
25 | father.borrow().index(index)
| ---------------^^^^^^^^^^^^^
| |
| returns a reference to data owned by the current function
| temporary value created here
I have no clue to solve this. How would a borrow from a RefCell
could be a temporary variable? If I'm doing this wrong, what's the right way to implement these?