Skip to content

Commit a933cdf

Browse files
apasel422graydon
authored andcommitted
clean up condition module:
* use Drop trait in Guard * fix formatting * remove bare function in Condition.raise_default * use moved types instead of borrowed pointers in Condition.{raise,trap} * import common functions
1 parent 9e2a59d commit a933cdf

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed

src/libcore/condition.rs

+35-50
Original file line numberDiff line numberDiff line change
@@ -9,119 +9,109 @@
99
// except according to those terms.
1010

1111
use task;
12+
use task::local_data::{local_data_pop, local_data_set};
1213

1314
// helper for transmutation, shown below.
14-
type RustClosure = (int,int);
15+
type RustClosure = (int, int);
16+
1517
pub struct Handler<T, U> {
1618
handle: RustClosure,
1719
prev: Option<@Handler<T, U>>,
1820
}
1921

2022
pub struct Condition<T, U> {
2123
name: &static/str,
22-
key: task::local_data::LocalDataKey<Handler<T,U>>
24+
key: task::local_data::LocalDataKey<Handler<T, U>>
2325
}
2426

25-
impl<T, U> Condition<T,U> {
26-
27-
fn trap(&self, h: &self/fn(&T) ->U) -> Trap/&self<T,U> {
27+
impl<T, U> Condition<T, U> {
28+
fn trap(&self, h: &self/fn(T) -> U) -> Trap/&self<T, U> {
2829
unsafe {
2930
let p : *RustClosure = ::cast::transmute(&h);
3031
let prev = task::local_data::local_data_get(self.key);
31-
let h = @Handler{handle: *p, prev: prev};
32-
move Trap { cond: self, handler: h }
32+
let h = @Handler { handle: *p, prev: prev };
33+
Trap { cond: self, handler: h }
3334
}
3435
}
3536

36-
fn raise(t:&T) -> U {
37-
do self.raise_default(t) {
38-
fail fmt!("Unhandled condition: %s: %?",
39-
self.name,
40-
t);
41-
}
37+
fn raise(t: T) -> U {
38+
let msg = fmt!("Unhandled condition: %s: %?", self.name, t);
39+
self.raise_default(t, || fail msg)
4240
}
4341

44-
fn raise_default(t:&T, default: fn() -> U) -> U {
42+
fn raise_default(t: T, default: &fn() -> U) -> U {
4543
unsafe {
46-
match task::local_data::local_data_pop(self.key) {
44+
match local_data_pop(self.key) {
4745
None => {
4846
debug!("Condition.raise: found no handler");
4947
default()
5048
}
51-
5249
Some(handler) => {
5350
debug!("Condition.raise: found handler");
5451
match handler.prev {
55-
None => (),
56-
Some(hp) =>
57-
task::local_data::local_data_set(self.key, hp)
52+
None => {}
53+
Some(hp) => local_data_set(self.key, hp)
5854
}
59-
let handle : &fn(&T) -> U =
55+
let handle : &fn(T) -> U =
6056
::cast::transmute(handler.handle);
6157
let u = handle(t);
62-
task::local_data::local_data_set(self.key,
63-
handler);
64-
move u
58+
local_data_set(self.key, handler);
59+
u
6560
}
6661
}
6762
}
6863
}
6964
}
7065

71-
72-
7366
struct Trap<T, U> {
74-
cond: &Condition<T,U>,
67+
cond: &Condition<T, U>,
7568
handler: @Handler<T, U>
7669
}
7770

78-
impl<T, U> Trap<T,U> {
71+
impl<T, U> Trap<T, U> {
7972
fn in<V>(&self, inner: &self/fn() -> V) -> V {
8073
unsafe {
8174
let _g = Guard { cond: self.cond };
8275
debug!("Trap: pushing handler to TLS");
83-
task::local_data::local_data_set(self.cond.key, self.handler);
76+
local_data_set(self.cond.key, self.handler);
8477
inner()
8578
}
8679
}
8780
}
8881

8982
struct Guard<T, U> {
90-
cond: &Condition<T,U>,
91-
drop {
83+
cond: &Condition<T, U>
84+
}
85+
86+
impl<T, U> Guard<T, U> : Drop {
87+
fn finalize(&self) {
9288
unsafe {
9389
debug!("Guard: popping handler from TLS");
94-
let curr = task::local_data::local_data_pop(self.cond.key);
90+
let curr = local_data_pop(self.cond.key);
9591
match curr {
96-
None => (),
97-
Some(h) =>
98-
match h.prev {
99-
None => (),
100-
Some(hp) => {
101-
task::local_data::local_data_set(self.cond.key, hp)
102-
}
92+
None => {}
93+
Some(h) => match h.prev {
94+
None => {}
95+
Some(hp) => local_data_set(self.cond.key, hp)
10396
}
10497
}
10598
}
10699
}
107100
}
108101

109-
110102
#[cfg(test)]
111103
mod test {
112-
113104
condition! {
114105
sadness: int -> int;
115106
}
116107

117108
fn trouble(i: int) {
118-
debug!("trouble: raising conition");
119-
let j = sadness::cond.raise(&i);
109+
debug!("trouble: raising condition");
110+
let j = sadness::cond.raise(i);
120111
debug!("trouble: handler recovered with %d", j);
121112
}
122113

123114
fn nested_trap_test_inner() {
124-
125115
let mut inner_trapped = false;
126116

127117
do sadness::cond.trap(|_j| {
@@ -138,7 +128,6 @@ mod test {
138128

139129
#[test]
140130
fn nested_trap_test_outer() {
141-
142131
let mut outer_trapped = false;
143132

144133
do sadness::cond.trap(|_j| {
@@ -154,15 +143,14 @@ mod test {
154143
}
155144

156145
fn nested_reraise_trap_test_inner() {
157-
158146
let mut inner_trapped = false;
159147

160148
do sadness::cond.trap(|_j| {
161149
debug!("nested_reraise_trap_test_inner: in handler");
162150
inner_trapped = true;
163151
let i = 10;
164152
debug!("nested_reraise_trap_test_inner: handler re-raising");
165-
sadness::cond.raise(&i)
153+
sadness::cond.raise(i)
166154
}).in {
167155
debug!("nested_reraise_trap_test_inner: in protected block");
168156
trouble(1);
@@ -173,7 +161,6 @@ mod test {
173161

174162
#[test]
175163
fn nested_reraise_trap_test_outer() {
176-
177164
let mut outer_trapped = false;
178165

179166
do sadness::cond.trap(|_j| {
@@ -189,18 +176,16 @@ mod test {
189176

190177
#[test]
191178
fn test_default() {
192-
193179
let mut trapped = false;
194180

195181
do sadness::cond.trap(|j| {
196182
debug!("test_default: in handler");
197-
sadness::cond.raise_default(j, || {trapped=true; 5})
183+
sadness::cond.raise_default(j, || { trapped=true; 5 })
198184
}).in {
199185
debug!("test_default: in protected block");
200186
trouble(1);
201187
}
202188

203189
assert trapped;
204190
}
205-
206191
}

0 commit comments

Comments
 (0)