I want to explain this point a little bit more.
the "impedance mismatch" of using rust to do callbacks for "traditional" retained mode framework is NOT fundamental to callbacks or retained mode, but the architectural design of the framework.
for example, if a callback is only allowed to access states that are local to the widget, or in other words, it is captureless, or at least it doesn't capture shared states, then the borrow checker is totally ok.
on the other hand, a callback is useless if it were restricted to the widget and couldn't access the app states: you have to be able to access the app states in order to write the app logic.
traditionally, an even callback receives widget specific and event specific parameters. but the app states are stored alongside the callback function as some opaque user data/context pointer, typically attached to the widget and retrievable from widget handle, e.g. GetWindowLongPtr on windows, or the this pointer in many OOP wrappers. in rust, this user context is typically the captured environment of a closure.
the problem is, this user data/context is owned by the application, and must be wired up when the callback function is registered (i.e. must be captured when the closure is created). this forces all the callbacks to have shared ownership of the app states, i.e. Rc<RefCell<...>> in rust.
but it doesn't have to be like this! to designed a principled retained mode gui framework for rust, we can let the framework to manage the life cycle of the app states, e.g. moving the ownership into the event loop.
because the callbacks are dispatched from the event loop, which is single threaded, we can pass this state as an argument, as an exclusive reference, to the callbacks. this eliminates all the ergonomic issues related to interior mutability and weak pointers.
however, this approach still has the problems of composibility: all event handlers use the same type for the app states, which means your app must have a monolithic design, you can't break it into components, not easily at least.
to solve the composibility, we need an adapter layer that can give a "focused view" of a larger state value. if you have used xilem, you must have seen the lens function already.
the final piece of the puzzle is reactivity: when the app states get updated, how to notify the framework to redraw the ui? well, there's no definitely "best" solution to this problem. options may include (non exhaustive):
-
conservatively redraw the ui after every (batch of) event callbacks
-
only redraw when the user explicitly notified the framework, similar to the egui ctx.request_repaint() api
-
the callback must return an (optional) action, and the framework will take the action accordingly
-
the callbacks receives a copy of the states, the framework diff the states before and after callbacks, and redraw the ui when the states are changed.
-
require the app state to implement some trait that can tell the framework whether it has changed and needs to redraw the ui.
-
ditch the idea of arbitrary user defined app state type, for the app state to use some reactive signal wiring mechanism instead.