pub fn channel<T: Serialize + DeserializeOwned>() -> Result<(Sender<T>, Receiver<T>)>
Expand description
Creates a typed connected channel.
Examples found in repository?
examples/simple.rs (line 29)
11fn main() {
12 let bootstrapper = Bootstrapper::new().unwrap();
13 let path = bootstrapper.path().to_owned();
14
15 std::thread::spawn(move || {
16 let receiver = Receiver::<Task>::connect(path).unwrap();
17 loop {
18 let task = receiver.recv().unwrap();
19 match task {
20 Task::Sum(values, tx) => {
21 tx.send(values.into_iter().sum::<i64>()).unwrap();
22 }
23 Task::Shutdown => break,
24 }
25 }
26 });
27
28 println!("make channel 1");
29 let (tx, rx) = channel().unwrap();
30 bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
31 println!("result: {}", rx.recv().unwrap());
32
33 println!("make channel 2");
34 let (tx, rx) = channel().unwrap();
35 bootstrapper.send(Task::Sum(vec![1, 2, 3], tx)).unwrap();
36 println!("result: {}", rx.recv().unwrap());
37
38 bootstrapper.send(Task::Shutdown).unwrap();
39}
More examples
examples/proc.rs (line 34)
15fn main() {
16 if let Ok(path) = env::var(ENV_VAR) {
17 let receiver = Receiver::<Task>::connect(path).unwrap();
18 loop {
19 let task = receiver.recv().unwrap();
20 match dbg!(task) {
21 Task::Sum(values, tx) => {
22 tx.send(values.into_iter().sum::<i64>()).unwrap();
23 }
24 Task::Shutdown => break,
25 }
26 }
27 } else {
28 let bootstrapper = Bootstrapper::new().unwrap();
29 let mut child = process::Command::new(env::current_exe().unwrap())
30 .env(ENV_VAR, bootstrapper.path())
31 .spawn()
32 .unwrap();
33
34 let (tx, rx) = channel().unwrap();
35 bootstrapper.send(Task::Sum(vec![23, 42], tx)).unwrap();
36 println!("result: {}", rx.recv().unwrap());
37
38 let (tx, rx) = channel().unwrap();
39 bootstrapper.send(Task::Sum((0..10).collect(), tx)).unwrap();
40 println!("result: {}", rx.recv().unwrap());
41
42 bootstrapper.send(Task::Shutdown).unwrap();
43
44 child.kill().ok();
45 child.wait().ok();
46 }
47}