-
-
Save AlexTalker/526f9ccd4c10085405d1 to your computer and use it in GitHub Desktop.
This file contains 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