Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You 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



I 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.


Similar, 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?


I 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...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: