Last active
August 29, 2015 14:06
-
-
Save krdln/17e4478aa943c3c66a42 to your computer and use it in GitHub Desktop.
Simple fork-join concurrency in Rust.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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