I started working on my first more ambitious Rust project, and struggle with something I did not come across in any of the resources and tutorials I used for learning. The title of the question captures the abstract problem, but for the examples I'll use the concrete examples I am fighting with.
For my project, I need to interface with different third-party services, and I decided to use the actix framework as an abstraction for the different actors in my domain. The framework defines the Actor
trait that must be implemented:
use actix::prelude::*;
struct MyActor {
count: usize,
}
impl Actor for MyActor {
type Context = Context<Self>;
}
I have a trait of my own that defines the interface for the third-party integrations. Let's call it Client
. I want each client to behave like an actor.
use actix::Actor;
pub trait Client: Actor {}
Somewhere else I have a vector that stores references to all the active clients in the system. When I compile the code, I get the following error:
error[E0191]: the value of the associated type `Context` (from the trait `actix::actor::Actor`) must be specified
--> transponder/src/transponder.rs:15:26
|
15 | clients: Vec<Box<Client>>
| ^^^^^^ missing associated type `Context` value
I spent a few hours now attempting to solve this problem, but none of them worked.
- I tried to specify the type in the trait, but got
associated type defaults are unstable
as an error. - I tried to specify the type in the trait's implementation (
impl Simulation
), but gotassociated types are not allowed in inherent impls
as an error. - I tried some stuff with
impl <T: Actor> Simulation for T
(e.g. as shown here), but nothing worked.
My assumption is that I am missing an important piece of knowledge about traits and types. I would be very grateful if someone could help me solve my problem, and point me in the direction of the missing puzzle piece. I feel there is an important lesson about Rust in here that I really want to learn.