Skip to content

Instantly share code, notes, and snippets.

@gavinb
Created March 26, 2014 05:37
Show Gist options
  • Save gavinb/9777418 to your computer and use it in GitHub Desktop.
Save gavinb/9777418 to your computer and use it in GitHub Desktop.
Trying to share channels
extern crate native;
use std::comm::{Sender, Receiver, TryRecvResult, Data, channel};
struct Master<'a> {
chan_wc_to_engine: Sender<uint>,
chan_wc_from_engine: Receiver<uint>,
chan_engine_to_wc: Sender<uint>,
chan_engine_from_wc: Receiver<uint>,
}
impl<'a> Master<'a> {
fn new() -> Master<'a> {
let (chan_wc_to_engine, chan_engine_from_wc) = channel();
let (chan_engine_to_wc, chan_wc_from_engine) = channel();
let master = Master{ chan_wc_to_engine: chan_wc_to_engine,
chan_wc_from_engine: chan_wc_from_engine,
chan_engine_to_wc: chan_engine_to_wc,
chan_engine_from_wc: chan_engine_from_wc,
};
master
}
fn start_engine(&self) {
println!("start_engine");
native::task::spawn( proc() {
println!("process");
self.chan_engine_to_wc.send(42); // ***
println!("done");
});
}
fn listen(&self) {
println!("listen");
let indata = self.chan_wc_from_engine.recv();
println!("indata = {}", indata);
}
}
fn main() {
let m = Master::new();
m.start_engine();
m.listen();
println!("fin");
}
@gavinb
Copy link
Author

gavinb commented Mar 26, 2014

This fails to compile with the error:

mrb.rs:35:13: 35:17 error: cannot capture variable of type `&Master<'a>`, which does not fulfill `Send`, in a bounded closure
mrb.rs:35             self.chan_engine_to_wc.send(42); // ***
                      ^~~~
mrb.rs:35:13: 35:17 note: this closure's environment must satisfy `Send`
mrb.rs:35             self.chan_engine_to_wc.send(42); // ***
                      ^~~~
error: aborting due to previous error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment