5 ms·
This will only work "safely" with garbage-collected or ref-counted types and these do still not seem to work for interfaces. I'm not quite sure if I understand
by ekidd 12y ago
This will only work "safely" with garbage-collected or ref-counted types and these do still not seem to work for interfaces.
I'm not quite sure if I understand the problem you're describing. I can definitely share mutable objects between threads, and I can do so using abstract traits (aka "interfaces"). Here's an example:
https://gist.github.com/emk/acdf3ab9c79ba1abe6d2 https://gist.github.com/emk/acdf3ab9c79ba1abe6d2
I had to use a "box" here, because that's the easiest way to store objects of varying size that implement a specific interface. Experienced Rust developers may be able to simplify this a bit.
Is this what you were thinking about? Or did you mean something else?
- Matthias247 12y agoThe part that your example is missing is that one side uses the trait object (Common) whereas the other side uses the "normal" type (Foo or Bar) or another trait object view of the object. And the be able to cast between such "smart-boxed" types after you acquired one of them. See the disucssion a little bit below for examples with Rc (it's the same problem but within a thread instead of between threads).