Skip to content

Instantly share code, notes, and snippets.

@AlexTalker
Forked from anonymous/playground.rs
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexTalker/526f9ccd4c10085405d1 to your computer and use it in GitHub Desktop.
Save AlexTalker/526f9ccd4c10085405d1 to your computer and use it in GitHub Desktop.
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