Skip to content

Commit

Permalink
Rollup merge of rust-lang#57357 - frewsxcv:frewsxcv-partial-eq, r=Qui…
Browse files Browse the repository at this point in the history
…etMisdreavus

Cleanup PartialEq docs.

- Cleanup the `impl PartialEq<BookFormat> for Book` implementation
- Implement `impl PartialEq<Book> for BookFormat` so it’s symmetric
  - Fixes rust-lang#53844.
- Removes the last example since it appears to be redundant with the
  previous two examples.
  • Loading branch information
Centril authored Jan 18, 2019
2 parents 48a2f37 + 32b2834 commit c3c5d66
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
/// }
/// }
///
Expand All @@ -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);
/// ```
///
Expand Down

0 comments on commit c3c5d66

Please sign in to comment.