On 04/21/2013 05:17 AM, Alexander Stavonin wrote:
I'm trying to understand the idea of DuplexStream. First of all, it can't be 
used for cross-task communications:

  13     let value = gen_data();
  14     let (server, client) = DuplexStream();
  15
  16     do task::spawn {
  17         let val: ~[uint] = server.recv();
  18         io::println(fmt!("Value: %?", val));
  19         let res = val.map(|v| {v+1});
  20         client.send(res)
  21     }
  22
  23     server.send(value);
  24     io::println(fmt!("Resilt: %?", client.recv()));

because of error: "21:5 note: `server` moved into closure environment here because 
its type is moved by default".

In library sources I found example which passes messages inside one task, what 
IMHO is not too useful.
Would you please provide a brief explanation of DuplexStream usage scenario?
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Each end can both send and receive, so only one end needs to be moved into the closure:

let (server, client) = DuplexStream();

do spawn {
  let val = server.recv();
  let res = val.map(|v| v + 1);
  server.send(res);
}

client.send(value);
let res = client.recv();
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to