Skip to content

Commit 2e4392e

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#53844. - Removes the last example since it appears to be redundant with the previous two examples.
2 parents 9ba024b + 32b2834 commit 2e4392e

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/libcore/cmp.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ use self::Ordering::*;
9191
/// For example, let's tweak our previous code a bit:
9292
///
9393
/// ```
94+
/// // The derive implements <BookFormat> == <BookFormat> comparisons
95+
/// #[derive(PartialEq)]
9496
/// enum BookFormat {
9597
/// Paperback,
9698
/// Hardback,
@@ -102,31 +104,34 @@ use self::Ordering::*;
102104
/// format: BookFormat,
103105
/// }
104106
///
107+
/// // Implement <Book> == <BookFormat> comparisons
105108
/// impl PartialEq<BookFormat> for Book {
106109
/// fn eq(&self, other: &BookFormat) -> bool {
107-
/// match (&self.format, other) {
108-
/// (BookFormat::Paperback, BookFormat::Paperback) => true,
109-
/// (BookFormat::Hardback, BookFormat::Hardback) => true,
110-
/// (BookFormat::Ebook, BookFormat::Ebook) => true,
111-
/// (_, _) => false,
112-
/// }
110+
/// self.format == *other
111+
/// }
112+
/// }
113+
///
114+
/// // Implement <BookFormat> == <Book> comparisons
115+
/// impl PartialEq<Book> for BookFormat {
116+
/// fn eq(&self, other: &Book) -> bool {
117+
/// *self == other.format
113118
/// }
114119
/// }
115120
///
116121
/// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
117122
///
118123
/// assert!(b1 == BookFormat::Paperback);
119-
/// assert!(b1 != BookFormat::Ebook);
124+
/// assert!(BookFormat::Ebook != b1);
120125
/// ```
121126
///
122127
/// By changing `impl PartialEq for Book` to `impl PartialEq<BookFormat> for Book`,
123-
/// we've changed what type we can use on the right side of the `==` operator.
124-
/// This lets us use it in the `assert!` statements at the bottom.
128+
/// we allow `BookFormat`s to be compared with `Book`s.
125129
///
126130
/// You can also combine these implementations to let the `==` operator work with
127131
/// two different types:
128132
///
129133
/// ```
134+
/// #[derive(PartialEq)]
130135
/// enum BookFormat {
131136
/// Paperback,
132137
/// Hardback,
@@ -140,12 +145,13 @@ use self::Ordering::*;
140145
///
141146
/// impl PartialEq<BookFormat> for Book {
142147
/// fn eq(&self, other: &BookFormat) -> bool {
143-
/// match (&self.format, other) {
144-
/// (&BookFormat::Paperback, &BookFormat::Paperback) => true,
145-
/// (&BookFormat::Hardback, &BookFormat::Hardback) => true,
146-
/// (&BookFormat::Ebook, &BookFormat::Ebook) => true,
147-
/// (_, _) => false,
148-
/// }
148+
/// self.format == *other
149+
/// }
150+
/// }
151+
///
152+
/// impl PartialEq<Book> for BookFormat {
153+
/// fn eq(&self, other: &Book) -> bool {
154+
/// *self == other.format
149155
/// }
150156
/// }
151157
///
@@ -159,7 +165,7 @@ use self::Ordering::*;
159165
/// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
160166
///
161167
/// assert!(b1 == BookFormat::Paperback);
162-
/// assert!(b1 != BookFormat::Ebook);
168+
/// assert!(BookFormat::Ebook != b1);
163169
/// assert!(b1 == b2);
164170
/// ```
165171
///

0 commit comments

Comments
 (0)