Skip to content

Commit cf5e296

Browse files
Keegan McAllisteralexcrichton
authored andcommitted
---
yaml --- r: 151725 b: refs/heads/try2 c: 9c35ac5 h: refs/heads/master i: 151723: d514957 v: v3
1 parent c809d5a commit cf5e296

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: a0594ebb8baa0544ed0f818c494be20b3c9957a5
8+
refs/heads/try2: 9c35ac5666b62697bdd197028864fa66127a5415
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcore/cell.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@ impl<'b, T> Deref<T> for Ref<'b, T> {
186186
}
187187
}
188188

189+
/// Copy a `Ref`.
190+
///
191+
/// The `RefCell` is already immutably borrowed, so this cannot fail.
192+
///
193+
/// A `Clone` implementation would interfere with the widespread
194+
/// use of `r.borrow().clone()` to clone the contents of a `RefCell`.
195+
#[experimental]
196+
pub fn clone_ref<'b, T>(orig: &Ref<'b, T>) -> Ref<'b, T> {
197+
// Since this Ref exists, we know the borrow flag
198+
// is not set to WRITING.
199+
let borrow = orig.parent.borrow.get();
200+
debug_assert!(borrow != WRITING && borrow != UNUSED);
201+
orig.parent.borrow.set(borrow + 1);
202+
203+
Ref {
204+
parent: orig.parent,
205+
}
206+
}
207+
189208
/// Wraps a mutable borrowed reference to a value in a `RefCell` box.
190209
pub struct RefMut<'b, T> {
191210
parent: &'b RefCell<T>
@@ -307,4 +326,19 @@ mod test {
307326
let _ = _b;
308327
let _b = x.borrow_mut();
309328
}
329+
330+
#[test]
331+
fn clone_ref_updates_flag() {
332+
let x = RefCell::new(0);
333+
{
334+
let b1 = x.borrow();
335+
assert!(x.try_borrow_mut().is_none());
336+
{
337+
let _b2 = clone_ref(&b1);
338+
assert!(x.try_borrow_mut().is_none());
339+
}
340+
assert!(x.try_borrow_mut().is_none());
341+
}
342+
assert!(x.try_borrow_mut().is_some());
343+
}
310344
}

0 commit comments

Comments
 (0)