2012-10-17 18:40:18 -05:00
|
|
|
// helper for transmutation, shown below.
|
|
|
|
type RustClosure = (int,int);
|
2012-10-18 20:45:33 -05:00
|
|
|
struct Handler<T, U:Copy> {
|
2012-10-22 19:26:26 -05:00
|
|
|
handle: RustClosure,
|
2012-10-18 21:47:19 -05:00
|
|
|
prev: Option<@Handler<T, U>>,
|
2012-10-19 16:46:32 -05:00
|
|
|
}
|
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
struct Condition<T, U:Copy> {
|
|
|
|
key: task::local_data::LocalDataKey<Handler<T,U>>
|
2012-10-19 16:46:32 -05:00
|
|
|
}
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
impl<T, U: Copy> Condition<T,U> {
|
2012-10-17 18:40:18 -05:00
|
|
|
|
2012-10-19 16:46:32 -05:00
|
|
|
fn trap(&self, h: &self/fn(&T) ->U) -> Trap/&self<T,U> {
|
|
|
|
unsafe {
|
|
|
|
let p : *RustClosure = ::cast::transmute(&h);
|
2012-10-22 19:26:26 -05:00
|
|
|
let prev = task::local_data::local_data_get(self.key);
|
|
|
|
let h = @Handler{handle: *p, prev: prev};
|
2012-10-19 16:46:32 -05:00
|
|
|
move Trap { cond: self, handler: h }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 20:45:33 -05:00
|
|
|
fn raise(t:&T) -> U {
|
2012-10-17 18:40:18 -05:00
|
|
|
unsafe {
|
2012-10-22 19:26:26 -05:00
|
|
|
match task::local_data::local_data_pop(self.key) {
|
2012-10-18 14:27:09 -05:00
|
|
|
None => {
|
|
|
|
debug!("Condition.raise: found no handler");
|
|
|
|
fail
|
|
|
|
}
|
|
|
|
|
2012-10-17 18:40:18 -05:00
|
|
|
Some(handler) => {
|
2012-10-18 14:27:09 -05:00
|
|
|
debug!("Condition.raise: found handler");
|
2012-10-22 19:26:26 -05:00
|
|
|
match handler.prev {
|
|
|
|
None => (),
|
|
|
|
Some(hp) =>
|
|
|
|
task::local_data::local_data_set(self.key, hp)
|
|
|
|
}
|
|
|
|
let handle : &fn(&T) -> U =
|
|
|
|
::cast::transmute(handler.handle);
|
|
|
|
let u = handle(t);
|
|
|
|
task::local_data::local_data_set(self.key,
|
|
|
|
handler);
|
|
|
|
move u
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
struct Trap<T, U:Copy> {
|
|
|
|
cond: &Condition<T,U>,
|
|
|
|
handler: @Handler<T, U>
|
2012-10-17 18:40:18 -05:00
|
|
|
}
|
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
impl<T, U: Copy> Trap<T,U> {
|
|
|
|
fn in<V: Copy>(&self, inner: &self/fn() -> V) -> V {
|
|
|
|
unsafe {
|
|
|
|
let _g = Guard { cond: self.cond };
|
|
|
|
debug!("Trap: pushing handler to TLS");
|
|
|
|
task::local_data::local_data_set(self.cond.key, self.handler);
|
|
|
|
inner()
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 14:27:09 -05:00
|
|
|
}
|
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
struct Guard<T, U:Copy> {
|
|
|
|
cond: &Condition<T,U>,
|
|
|
|
drop {
|
|
|
|
unsafe {
|
|
|
|
debug!("Guard: popping handler from TLS");
|
|
|
|
let curr = task::local_data::local_data_pop(self.cond.key);
|
|
|
|
match curr {
|
|
|
|
None => (),
|
|
|
|
Some(h) =>
|
|
|
|
match h.prev {
|
|
|
|
None => (),
|
|
|
|
Some(hp) => {
|
|
|
|
task::local_data::local_data_set(self.cond.key, hp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-18 14:27:09 -05:00
|
|
|
}
|
2012-10-18 21:47:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2012-10-22 19:26:26 -05:00
|
|
|
mod test {
|
|
|
|
|
|
|
|
fn sadness_key(_x: @Handler<int,int>) { }
|
|
|
|
fn trouble(i: int) {
|
|
|
|
// Condition should work as a const, just limitations in consts.
|
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
|
|
|
debug!("trouble: raising conition");
|
|
|
|
let j = sadness_condition.raise(&i);
|
|
|
|
debug!("trouble: handler recovered with %d", j);
|
|
|
|
}
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
fn nested_trap_test_inner() {
|
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
let mut inner_trapped = false;
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
do sadness_condition.trap(|_j| {
|
|
|
|
debug!("nested_trap_test_inner: in handler");
|
|
|
|
inner_trapped = true;
|
|
|
|
0
|
|
|
|
}).in {
|
|
|
|
debug!("nested_trap_test_inner: in protected block");
|
|
|
|
trouble(1);
|
|
|
|
}
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
assert inner_trapped;
|
|
|
|
}
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
#[test]
|
|
|
|
fn nested_trap_test_outer() {
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
let mut outer_trapped = false;
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
do sadness_condition.trap(|_j| {
|
|
|
|
debug!("nested_trap_test_outer: in handler");
|
|
|
|
outer_trapped = true; 0
|
|
|
|
}).in {
|
|
|
|
debug!("nested_guard_test_outer: in protected block");
|
|
|
|
nested_trap_test_inner();
|
|
|
|
trouble(1);
|
|
|
|
}
|
2012-10-18 21:47:19 -05:00
|
|
|
|
2012-10-19 16:46:32 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
assert outer_trapped;
|
|
|
|
}
|
2012-10-19 16:46:32 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
fn nested_reraise_trap_test_inner() {
|
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
|
|
|
|
|
|
|
let mut inner_trapped = false;
|
|
|
|
|
|
|
|
do sadness_condition.trap(|_j| {
|
|
|
|
debug!("nested_reraise_trap_test_inner: in handler");
|
|
|
|
inner_trapped = true;
|
|
|
|
let i = 10;
|
|
|
|
debug!("nested_reraise_trap_test_inner: handler re-raising");
|
|
|
|
sadness_condition.raise(&i)
|
|
|
|
}).in {
|
|
|
|
debug!("nested_reraise_trap_test_inner: in protected block");
|
|
|
|
trouble(1);
|
|
|
|
}
|
2012-10-19 16:46:32 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
assert inner_trapped;
|
2012-10-19 16:46:32 -05:00
|
|
|
}
|
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
#[test]
|
|
|
|
fn nested_reraise_trap_test_outer() {
|
|
|
|
|
|
|
|
let sadness_condition : Condition<int,int> =
|
|
|
|
Condition { key: sadness_key };
|
2012-10-19 16:46:32 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
let mut outer_trapped = false;
|
2012-10-19 16:46:32 -05:00
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
do sadness_condition.trap(|_j| {
|
|
|
|
debug!("nested_reraise_trap_test_outer: in handler");
|
|
|
|
outer_trapped = true; 0
|
|
|
|
}).in {
|
|
|
|
debug!("nested_reraise_trap_test_outer: in protected block");
|
|
|
|
nested_reraise_trap_test_inner();
|
|
|
|
}
|
2012-10-19 16:46:32 -05:00
|
|
|
|
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
assert outer_trapped;
|
2012-10-19 16:46:32 -05:00
|
|
|
}
|
|
|
|
|
2012-10-22 19:26:26 -05:00
|
|
|
}
|