diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-01-18 18:06:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-18 18:06:30 +0100 |
| commit | bca490808fda5ccd73522750025c47b79306aa72 (patch) | |
| tree | 591c4d8321f854f4dd84a1863de47335d4fb71eb /src/libcore | |
| parent | 76cdccb2c2b90539fe37f4a264a80c94d395cc78 (diff) | |
| parent | 32b28340b20ea8b45d3e663dc81bb9092ecdeeb1 (diff) | |
| download | rust-bca490808fda5ccd73522750025c47b79306aa72.tar.gz rust-bca490808fda5ccd73522750025c47b79306aa72.zip | |
Rollup merge of #57357 - frewsxcv:frewsxcv-partial-eq, r=QuietMisdreavus
Cleanup PartialEq docs. - Cleanup the `impl PartialEq<BookFormat> for Book` implementation - Implement `impl PartialEq<Book> for BookFormat` so it’s symmetric - Fixes https://github.com/rust-lang/rust/issues/53844. - Removes the last example since it appears to be redundant with the previous two examples.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/cmp.rs | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 86f28a957cd..d43a5c1032c 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -91,6 +91,8 @@ use self::Ordering::*; /// For example, let's tweak our previous code a bit: /// /// ``` +/// // The derive implements <BookFormat> == <BookFormat> comparisons +/// #[derive(PartialEq)] /// enum BookFormat { /// Paperback, /// Hardback, @@ -102,31 +104,34 @@ use self::Ordering::*; /// format: BookFormat, /// } /// +/// // Implement <Book> == <BookFormat> comparisons /// impl PartialEq<BookFormat> for Book { /// fn eq(&self, other: &BookFormat) -> bool { -/// match (&self.format, other) { -/// (BookFormat::Paperback, BookFormat::Paperback) => true, -/// (BookFormat::Hardback, BookFormat::Hardback) => true, -/// (BookFormat::Ebook, BookFormat::Ebook) => true, -/// (_, _) => false, -/// } +/// self.format == *other +/// } +/// } +/// +/// // Implement <BookFormat> == <Book> comparisons +/// impl PartialEq<Book> for BookFormat { +/// fn eq(&self, other: &Book) -> bool { +/// *self == other.format /// } /// } /// /// let b1 = Book { isbn: 3, format: BookFormat::Paperback }; /// /// assert!(b1 == BookFormat::Paperback); -/// assert!(b1 != BookFormat::Ebook); +/// assert!(BookFormat::Ebook != b1); /// ``` /// /// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`, -/// we've changed what type we can use on the right side of the `==` operator. -/// This lets us use it in the `assert!` statements at the bottom. +/// we allow `BookFormat`s to be compared with `Book`s. /// /// You can also combine these implementations to let the `==` operator work with /// two different types: /// /// ``` +/// #[derive(PartialEq)] /// enum BookFormat { /// Paperback, /// Hardback, @@ -140,12 +145,13 @@ use self::Ordering::*; /// /// impl PartialEq<BookFormat> for Book { /// fn eq(&self, other: &BookFormat) -> bool { -/// match (&self.format, other) { -/// (&BookFormat::Paperback, &BookFormat::Paperback) => true, -/// (&BookFormat::Hardback, &BookFormat::Hardback) => true, -/// (&BookFormat::Ebook, &BookFormat::Ebook) => true, -/// (_, _) => false, -/// } +/// self.format == *other +/// } +/// } +/// +/// impl PartialEq<Book> for BookFormat { +/// fn eq(&self, other: &Book) -> bool { +/// *self == other.format /// } /// } /// @@ -159,7 +165,7 @@ use self::Ordering::*; /// let b2 = Book { isbn: 3, format: BookFormat::Ebook }; /// /// assert!(b1 == BookFormat::Paperback); -/// assert!(b1 != BookFormat::Ebook); +/// assert!(BookFormat::Ebook != b1); /// assert!(b1 == b2); /// ``` /// |
