From a605416bb471652768a28f9fc5d54f0327f6d770 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Mon, 22 Oct 2012 18:05:17 -0700 Subject: [PATCH] core: add condition.raise_default. --- src/libcore/condition.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/libcore/condition.rs b/src/libcore/condition.rs index e50ebfc439b..dd5f9752137 100644 --- a/src/libcore/condition.rs +++ b/src/libcore/condition.rs @@ -20,12 +20,18 @@ fn trap(&self, h: &self/fn(&T) ->U) -> Trap/&self { } } - fn raise(t:&T) -> U { + fn raise(t:&T) -> U { + do self.raise_default(t) { + fail ~"Unhandled condition"; + } + } + + fn raise_default(t:&T, default: fn() -> U) -> U { unsafe { match task::local_data::local_data_pop(self.key) { None => { debug!("Condition.raise: found no handler"); - fail + default() } Some(handler) => { @@ -134,7 +140,6 @@ fn nested_trap_test_outer() { trouble(1); } - assert outer_trapped; } @@ -174,8 +179,25 @@ fn nested_reraise_trap_test_outer() { nested_reraise_trap_test_inner(); } - assert outer_trapped; } -} \ No newline at end of file + #[test] + fn test_default() { + let sadness_condition : Condition = + Condition { key: sadness_key }; + + let mut trapped = false; + + do sadness_condition.trap(|j| { + debug!("test_default: in handler"); + sadness_condition.raise_default(j, || {trapped=true; 5}) + }).in { + debug!("test_default: in protected block"); + trouble(1); + } + + assert trapped; + } + +}