So I am in a fistfight with the borrow checker again. What's new?
fn foo() -> Box<dyn KeycodeProvider> {
let (kb_tx, kb_rx) = mpsc::unbounded();
let mut kb_state = Box::new(AppKb {
xkb_state: None,
xkb_keymap: None,
});
let mapped_kb_rx= {
let kb_state_mut = kb_state.as_mut();
kb_state_mut.map_receiver(kb_rx)
};
kb_state
}
Here, AppKb implements the KeycodeProvider trait. The map_receiver function stores the provided reference to self in a closure (it basically just maps the stream, but to do that it needs to access xkb_state and xkb_keymap inside the AppKb struct.)
Now, when I try to return kb_state, the borrow checker complains. I don't quite understand why, since presumably the AppKb struct isn't actually moved. It's only the box that's moved, and the closure inside map_receiver doesn't hold a reference to it.
What is the way to solve this? I know I can use Rc<RefCell<>>, but that kind of just seems like a weird workaround.