Make cell with_ref/with_mut_ref use finally. Close #7975.
This commit is contained in:
parent
c8c09d40fc
commit
31f9b51592
1 changed files with 8 additions and 8 deletions
|
@ -13,6 +13,7 @@
|
||||||
#[missing_doc];
|
#[missing_doc];
|
||||||
|
|
||||||
use cast::transmute_mut;
|
use cast::transmute_mut;
|
||||||
|
use unstable::finally::Finally;
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -65,18 +66,17 @@ impl<T> Cell<T> {
|
||||||
|
|
||||||
/// Calls a closure with a reference to the value.
|
/// Calls a closure with a reference to the value.
|
||||||
pub fn with_ref<R>(&self, op: &fn(v: &T) -> R) -> R {
|
pub fn with_ref<R>(&self, op: &fn(v: &T) -> R) -> R {
|
||||||
let v = self.take();
|
do self.with_mut_ref |ptr| { op(ptr) }
|
||||||
let r = op(&v);
|
|
||||||
self.put_back(v);
|
|
||||||
r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calls a closure with a mutable reference to the value.
|
/// Calls a closure with a mutable reference to the value.
|
||||||
pub fn with_mut_ref<R>(&self, op: &fn(v: &mut T) -> R) -> R {
|
pub fn with_mut_ref<R>(&self, op: &fn(v: &mut T) -> R) -> R {
|
||||||
let mut v = self.take();
|
let mut v = Some(self.take());
|
||||||
let r = op(&mut v);
|
do (|| {
|
||||||
self.put_back(v);
|
op(v.get_mut_ref())
|
||||||
r
|
}).finally {
|
||||||
|
self.put_back(v.take_unwrap());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue