randomPoison / thespian

An experiment in designing an ergonomic actor framework for Rust
1 stars 0 forks source link

Support actor traits #15

Open randomPoison opened 4 years ago

randomPoison commented 4 years ago

It would be useful to define "thespian-aware" traits that actors can implement and be used to abstract over the concrete actor being communicated with. For example, we may want to create a mock actor implementation for use in testing, in which case we need a way to swap between the "real" actor instance and the mock.

This can be accomplished by generating concrete message types for the functions a trait, and then reusing those existing messages when implementing the trait for the actor rather than generating different message types for each actor implementing the trait. For example:

#[thespian::actor_trait]
pub trait MyTrait {
    fn foo(&self, value: u32);
}

Would generate the following helper types:

pub struct MyTrait__foo(u32);

pub struct MyTraitProxy {
    // ...
}

impl MyTraitProxy {
    pub fn foo(&self, value: u32) -> Result<(), MessageError> {
        // ...
    }
}

We would also need a way to convert a proxy for an actor that implements an actor trait into the trait's proxy type. This will likely also require implementing a more flexible ProxyFor equivalent, since the current ProxyFor is generic over a concrete actor type and we instead need to be "generic over a trait" (which isn't actually possible with Rust's current type system). This will likely require a heavy dose of type erasure, and we may need to lean on code generation to produce some extra dummy types to make this work. Explorations into the design space here are welcome!