Skip to content

Commit 4dabf3b

Browse files
committed
---
yaml --- r: 56680 b: refs/heads/try c: babe506 h: refs/heads/master v: v3
1 parent 0282c68 commit 4dabf3b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: c081ffbd1e845687202a975ea2e698b623e5722f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 79a2b2eafc3c766cecec8a5f76317693bae9ed17
5-
refs/heads/try: c08fb75dcd656707faf28264e93513da4606ea29
5+
refs/heads/try: babe50633349cd29f0a0757079e0e13bdc0310fa
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libcore/cell.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ pub impl<T> Cell<T> {
7373
self.put_back(v);
7474
r
7575
}
76+
77+
// Calls a closure with a mutable reference to the value.
78+
fn with_mut_ref<R>(&self, op: &fn(v: &mut T) -> R) -> R {
79+
let mut v = self.take();
80+
let r = op(&mut v);
81+
self.put_back(v);
82+
r
83+
}
7684
}
7785

7886
#[test]
@@ -101,3 +109,21 @@ fn test_put_back_non_empty() {
101109
let value_cell = Cell(~10);
102110
value_cell.put_back(~20);
103111
}
112+
113+
#[test]
114+
fn test_with_ref() {
115+
let good = 6;
116+
let c = Cell(~[1, 2, 3, 4, 5, 6]);
117+
let l = do c.with_ref() |v| { v.len() };
118+
assert!(l == good);
119+
}
120+
121+
#[test]
122+
fn test_with_mut_ref() {
123+
let good = ~[1, 2, 3];
124+
let mut v = ~[1, 2];
125+
let c = Cell(v);
126+
do c.with_mut_ref() |v| { v.push(3); }
127+
let v = c.take();
128+
assert!(v == good);
129+
}

0 commit comments

Comments
 (0)