We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c8c09d4 commit 31f9b51Copy full SHA for 31f9b51
src/libstd/cell.rs
@@ -13,6 +13,7 @@
13
#[missing_doc];
14
15
use cast::transmute_mut;
16
+use unstable::finally::Finally;
17
use prelude::*;
18
19
/*
@@ -65,18 +66,17 @@ impl<T> Cell<T> {
65
66
67
/// Calls a closure with a reference to the value.
68
pub fn with_ref<R>(&self, op: &fn(v: &T) -> R) -> R {
- let v = self.take();
69
- let r = op(&v);
70
- self.put_back(v);
71
- r
+ do self.with_mut_ref |ptr| { op(ptr) }
72
}
73
74
/// Calls a closure with a mutable reference to the value.
75
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
79
+ let mut v = Some(self.take());
+ do (|| {
+ op(v.get_mut_ref())
+ }).finally {
+ self.put_back(v.take_unwrap());
+ }
80
81
82
0 commit comments