From 4c3b42271ba74ff905dd722e6480701713408d60 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 30 Oct 2014 15:33:47 -0500 Subject: [PATCH] DSTify Box implementation of PartialEq, PartialOrd, Eq, Ord --- src/liballoc/boxed.rs | 40 +++++++++++++++++++ .../run-pass/deriving-eq-ord-boxed-slice.rs | 24 +++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/test/run-pass/deriving-eq-ord-boxed-slice.rs diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index de98bc63183a2..d1fc921ffdab5 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -61,12 +61,16 @@ impl Clone for Box { } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] impl PartialEq for Box { #[inline] fn eq(&self, other: &Box) -> bool { *(*self) == *(*other) } #[inline] fn ne(&self, other: &Box) -> bool { *(*self) != *(*other) } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] impl PartialOrd for Box { #[inline] fn partial_cmp(&self, other: &Box) -> Option { @@ -81,14 +85,50 @@ impl PartialOrd for Box { #[inline] fn gt(&self, other: &Box) -> bool { *(*self) > *(*other) } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] impl Ord for Box { #[inline] fn cmp(&self, other: &Box) -> Ordering { (**self).cmp(&**other) } } +// NOTE(stage0): remove impl after a snapshot +#[cfg(stage0)] impl Eq for Box {} +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +impl PartialEq for Box { + #[inline] + fn eq(&self, other: &Box) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &Box) -> bool { PartialEq::ne(&**self, &**other) } +} +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +impl PartialOrd for Box { + #[inline] + fn partial_cmp(&self, other: &Box) -> Option { + PartialOrd::partial_cmp(&**self, &**other) + } + #[inline] + fn lt(&self, other: &Box) -> bool { PartialOrd::lt(&**self, &**other) } + #[inline] + fn le(&self, other: &Box) -> bool { PartialOrd::le(&**self, &**other) } + #[inline] + fn ge(&self, other: &Box) -> bool { PartialOrd::ge(&**self, &**other) } + #[inline] + fn gt(&self, other: &Box) -> bool { PartialOrd::gt(&**self, &**other) } +} +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +impl Ord for Box { + #[inline] + fn cmp(&self, other: &Box) -> Ordering { + Ord::cmp(&**self, &**other) + } +} +#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot +impl Eq for Box {} + /// Extension methods for an owning `Any` trait object. #[unstable = "post-DST and coherence changes, this will not be a trait but \ rather a direct `impl` on `Box`"] diff --git a/src/test/run-pass/deriving-eq-ord-boxed-slice.rs b/src/test/run-pass/deriving-eq-ord-boxed-slice.rs new file mode 100644 index 0000000000000..b16c2ccb46eb8 --- /dev/null +++ b/src/test/run-pass/deriving-eq-ord-boxed-slice.rs @@ -0,0 +1,24 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[deriving(PartialEq, PartialOrd, Eq, Ord)] +struct Foo(Box<[u8]>); + +pub fn main() { + let a = Foo(box [0, 1, 2]); + let b = Foo(box [0, 1, 2]); + assert!(a == b); + println!("{}", a != b); + println!("{}", a < b); + println!("{}", a <= b); + println!("{}", a == b); + println!("{}", a > b); + println!("{}", a >= b); +}