-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An Actor implementing multiple traits? #33
Comments
Providing concrete example of what I am talking about: Here I define the parent's trait inside of the child. So this way, the child only references the parent for all io requests. All chips would do this, so they don't communicate with each other. This way you can mix and match any chip to communicate with any other chip. The glue between all the chips would be handled by one single parent, implementing all of these chips. Here is an example board implementing the chip But actor of trait seems to only allow for one trait Does it make sense why I am interested in allowing for multiple traits for one actor? |
You can define a trait made up of other traits:
Then you could use However I suspect that this does not solve your problem, at least not immediately. I guess you want each chip to receive a reference to the board actor that lists only the traits that that chip requires, not all possible traits. So this means casting one trait to another. This may be possible, but I have not tried it. For example you need to cast I wonder whether Rust is able to do these kinds of casts. It is from one dynamic trait to another, but still everything is static knowledge, at least locally to where the actor is created. It may be possible. If you look at the |
I tried the casting without success. The process of going from But certainly you can do the multiple-traits thing, by defining a single trait which includes all of them. |
I was able to define:
However, I am not sure what to do from here. I understand Rust loses the ability to cast the trait once it is wrapped in something like a RefCell. Reading online it seems like RefCell, and hence Actor can't cast due to invariance. Is there a way to briefly unwrap the Actor to cast the trait? Or somehow don't wrap the trait into an Actor until later? AlternativelyHere is a proof of concept of how to can cast it with generics: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=21fa200c0aea421fea892e7334db616a But I am not sure how to convert the
Because it is no longer |
I don't think it can be done with this approach. If you do However if you look at this alternate method of doing dyn Trait, that uses a wrapper. You could have several wrappers for different traits. Effectively Rust will create a different wrapper for each combination of external trait and board (since it is generic). The efficiency should be about the same, but the interface looks different. Although it doesn't look quite so clear in the code (actor calls look like normal calls), I wonder whether that could help. To be honest in this kind of situation I've used the third method on that page, i.e. using It would be nice if we could find a clean way of doing this, but this faces Rust limitations. Maybe if one day I could implement |
Omg, that was a lot of indirection / boilerplate... But it works! Thank you! I don't understand if I would be able to use |
You don't need to have an ActorOwnAnon at all. That is only if you don't have anything else owning the actor, to keep it from being killed off. A Sorry about the boilerplate. I was looking again at |
In my emulator, I am making each chip actor run independent of each other, not reference each other directly.
I do this by having them only reference a parent trait actor any time they want to
call!
other actors.The parent actor implementing this trait, will
call!
the other children actors.Ideally, I would like this to be just one central motherboard actor, implementing all traits of all chips, with all chips stored in one struct.
But it seems like
actor_of_trait
is written in a way that forces me to choose just one trait.Is it possible for me to make an actor which implements multiple traits, and pass this one central motherboard to all chip actors?
Otherwise, it looks like I will need a separate board actor implementing the trait for each chip.
The text was updated successfully, but these errors were encountered: