Skip to content

Instantly share code, notes, and snippets.

@reusee
Created October 5, 2012 10:34
Show Gist options
  • Select an option

  • Save reusee/3839158 to your computer and use it in GitHub Desktop.

Select an option

Save reusee/3839158 to your computer and use it in GitHub Desktop.
rust message ring
use task::spawn;
use pipes::{Chan, Port, stream};
fn main() {
let mut n = 100;
let m = 1000000;
let mut (firstChan, leftPort): (Chan<int>, Port<int>) = stream();
firstChan.send(m);
let mut (rightChan, rightPort): (Chan<int>, Port<int>) = stream();
startNode(leftPort, rightChan);
loop {
leftPort <- rightPort;
let (newChan, newPort): (Chan<int>, Port<int>) = stream();
rightChan <- newChan;
rightPort <- newPort;
startNode(leftPort, rightChan);
n -= 1;
if n <= 2 {
break
}
}
startNode(rightPort, firstChan);
}
fn startNode(port: Port<int>, chan: Chan<int>) {
do spawn {
loop {
let rec = port.recv();
io::println(fmt!("%d", rec));
if rec == 0 {
chan.send(0);
break
}
chan.send(rec - 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment