Hyper has the following example of a Handler
that implements Sync
:
use std::sync::Mutex;
use std::sync::mpsc::{channel, Sender};
use hyper::server::{Handler, Server, Request, Response};
struct SenderHandler {
sender: Mutex<Sender<&'static str>>
}
impl Handler for SenderHandler {
fn handle(&self, req: Request, res: Response) {
self.sender.lock().unwrap().send("start").unwrap();
}
}
and states that a Handler
must implement Sync
, since Handler
can be called from different threads.
To me, this sounds like an unnecessary performance penalty. What I would prefer would be to setup one SenderHandler
per thread, each being independent, which would remove the requirement on implementing Sync
.
Have I misunderstood Hyper, Rust's type-system or is this not possible?