提交 cb943b7d 编写于 作者: B bors

auto merge of #18212 : kmcallister/rust/unsafecell, r=thestinger

Fixes #18131.
......@@ -191,6 +191,17 @@ pub fn set(&self, value: T) {
*self.value.get() = value;
}
}
/// Get a reference to the underlying `UnsafeCell`.
///
/// This can be used to circumvent `Cell`'s safety checks.
///
/// This function is `unsafe` because `UnsafeCell`'s field is public.
#[inline]
#[experimental]
pub unsafe fn as_unsafe_cell<'a>(&'a self) -> &'a UnsafeCell<T> {
&self.value
}
}
#[unstable = "waiting for `Clone` trait to become stable"]
......@@ -306,6 +317,17 @@ pub fn borrow_mut<'a>(&'a self) -> RefMut<'a, T> {
None => fail!("RefCell<T> already borrowed")
}
}
/// Get a reference to the underlying `UnsafeCell`.
///
/// This can be used to circumvent `RefCell`'s safety checks.
///
/// This function is `unsafe` because `UnsafeCell`'s field is public.
#[inline]
#[experimental]
pub unsafe fn as_unsafe_cell<'a>(&'a self) -> &'a UnsafeCell<T> {
&self.value
}
}
#[unstable = "waiting for `Clone` to become stable"]
......
......@@ -127,3 +127,22 @@ fn clone_ref_updates_flag() {
}
assert!(x.try_borrow_mut().is_some());
}
#[test]
fn as_unsafe_cell() {
let c1: Cell<uint> = Cell::new(0u);
c1.set(1u);
assert_eq!(1u, unsafe { *c1.as_unsafe_cell().get() });
let c2: Cell<uint> = Cell::new(0u);
unsafe { *c2.as_unsafe_cell().get() = 1u; }
assert_eq!(1u, c2.get());
let r1: RefCell<uint> = RefCell::new(0u);
*r1.borrow_mut() = 1u;
assert_eq!(1u, unsafe { *r1.as_unsafe_cell().get() });
let r2: RefCell<uint> = RefCell::new(0u);
unsafe { *r2.as_unsafe_cell().get() = 1u; }
assert_eq!(1u, *r2.borrow());
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册