From 6d7b143036e1d61c8f58864db3445d2a0ae4ad11 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 16 Aug 2012 19:36:49 -0700 Subject: [PATCH] libstd: Add a function to borrow a cell --- src/libstd/cell.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 02cae69eca5..3d8f63c26cd 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -39,6 +39,13 @@ impl Cell { 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); -} \ No newline at end of file +}