| 1 |
// { dg-options "-w" } |
| 2 |
// taken from https://github.com/rust-lang/rust/blob/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/cmp.rs#L98 |
| 3 |
|
| 4 |
#[lang = "sized"] |
| 5 |
pub trait Sized {} |
| 6 |
|
| 7 |
#[lang = "eq"] |
| 8 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 9 |
#[doc(alias = "==")] |
| 10 |
#[doc(alias = "!=")] |
| 11 |
pub trait PartialEq<Rhs: ?Sized = Self> { |
| 12 |
/// This method tests for `self` and `other` values to be equal, and is used |
| 13 |
/// by `==`. |
| 14 |
#[must_use] |
| 15 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 16 |
fn eq(&self, other: &Rhs) -> bool; |
| 17 |
|
| 18 |
/// This method tests for `!=`. |
| 19 |
#[inline] |
| 20 |
#[must_use] |
| 21 |
#[stable(feature = "rust1", since = "1.0.0")] |
| 22 |
fn ne(&self, other: &Rhs) -> bool { |
| 23 |
!self.eq(other) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
enum BookFormat { |
| 28 |
Paperback, |
| 29 |
Hardback, |
| 30 |
Ebook, |
| 31 |
} |
| 32 |
|
| 33 |
impl PartialEq<BookFormat> for BookFormat { |
| 34 |
fn eq(&self, other: &BookFormat) -> bool { |
| 35 |
self == other |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
pub struct Book { |
| 40 |
isbn: i32, |
| 41 |
format: BookFormat, |
| 42 |
} |
| 43 |
|
| 44 |
// Implement <Book> == <BookFormat> comparisons |
| 45 |
impl PartialEq<BookFormat> for Book { |
| 46 |
fn eq(&self, other: &BookFormat) -> bool { |
| 47 |
self.format == *other |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// Implement <BookFormat> == <Book> comparisons |
| 52 |
impl PartialEq<Book> for BookFormat { |
| 53 |
fn eq(&self, other: &Book) -> bool { |
| 54 |
*self == other.format |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// Implement <Book> == <Book> comparisons |
| 59 |
impl PartialEq<Book> for Book { |
| 60 |
fn eq(&self, other: &Book) -> bool { |
| 61 |
self.isbn == other.isbn |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
pub fn main() { |
| 66 |
let b1 = Book { |
| 67 |
isbn: 1, |
| 68 |
format: BookFormat::Paperback, |
| 69 |
}; |
| 70 |
let b2 = Book { |
| 71 |
isbn: 2, |
| 72 |
format: BookFormat::Paperback, |
| 73 |
}; |
| 74 |
|
| 75 |
let _c1: bool = b1 == BookFormat::Paperback; |
| 76 |
let _c2: bool = BookFormat::Paperback == b2; |
| 77 |
let _c3: bool = b1 != b2; |
| 78 |
} |