libstd: Add a function to borrow a cell

This commit is contained in:
Patrick Walton 2012-08-16 19:36:49 -07:00
parent 0e3825d38c
commit 6d7b143036

View File

@ -39,6 +39,13 @@ impl<T> Cell<T> {
fn is_empty() -> bool {
self.value.is_none()
}
// Calls a closure with a reference to the value.
fn with_ref(f: fn(v: &T)) {
let val = move self.take();
f(&val);
self.put_back(move val);
}
}
#[test]
@ -66,4 +73,4 @@ fn test_take_empty() {
fn test_put_back_non_empty() {
let value_cell = Cell(~10);
value_cell.put_back(~20);
}
}