Skip to content

Commit

Permalink
Add as_unsafe_cell() for Cell and RefCell
Browse files Browse the repository at this point in the history
Fixes #18131.
  • Loading branch information
Keegan McAllister committed Oct 24, 2014
1 parent a10917a commit 7317ef5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/libcore/cell.rs
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
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());
}

9 comments on commit 7317ef5

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from thestinger
at kmcallister@7317ef5

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging kmcallister/rust/unsafecell = 7317ef5 into auto

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kmcallister/rust/unsafecell = 7317ef5 merged ok, testing candidate = 90fa5c10

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from thestinger
at kmcallister@7317ef5

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging kmcallister/rust/unsafecell = 7317ef5 into auto

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kmcallister/rust/unsafecell = 7317ef5 merged ok, testing candidate = cb943b7

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 7317ef5 Oct 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = cb943b7

Please sign in to comment.