How does Dioxus mut signals work?

Dioxus demonstrations explicitly declare signals/states as mut

let mut x = use_signal(|| 0.0f64);

The problem I see with this is that then taking this x mutably in lambdas keepsx moved until the lambda is dropped, which means the component's rsx! macro shouldn't be able to use the signal at all. Somehow, Dioxus just works with it.

To me it should be:

let x = use_signal(|| 0.0f64);

And don't allow std::ops like AddAssign and use interior mutability.

Conceptually GenerationalBox<T> which backs Signal<T> is similar to &'static mut T. When you move the signal into a closure, it just moves the point, not the value. Any methods that accept &mut self on signal are just to get some borrow checking behavior to prevent errors. Internally everything is backed by interior mutability (either RefCell or RwLock)

The generational box source code and documentation have more details generational_box - Rust

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.