Created
June 4, 2014 07:04
-
-
Save wycats/bc558ed596a49cb6f365 to your computer and use it in GitHub Desktop.
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
extern crate debug; | |
extern crate native; | |
use std::rt::task::{Task, Execute}; | |
use std::any::Any; | |
use std::rt::task::BlockedTask; | |
use std::task::TaskOpts; | |
use std::rt::rtio::LocalIo; | |
use native::io::IoFactory; | |
#[deriving(Show)] | |
struct Inner { | |
text: String | |
} | |
#[deriving(Show)] | |
struct Args<'a> { | |
fail: bool, | |
first: String, | |
second: Vec<&'static str>, | |
reference: Option<&'a Inner> | |
} | |
#[start] | |
fn main(_: int, _: **u8) -> int { | |
let inner = Inner { text: "Heyo".to_str() }; | |
let mut args = Some(box Args { fail: false, first: "hello".to_str(), second: vec!("world", "and", "stuff"), reference: Some(&inner) }); | |
let mut fail = Some(box Args { fail: true, first: "hello".to_str(), second: vec!(), reference: None }); | |
println!("{}", task_run(|| testing(args.take_unwrap()))); | |
println!("{}", task_run(|| testing(fail.take_unwrap()))); | |
0 | |
} | |
fn testing(args: &Args) { | |
if args.fail { fail!("YOLO BROLO"); } | |
println!("{}", args); | |
} | |
fn task_run(closure: ||) -> String { | |
let (tx, rx) = channel(); | |
let mut t = task(); | |
t.death.on_exit = Some(Execute(proc(r) { | |
tx.send(r.map_err(|e| cause_str(&e))); | |
})); | |
t.run(closure); | |
format!("{}", rx.recv()) | |
} | |
fn task() -> Box<Task> { | |
let mut t = Task::new(); | |
t.put_runtime(box IoWrapper::new() as Box<std::rt::Runtime:Send>); | |
box t | |
} | |
fn cause_str(reason: &Box<Any:Send>) -> String { | |
use std::any::AnyRefExt; | |
match reason.as_ref::<&'static str>() { | |
Some(s) => *s, | |
None => match reason.as_ref::<String>() { | |
Some(s) => s.as_slice(), | |
None => "Box<Any>", | |
} | |
}.to_str() | |
} | |
struct IoWrapper { | |
io_factory: native::io::IoFactory | |
} | |
impl IoWrapper { | |
pub fn new() -> IoWrapper { | |
IoWrapper { io_factory: native::io::IoFactory::new() } | |
} | |
} | |
#[allow(unused_variable)] | |
impl std::rt::Runtime for IoWrapper { | |
fn yield_now(~self, cur_task: Box<Task>) { unimplemented!() } | |
fn maybe_yield(~self, cur_task: Box<Task>) { unimplemented!() } | |
fn deschedule(~self, times: uint, cur_task: Box<Task>, f: |BlockedTask| -> Result<(), BlockedTask>) { unimplemented!() } | |
fn reawaken(~self, to_wake: Box<Task>) { unimplemented!() } | |
fn spawn_sibling(~self, cur_task: Box<Task>, opts: TaskOpts, f: proc(): Send) { unimplemented!() } | |
fn stack_bounds(&self) -> (uint, uint) { unimplemented!() } | |
fn can_block(&self) -> bool { unimplemented!() } | |
fn wrap(~self) -> Box<Any> { unimplemented!() } | |
fn local_io<'a>(&'a mut self) -> Option<LocalIo<'a>> { | |
Some(LocalIo::new(&mut self.io_factory as &mut std::rt::rtio::IoFactory)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment