Skip to content

Commit 992bb13

Browse files
committed
Auto merge of #33306 - vadixidav:master, r=alexcrichton
add implementation of Ord for Cell<T> and RefCell<T> where T: Ord Raised this in issue #33305.
2 parents ed1ece6 + 4dcb637 commit 992bb13

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

src/libcore/cell.rs

+73-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
#![stable(feature = "rust1", since = "1.0.0")]
146146

147147
use clone::Clone;
148-
use cmp::{PartialEq, Eq};
148+
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
149149
use default::Default;
150150
use marker::{Copy, Send, Sync, Sized, Unsize};
151151
use ops::{Deref, DerefMut, Drop, FnOnce, CoerceUnsized};
@@ -279,6 +279,42 @@ impl<T:PartialEq + Copy> PartialEq for Cell<T> {
279279
#[stable(feature = "cell_eq", since = "1.2.0")]
280280
impl<T:Eq + Copy> Eq for Cell<T> {}
281281

282+
#[stable(feature = "cell_ord", since = "1.10.0")]
283+
impl<T:PartialOrd + Copy> PartialOrd for Cell<T> {
284+
#[inline]
285+
fn partial_cmp(&self, other: &Cell<T>) -> Option<Ordering> {
286+
self.get().partial_cmp(&other.get())
287+
}
288+
289+
#[inline]
290+
fn lt(&self, other: &Cell<T>) -> bool {
291+
self.get() < other.get()
292+
}
293+
294+
#[inline]
295+
fn le(&self, other: &Cell<T>) -> bool {
296+
self.get() <= other.get()
297+
}
298+
299+
#[inline]
300+
fn gt(&self, other: &Cell<T>) -> bool {
301+
self.get() > other.get()
302+
}
303+
304+
#[inline]
305+
fn ge(&self, other: &Cell<T>) -> bool {
306+
self.get() >= other.get()
307+
}
308+
}
309+
310+
#[stable(feature = "cell_ord", since = "1.10.0")]
311+
impl<T:Ord + Copy> Ord for Cell<T> {
312+
#[inline]
313+
fn cmp(&self, other: &Cell<T>) -> Ordering {
314+
self.get().cmp(&other.get())
315+
}
316+
}
317+
282318
/// A mutable memory location with dynamically checked borrow rules
283319
///
284320
/// See the [module-level documentation](index.html) for more.
@@ -514,6 +550,42 @@ impl<T: ?Sized + PartialEq> PartialEq for RefCell<T> {
514550
#[stable(feature = "cell_eq", since = "1.2.0")]
515551
impl<T: ?Sized + Eq> Eq for RefCell<T> {}
516552

553+
#[stable(feature = "cell_ord", since = "1.10.0")]
554+
impl<T: ?Sized + PartialOrd> PartialOrd for RefCell<T> {
555+
#[inline]
556+
fn partial_cmp(&self, other: &RefCell<T>) -> Option<Ordering> {
557+
self.borrow().partial_cmp(&*other.borrow())
558+
}
559+
560+
#[inline]
561+
fn lt(&self, other: &RefCell<T>) -> bool {
562+
*self.borrow() < *other.borrow()
563+
}
564+
565+
#[inline]
566+
fn le(&self, other: &RefCell<T>) -> bool {
567+
*self.borrow() <= *other.borrow()
568+
}
569+
570+
#[inline]
571+
fn gt(&self, other: &RefCell<T>) -> bool {
572+
*self.borrow() > *other.borrow()
573+
}
574+
575+
#[inline]
576+
fn ge(&self, other: &RefCell<T>) -> bool {
577+
*self.borrow() >= *other.borrow()
578+
}
579+
}
580+
581+
#[stable(feature = "cell_ord", since = "1.10.0")]
582+
impl<T: ?Sized + Ord> Ord for RefCell<T> {
583+
#[inline]
584+
fn cmp(&self, other: &RefCell<T>) -> Ordering {
585+
self.borrow().cmp(&*other.borrow())
586+
}
587+
}
588+
517589
struct BorrowRef<'b> {
518590
borrow: &'b Cell<BorrowFlag>,
519591
}

0 commit comments

Comments
 (0)