I've been struggling with this for a while now and just going in circles, so I'm hoping someone can point out where I'm going wrong.
I'm playing with Actix Web, and setting up my first handlers - which is a simple Healthcheck of the system. So what I've got is:
- A
Healthcheck
trait defining a healthcheck - A
HealthcheckHandler
struct that implements theHandler
trait (This is an Actix Web concept) and contains a HashMap - A function that builds an
App
instance for the healthcheck routes (This is an Actix Web concept) by taking aHashMap<String, &Healthcheck>
When I try to build this I get errors that the trait "cannot be sent between threads safely".
I've tried with &Healthcheck
, Box<Healthcheck>
, Box<Healthcheck + Send>
based on another answer on here, and even Mutex<&Healthcheck>
all without luck, but all with subtly different errors. It all seems to be around needing to implement some combinations of Sync, Send and/or Clone, but I'm not sure how to get around that here.
Any pointers to what I should to fix this?
Actual example code:
pub trait Healthcheck {
fn check(&self) -> Result<String, String>;
}
struct HealthcheckHandler {
handlers: HashMap<String, Box<Healthcheck>>,
}
pub fn build_app(handlers: HashMap<String, Box<Healthcheck>>) -> App<()> {
let handler = HealthcheckHandler {
handlers: handlers,
};
App::new()
.prefix("/health")
.resource("", |r| {
r.get().h(handler);
})
}
pub fn start(settings: HashMap<String, String>) {
let mut healthchecks: HashMap<String, Box<Healthcheck>> = HashMap::new();
let server = server::new(|| { // <-- This is where the errors happen. This closure is used to spawn threads.
vec![
build_app(healthchecks).middleware(middleware::Logger::default())
]
});
}
Cheers