Skip to content

Instantly share code, notes, and snippets.

Created June 20, 2015 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/23a6c38a9b08a2e76167 to your computer and use it in GitHub Desktop.
Save anonymous/23a6c38a9b08a2e76167 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::sync::mpsc::{channel, Sender};
use std::sync::Mutex;
use std::thread;
#[derive(Debug)]
enum Message { A, B }
struct Request;
struct Response;
struct IronError;
type IronResult<T> = Result<T, IronError>;
// copied from Iron
trait Handler: Send + Sync {
fn handle(&self, &mut Request) -> IronResult<Response>;
}
impl<F> Handler for F where F: Send + Sync + Fn(&mut Request) -> IronResult<Response> {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
(*self)(req)
}
}
fn make_handler(tx: Sender<Message>) -> Box<Handler> {
let mtx = Mutex::new(tx);
Box::new(move |req: &mut Request| { mtx.lock().unwrap().send(Message::A); Ok(Response) })
}
fn iron_setup(tx: Sender<Message>) {
let handler = make_handler(tx);
handler.handle(&mut Request);
}
fn main() {
let (tx, rx) = channel::<Message>();
thread::spawn(move || iron_setup(tx.clone()));
println!("{:?}", rx.recv());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment