]> git.lizzy.rs Git - rust.git/commitdiff
Implement Eq for Cell and RefCell.
authorSimon Sapin <simon.sapin@exyr.org>
Sun, 24 May 2015 08:38:59 +0000 (10:38 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Sun, 24 May 2015 08:38:59 +0000 (10:38 +0200)
`core::cell::Cell<T>` and `core::cell::RefCell<T>` currently implement
`PartialEq` when `T` does, and just defer to comparing `T` values.
There is no reason the same shouldn’t apply to `Eq`.

This enables `#[derive(Eq, PartialEq)]` on e.g.
structs that have a `RefCell` field.

src/libcore/cell.rs

index 45a8012210417842edf6adb231defb94ebcb4410..8f531de261118b2751ea22bd07af188a52e14596 100644 (file)
 #![stable(feature = "rust1", since = "1.0.0")]
 
 use clone::Clone;
-use cmp::PartialEq;
+use cmp::{PartialEq, Eq};
 use default::Default;
 use marker::{Copy, Send, Sync, Sized};
 use ops::{Deref, DerefMut, Drop};
@@ -263,6 +263,9 @@ fn eq(&self, other: &Cell<T>) -> bool {
     }
 }
 
+#[stable(feature = "cell_eq", since = "1.2.0")]
+impl<T:Eq + Copy> Eq for Cell<T> {}
+
 /// A mutable memory location with dynamically checked borrow rules
 ///
 /// See the [module-level documentation](index.html) for more.
@@ -273,7 +276,7 @@ pub struct RefCell<T: ?Sized> {
 }
 
 /// An enumeration of values returned from the `state` method on a `RefCell<T>`.
-#[derive(Copy, Clone, PartialEq, Debug)]
+#[derive(Copy, Clone, PartialEq, Eq, Debug)]
 #[unstable(feature = "std_misc")]
 pub enum BorrowState {
     /// The cell is currently being read, there is at least one active `borrow`.
@@ -479,6 +482,9 @@ fn eq(&self, other: &RefCell<T>) -> bool {
     }
 }
 
+#[stable(feature = "cell_eq", since = "1.2.0")]
+impl<T: ?Sized + Eq> Eq for RefCell<T> {}
+
 struct BorrowRef<'b> {
     _borrow: &'b Cell<BorrowFlag>,
 }