Skip to content

Commit 31f9b51

Browse files
committed
Make cell with_ref/with_mut_ref use finally. Close #7975.
1 parent c8c09d4 commit 31f9b51

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/libstd/cell.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#[missing_doc];
1414

1515
use cast::transmute_mut;
16+
use unstable::finally::Finally;
1617
use prelude::*;
1718

1819
/*
@@ -65,18 +66,17 @@ impl<T> Cell<T> {
6566

6667
/// Calls a closure with a reference to the value.
6768
pub fn with_ref<R>(&self, op: &fn(v: &T) -> R) -> R {
68-
let v = self.take();
69-
let r = op(&v);
70-
self.put_back(v);
71-
r
69+
do self.with_mut_ref |ptr| { op(ptr) }
7270
}
7371

7472
/// Calls a closure with a mutable reference to the value.
7573
pub fn with_mut_ref<R>(&self, op: &fn(v: &mut T) -> R) -> R {
76-
let mut v = self.take();
77-
let r = op(&mut v);
78-
self.put_back(v);
79-
r
74+
let mut v = Some(self.take());
75+
do (|| {
76+
op(v.get_mut_ref())
77+
}).finally {
78+
self.put_back(v.take_unwrap());
79+
}
8080
}
8181
}
8282

0 commit comments

Comments
 (0)