Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more methods in Cell for non-Copy types #39715

Closed
wants to merge 2 commits into from
Closed
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
109 changes: 107 additions & 2 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ use fmt::{self, Debug, Display};
use marker::Unsize;
use mem;
use ops::{Deref, DerefMut, CoerceUnsized};
use ptr;

/// A mutable memory location.
///
Expand Down Expand Up @@ -280,6 +281,19 @@ unsafe impl<T> Send for Cell<T> where T: Send {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> !Sync for Cell<T> {}

#[unstable(feature = "move_cell", issue = "39264")]
impl<T:Clone> Clone for Cell<T> {
#[inline]
default fn clone(&self) -> Cell<T> {
// convert `*mut T` to `&T`, because we can ensure that
// there are no mutations or mutable aliases going on when casting to &T.
let inner : &T = unsafe { self.value.get().as_ref().unwrap() };
// If `clone` panics, the `Cell` itself is in a valid state.
let another = inner.clone();
Cell::new(another)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T:Copy> Clone for Cell<T> {
#[inline]
Expand All @@ -289,14 +303,26 @@ impl<T:Copy> Clone for Cell<T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T:Default + Copy> Default for Cell<T> {
impl<T:Default> Default for Cell<T> {
/// Creates a `Cell<T>`, with the `Default` value for T.
#[inline]
fn default() -> Cell<T> {
Cell::new(Default::default())
}
}

#[unstable(feature = "move_cell", issue = "39264")]
impl<T:PartialEq> PartialEq for Cell<T> {
#[inline]
default fn eq(&self, other: &Cell<T>) -> bool {
if ptr::eq(self, other) {
true
} else {
unsafe { Cell::take_and_restore(self, other, |l, r| l == r) }
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T:PartialEq + Copy> PartialEq for Cell<T> {
#[inline]
Expand All @@ -305,9 +331,60 @@ impl<T:PartialEq + Copy> PartialEq for Cell<T> {
}
}

#[unstable(feature = "move_cell", issue = "39264")]
impl<T:Eq> Eq for Cell<T> {}

#[stable(feature = "cell_eq", since = "1.2.0")]
impl<T:Eq + Copy> Eq for Cell<T> {}

#[unstable(feature = "move_cell", issue = "39264")]
impl<T:PartialOrd> PartialOrd for Cell<T> {
#[inline]
default fn partial_cmp(&self, other: &Cell<T>) -> Option<Ordering> {
if ptr::eq(self, other) {
Some(Ordering::Equal)
} else {
unsafe { Cell::take_and_restore(self, other, |l, r| l.partial_cmp(r)) }
}
}

#[inline]
default fn lt(&self, other: &Cell<T>) -> bool {
if ptr::eq(self, other) {
true
} else {
unsafe { Cell::take_and_restore(self, other, |l, r| l < r) }
}
}

#[inline]
default fn le(&self, other: &Cell<T>) -> bool {
if ptr::eq(self, other) {
true
} else {
unsafe { Cell::take_and_restore(self, other, |l, r| l <= r) }
}
}

#[inline]
default fn gt(&self, other: &Cell<T>) -> bool {
if ptr::eq(self, other) {
true
} else {
unsafe { Cell::take_and_restore(self, other, |l, r| l > r) }
}
}

#[inline]
default fn ge(&self, other: &Cell<T>) -> bool {
if ptr::eq(self, other) {
true
} else {
unsafe { Cell::take_and_restore(self, other, |l, r| l >= r) }
}
}
}

#[stable(feature = "cell_ord", since = "1.10.0")]
impl<T:PartialOrd + Copy> PartialOrd for Cell<T> {
#[inline]
Expand Down Expand Up @@ -336,6 +413,18 @@ impl<T:PartialOrd + Copy> PartialOrd for Cell<T> {
}
}

#[unstable(feature = "move_cell", issue = "39264")]
impl<T:Ord> Ord for Cell<T> {
#[inline]
default fn cmp(&self, other: &Cell<T>) -> Ordering {
if ptr::eq(self, other) {
Ordering::Equal
} else {
unsafe { Cell::take_and_restore(self, other, |l, r| l.cmp(r)) }
}
}
}

#[stable(feature = "cell_ord", since = "1.10.0")]
impl<T:Ord + Copy> Ord for Cell<T> {
#[inline]
Expand All @@ -345,7 +434,7 @@ impl<T:Ord + Copy> Ord for Cell<T> {
}

#[stable(feature = "cell_from", since = "1.12.0")]
impl<T: Copy> From<T> for Cell<T> {
impl<T> From<T> for Cell<T> {
fn from(t: T) -> Cell<T> {
Cell::new(t)
}
Expand Down Expand Up @@ -422,6 +511,22 @@ impl<T> Cell<T> {
pub fn into_inner(self) -> T {
unsafe { self.value.into_inner() }
}

/// Private function used for implementing other methods when `T` is not `Copy`.
///
/// This is `unsafe` because `left` and `right` may point to the same object.
#[unstable(feature = "move_cell", issue = "39264")]
unsafe fn take_and_restore<Result, F>(left: &Self, right: &Self, f: F) -> Result
where F: FnOnce(&T, &T) -> Result
{
// convert `*mut T` to `&T`, because we can ensure that
// there are no mutations or mutable aliases going on when casting to &T.
let lhs = left.value.get().as_ref().unwrap();
let rhs = right.value.get().as_ref().unwrap();
// If panic happens in `f`, both `Cell`s are in a valid state.
let result = f(lhs, rhs);
return result;
}
}

impl<T: Default> Cell<T> {
Expand Down