Created
June 20, 2015 15:01
-
-
Save anonymous/23a6c38a9b08a2e76167 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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