Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ impl<T:Copy> Cell<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"]
Expand Down Expand Up @@ -306,6 +317,17 @@ impl<T> RefCell<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"]
Expand Down
19 changes: 19 additions & 0 deletions src/libcoretest/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}