3 ms·
I'm not aware of any production quality UI toolkits in Rust, but using traits and reference counting, Rust certainly supports this use case. In fact, it suppor
by humanrebar 12y ago
I'm not aware of any production quality UI toolkits in Rust, but using traits and reference counting, Rust certainly supports this use case.
In fact, it supports it more flexibly since different widgets don't have to share the same methods and data at all... well, unless you want them to. And you can have your widgets implement more than one trait without having to combine them all into one giant class definition.
- Matthias247 12y agoAs soon as it supports storing trait objects in smart pointers and casting them. That's afaik still not the case.
- humanrebar 12y agoYou can as long as they are immutable objects. Again, you can't alias mutable references, even if they are of the exact same type. Maybe you should read this: http://doc.rust-lang.org/guide.html#traits http://doc.rust-lang.org/guide.html#traits
- steveklabnik 12y agoI believe your parent is referring to storing trait objects in something like Arc<T>, which is not supported: currently, Arc<T> requires T to be sized.
- Matthias247 12y agoSimilar, but not exactly: As I said, the objective is to have access to one object (in it's native form or in an interface (supertype) form from different locations. For that you can can use Rc. To be able to mutate it you can add RefMut and end up with Rc<RefMut<T>>, e.g. Rc<RefMut<IWidget>>. And now how do I get an Rc<RefMut<TextBox>> or any other type like &mut TextBox back from that?
- comex 12y agoI think you could store objects of type Box<DerefMut<IWidget>> and then write a generic wrapper struct which holds an Rc<RefCell<T>> and derefs into the trait object. Admittedly this is ugly, and unnecessarily slow. For a more general solution, I think the language would allow implementing some kind of RcTransformed type which, given 'T, U : Unsized?, Transform : Fn(&T) -> &U' (or possibly a more general type, but I think that would require HKT?), takes an Rc<T>, calls the transform, and stores both the reference and the transformed reference, allowing direct access to the latter. This would require an unsafe implementation, but would be safe to use. Alternatively, there may be some existing functionality like that that I just haven't heard of...