Skip to content

Instantly share code, notes, and snippets.

@gterzian
Last active August 7, 2018 22:00
Show Gist options
  • Save gterzian/4130d85391e2ead5c98e55b5e4e9205e to your computer and use it in GitHub Desktop.
Save gterzian/4130d85391e2ead5c98e55b5e4e9205e to your computer and use it in GitHub Desktop.
fn square(merge_chan: Sender<PipelineMsg>) -> Sender<PipelineMsg> {
let (chan, port) = channel();
let _ = thread::Builder::new().spawn(move || {
// Iteration will stop,
// once the sender returned by "square" is dropped.
for msg in port {
let num = match msg {
PipelineMsg::Generated(num) => num,
_ => panic!("unexpected message receiving at square stage"),
};
// Sending squared numbers on to the "merge" stage...
let _ = merge_chan.send(PipelineMsg::Squared(num * num));
}
});
// Sharing our sender with the world...
chan
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment