DioxusLabs / dioxus

Fullstack GUI library for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
19.33k stars 736 forks source link

Use owning_ref to remove need for calling .read and .write on signals #2181

Closed vultix closed 1 month ago

vultix commented 3 months ago

Feature Request

I believe it should be possible using the owning-ref crate to directly implement Deref and DerefMut for Signal, allowing code like signal.mutate() instead of signal.write().mutate()

This will be a huge ergonomic win if we can get it to work!

ealmloff commented 3 months ago

I'm not familiar with OwningRef, but rust has a limitation that makes it impossible to both implement Copy and Drop at the same time. To safely implement Deref, Refs need to hold a lock that prevents other RefMuts from accessing the data at the same time. To release that lock when you are done using the Ref, you need to implement Drop.

Because of this I don't think it is possible to implement both Copy and Deref on Signal. (RefRef in the OwningRef crate implements Drop so it cannot implement Copy)

vultix commented 1 month ago

Ah, you're spot on. If we ever get something akin to super let it should be possible to implement something like this, but for now this seems impossible.