I am new to Rust here. I am trying to build a concurrent btree and I'm struggling with implementing algorithms on the tree.
In this example, I used a simplified version of my tree where the LatchNode
's type is Arc<RwLock<Node>>
. I would like to implement a method called find_child
which not only finds the node with matching key in the tree, I also want a stack of RwLockWriteGuard
for all the parent nodes on the way to the child. This way, we are confident that another concurrent operation won't mess up the search result.
pub struct Node {
k: i32,
children: Vec<Option<LatchNode>>,
}
type LatchNode = Arc<RwLock<Node>>;
pub fn find_child<'a>(
root: LatchNode,
k: i32,
) -> (RwLockWriteGuard<'a, Node>, Vec<RwLockWriteGuard<'a, Node>>) {
let guard = root.clone().write().unwrap();
let mut stack = Vec::new();
stack.push(guard);
loop {
let temp = stack.last().unwrap();
// for simplicity, we travese down first node in children
let child = temp.children[0].clone();
match child {
Some(ref child_node) => {
let child_guard = child_node.write().unwrap();
if child_guard.k == k {
return (child_guard, stack);
}
stack.push(child_guard)
}
None => {
panic!("No node found");
}
}
}
}
However, I am getting the error
cannot return value referencing local data `child.0`
returns a value referencing data owned by the current function
`child.0` is borrowed here -> refers to the line : Some(ref child_node) => {
Looking at the code, it seems like Rust compiler thinks child_node
is a temporary variable. But I am confident that RwLock that Arc
points to will exist even at the end of the function. How might I be able to fix this method?