Is there a way to implement "become" in Stakker? #35
Replies: 2 comments 2 replies
-
That's an interesting idea. It would mean going through a kind of redirect table depending on what "identity" the actor is taking at that time. In Stakker, actor method calls are resolved statically at compile-time, so that everything possible can be optimised down. So the question is whether traits can help with this. (See https://uazu.github.io/stakker/g-traits.html about some different approaches to traits with actors.) The problem being that the trait vtable is captured when the actor is created. Rust doesn't give us a way to switch that at runtime after it is captured. However maybe it could be done with something like I guess "become" is straightforward if you're doing method lookup at runtime (which Stakker isn't), so it means a few more hoops have to be jumped through to make it work for Stakker. The extra indirection to allow switching "identities" ends up being explicit, at least how thing are at present. So the quick answer is that no I haven't looked at doing this, and there isn't any built-in Stakker mechanism to enable it. It would be good if a way could be found, though, that looks neat in the finished code. I will have a think. |
Beta Was this translation helpful? Give feedback.
-
So if we were to approach this through traits, that would mean defining a trait for the actor's API, and then having several implementations of that trait for the different modes that the actor could be in. The actor would contain a The alternative would be to have an enum for the different modes kept inside the actor structure. Then for each of the methods, you'd need a match on that enum to decide what to do. Since Rust permits data to be associated with enum variants, then the match can pick up access to the variant's data with So it's a matter of how you'd prefer to structure the code. With an enum and match, all the code is organized under the method calls. With a trait and separate implementations, it's all grouped under the implementations. I think there would be more duplication using a trait, since there is a definition and then all those signatures have to be repeated for the implementations. The duplication when using an enum is just the match variants. The enum approach may have an advantage when some handling is shared between modes, since state related to that shared handling could be kept outside of the enum. If you're still interested in the trait approach, I could perhaps try and put together an example. I'm not sure how exactly the trait methods will inform the glue code when to switch modes. Maybe an additional argument or return value. Anyway let me know if you're interested in trying this and need me to put something together. |
Beta Was this translation helpful? Give feedback.
-
I think the ergonomics of Stakker are really nice, but one thing is missing for me, coming from CAF, namely
become
.In CAF an actor can use
become
to change it's behavior, which can be used to implement different states of a state machine.For instance:
Imagine you have a protocol that can send two message types. In CAF you can create two behaviors with the same interface and change the behavior at runtime. So you can have a
ParseHeaderPackageActor
which can become aParseDataPackageActor
and when it's done, it can useunbecome
to go back to the previous behavior.So my question is, if I can somehow replicate that in Stakker. I already tried creating an actor from a trait, but switching behavior doesn't seem to work there.
I can probably find a way to implement this using either an additional actor or with using an enum for the internal state, but it would be cool if there were a stakker way to do this.
Beta Was this translation helpful? Give feedback.
All reactions