Skip to content

Instantly share code, notes, and snippets.

@krdln
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krdln/17e4478aa943c3c66a42 to your computer and use it in GitHub Desktop.
Save krdln/17e4478aa943c3c66a42 to your computer and use it in GitHub Desktop.
Simple fork-join concurrency in Rust.
#![feature(unboxed_closures, unboxed_closure_sugar)]
use std::rt::thread::Thread;
use std::kinds::marker::InvariantType;
use std::mem::transmute;
fn main() {
let mut arr = [0, 1, 2, 3, 4];
let _ = { // change _ to _bg and it fails to compile!
let mut handle = arr.mut_iter(); // ref|:| ICEd rustc
async(|:| for x in handle { *x /= 2; })
};
assert_eq!(arr[4], 2i);
}
pub struct Task<C> {
borrow_: InvariantType<C>,
_thread: Thread<()>
}
pub fn async<C: FnOnce<(),()> + Sync>(closure: C) -> Task<C> {
unsafe {
let stripped: *mut () = transmute(box closure);
Task {
borrow_: InvariantType,
_thread: Thread::start( proc() {
let box closure: Box<C> = transmute(stripped);
closure.call_once(());
} )
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment