Skip to content
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

is there any way to create custom Actor traits that are object safe ? #10

Open
cameronbraid opened this issue Jan 20, 2023 · 1 comment

Comments

@cameronbraid
Copy link

cameronbraid commented Jan 20, 2023

I would like to define a custom actor trait like the following :

pub trait StorageActor : Handler<WriteData> + Handler<ReadData> {}

However when trying to use a LocalActorRef<StorageActor> I get

size for values of type `(dyn StorageActor + 'static)` cannot be known at compilation time
the trait `Sized` is not implemented for `(dyn StorageActor + 'static)`

I'm still fairly new to rust, and after reading https://huonw.github.io/blog/2015/01/object-safety/ I still don't fully understand what the specific blocker is. - could it be in the Actor trait fn actor_ref(&self, ctx: &ActorContext) -> LocalActorRef<Self> which seems to match up to the link under References Self - There’s two fundamental ways in which this can happen, as an argument or as a return value

Cheers

Cameron

@LeonHartley
Copy link
Owner

Hey @cameronbraid,

It's not currently possible to use a trait object in place of an Actor type for LocalActorRefs, there is too much reliance on using the type directly, with requirements for it to be Sized. I do think it's possible to remove this limitation in the future, but will require a little more thought.

As a workaround, you could potentially create a Storage trait, which has async read and write methods, and implementations of it calling some underlying LocalActorRef respectively - this is a pattern that I've used myself in Coerce, when writing the actor persistence storage system with support for multiple backends: https://github.com/LeonHartley/Coerce-rs/blob/master/coerce/src/persistent/journal/storage.rs#L14

Implementation example, backed by an actor: https://github.com/LeonHartley/Coerce-rs/blob/master/providers/persistence/coerce-redis/src/journal/mod.rs#L125

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants