Skip to content

Commit

Permalink
Make cell with_ref/with_mut_ref use finally. Close #7975.
Browse files Browse the repository at this point in the history
  • Loading branch information
bblum committed Aug 12, 2013
1 parent c8c09d4 commit 31f9b51
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/libstd/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#[missing_doc];

use cast::transmute_mut;
use unstable::finally::Finally;
use prelude::*;

/*
Expand Down Expand Up @@ -65,18 +66,17 @@ impl<T> Cell<T> {

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

/// Calls a closure with a mutable reference to the value.
pub fn with_mut_ref<R>(&self, op: &fn(v: &mut T) -> R) -> R {
let mut v = self.take();
let r = op(&mut v);
self.put_back(v);
r
let mut v = Some(self.take());
do (|| {
op(v.get_mut_ref())
}).finally {
self.put_back(v.take_unwrap());
}
}
}

Expand Down

0 comments on commit 31f9b51

Please sign in to comment.