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.
This commit is contained in:
@@ -91,6 +91,8 @@ use self::Ordering::*;
|
|||||||
/// For example, let's tweak our previous code a bit:
|
/// For example, let's tweak our previous code a bit:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
/// // The derive implements <BookFormat> == <BookFormat> comparisons
|
||||||
|
/// #[derive(PartialEq)]
|
||||||
/// enum BookFormat {
|
/// enum BookFormat {
|
||||||
/// Paperback,
|
/// Paperback,
|
||||||
/// Hardback,
|
/// Hardback,
|
||||||
@@ -102,31 +104,34 @@ use self::Ordering::*;
|
|||||||
/// format: BookFormat,
|
/// format: BookFormat,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
|
/// // Implement <Book> == <BookFormat> comparisons
|
||||||
/// impl PartialEq<BookFormat> for Book {
|
/// impl PartialEq<BookFormat> for Book {
|
||||||
/// fn eq(&self, other: &BookFormat) -> bool {
|
/// fn eq(&self, other: &BookFormat) -> bool {
|
||||||
/// match (&self.format, other) {
|
/// self.format == *other
|
||||||
/// (BookFormat::Paperback, BookFormat::Paperback) => true,
|
/// }
|
||||||
/// (BookFormat::Hardback, BookFormat::Hardback) => true,
|
/// }
|
||||||
/// (BookFormat::Ebook, BookFormat::Ebook) => true,
|
///
|
||||||
/// (_, _) => false,
|
/// // 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 };
|
/// let b1 = Book { isbn: 3, format: BookFormat::Paperback };
|
||||||
///
|
///
|
||||||
/// assert!(b1 == 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`,
|
/// 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.
|
/// we allow `BookFormat`s to be compared with `Book`s.
|
||||||
/// This lets us use it in the `assert!` statements at the bottom.
|
|
||||||
///
|
///
|
||||||
/// You can also combine these implementations to let the `==` operator work with
|
/// You can also combine these implementations to let the `==` operator work with
|
||||||
/// two different types:
|
/// two different types:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
/// #[derive(PartialEq)]
|
||||||
/// enum BookFormat {
|
/// enum BookFormat {
|
||||||
/// Paperback,
|
/// Paperback,
|
||||||
/// Hardback,
|
/// Hardback,
|
||||||
@@ -140,12 +145,13 @@ use self::Ordering::*;
|
|||||||
///
|
///
|
||||||
/// impl PartialEq<BookFormat> for Book {
|
/// impl PartialEq<BookFormat> for Book {
|
||||||
/// fn eq(&self, other: &BookFormat) -> bool {
|
/// fn eq(&self, other: &BookFormat) -> bool {
|
||||||
/// match (&self.format, other) {
|
/// self.format == *other
|
||||||
/// (&BookFormat::Paperback, &BookFormat::Paperback) => true,
|
/// }
|
||||||
/// (&BookFormat::Hardback, &BookFormat::Hardback) => true,
|
/// }
|
||||||
/// (&BookFormat::Ebook, &BookFormat::Ebook) => true,
|
///
|
||||||
/// (_, _) => false,
|
/// 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 };
|
/// let b2 = Book { isbn: 3, format: BookFormat::Ebook };
|
||||||
///
|
///
|
||||||
/// assert!(b1 == BookFormat::Paperback);
|
/// assert!(b1 == BookFormat::Paperback);
|
||||||
/// assert!(b1 != BookFormat::Ebook);
|
/// assert!(BookFormat::Ebook != b1);
|
||||||
/// assert!(b1 == b2);
|
/// assert!(b1 == b2);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|||||||
Reference in New Issue
Block a user