Skip to content

Instantly share code, notes, and snippets.

@trimoq
Last active October 14, 2019 15:33
Show Gist options
  • Save trimoq/e3d9eaa1d8f6764e4bf50c46ad37812c to your computer and use it in GitHub Desktop.
Save trimoq/e3d9eaa1d8f6764e4bf50c46ad37812c to your computer and use it in GitHub Desktop.
// creates a mpsc-channel for thread communication.
// the type of the channel is inferred at compile-time
let (tx, rx) = mpsc::channel();
// (not shown) make the transmitter available to the other thread(s)
// ...
// start a new thread and capture the environment
thread::spawn(move || {
let mut lines = Vec::new();
// .. other variables omitted
// execute the following block for all items that are received over the channel
rx.iter().for_each(
|req| {
// modify the internal data struct `lines`, depending on the
// type of received UpdateRequest
match req {
UpdateRequest::InitialUpdate { positioned_line } => {
lines.push(positioned_line);
}
UpdateRequest::PositionUpdate { line_id, position } => {
// ...
}
UpdateRequest::TextUpdate { line_id, new_text } => {
// ...
}
UpdateRequest::SaveUpdate =>{
// ...
}
}
// save the result result
// ...
}
);
// this position will only be reached, when the channel closes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment