Help with lifetimes in a gtk MVC app


#1

I’m looking for a little help dealing with gtk in an MVC app.

I’m still trying to get a small example working, but the basic issue is that I have two callbacks from gtk. One needs mutable access to the controller to respond to user actions. Since it’s captured in a closure, it needs static lifetime, so I’ve wrapped the controller in Rc<RefCell>, and it’s clones get captured by the closures so the callback can borrow it with appropriate mutability and scope.

The second callback - used to update the display - really just needs read-only access to the model. So I just clone the controller again, and let the callback borrow that to extract the model information from.

So far, so good. But the first callback needs to trigger updates of the display before it exits. Since it’s a method of the controller, it’s always borrowed when this happens, so the second callback panics when it borrows the controller to get access to the model.

What I’d like to do is have only a reference to the model captured by the second closure But since the controller creates the model, I’ve gone ahead and embedded the model directly in it to keep things simple. I don’t change the model once the controller is created, so this should be safe. But the compiler (quite correctly) doesn’t trust that. Is there some way to tell the compiler that the model in the controller shouldn’t change?

The obvious workaround is to wrap the model in an Rc::RefCell as well, and let the controller use that. But this feels like the view creating problems in the controller, which rubs me the wrong way.