From 96678df83821407f2112028cc0099866cb548c49 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 5 Jan 2019 12:41:12 -0500 Subject: [PATCH 1/8] Cleanup PartialEq docs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Cleanup the `impl PartialEq for Book` implementation - Implement `impl PartialEq 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. --- src/libcore/cmp.rs | 56 ++++++++-------------------------------------- 1 file changed, 9 insertions(+), 47 deletions(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index f420d0d00a401..cf24e08e2e706 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -107,67 +107,29 @@ use self::Ordering::*; /// format: BookFormat, /// } /// +/// // Implement == comparisons /// impl PartialEq 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 /// } /// } /// -/// let b1 = Book { isbn: 3, format: BookFormat::Paperback }; -/// -/// assert!(b1 == BookFormat::Paperback); -/// assert!(b1 != BookFormat::Ebook); -/// ``` -/// -/// By changing `impl PartialEq for Book` to `impl PartialEq 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. -/// -/// You can also combine these implementations to let the `==` operator work with -/// two different types: -/// -/// ``` -/// enum BookFormat { -/// Paperback, -/// Hardback, -/// Ebook, -/// } -/// -/// struct Book { -/// isbn: i32, -/// format: BookFormat, -/// } -/// -/// impl PartialEq 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, -/// } -/// } -/// } -/// -/// impl PartialEq for Book { +/// // Implement == comparisons +/// impl PartialEq for BookFormat { /// fn eq(&self, other: &Book) -> bool { -/// self.isbn == other.isbn +/// *other == self.format /// } /// } /// /// let b1 = Book { isbn: 3, format: BookFormat::Paperback }; -/// let b2 = Book { isbn: 3, format: BookFormat::Ebook }; /// /// assert!(b1 == BookFormat::Paperback); -/// assert!(b1 != BookFormat::Ebook); -/// assert!(b1 == b2); +/// assert!(BookFormat::Ebook != b1); /// ``` /// +/// By changing `impl PartialEq for Book` to `impl PartialEq for Book`, +/// we allow `BookFormat`s to be compared with `Book`s. +/// /// # Examples /// /// ``` From 0d3dfdf6aadd7dd241956fa234bdd01de7291e5f Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 5 Jan 2019 22:52:13 -0500 Subject: [PATCH 2/8] Fix compile error --- src/libcore/cmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index cf24e08e2e706..89afe7057b17b 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -117,7 +117,7 @@ use self::Ordering::*; /// // Implement == comparisons /// impl PartialEq for BookFormat { /// fn eq(&self, other: &Book) -> bool { -/// *other == self.format +/// *self == other.format /// } /// } /// From 319a2c1cc2a4d99d5b1d6fb424c77963fd668f24 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 6 Jan 2019 11:08:34 -0500 Subject: [PATCH 3/8] add missing derive to fix compile error --- src/libcore/cmp.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 89afe7057b17b..5b78aa632d17f 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -65,6 +65,7 @@ use self::Ordering::*; /// the same book if their ISBN matches, even if the formats differ: /// /// ``` +/// #[derive(PartialEq)] /// enum BookFormat { /// Paperback, /// Hardback, From bbbabdfc52eaa897c965bf10f50af4fc1512d4f0 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 6 Jan 2019 15:18:22 -0500 Subject: [PATCH 4/8] Update cmp.rs --- src/libcore/cmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 5b78aa632d17f..64e37ec5a56cb 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -65,7 +65,6 @@ use self::Ordering::*; /// the same book if their ISBN matches, even if the formats differ: /// /// ``` -/// #[derive(PartialEq)] /// enum BookFormat { /// Paperback, /// Hardback, @@ -97,6 +96,7 @@ use self::Ordering::*; /// For example, let's tweak our previous code a bit: /// /// ``` +/// #[derive(PartialEq)] /// enum BookFormat { /// Paperback, /// Hardback, From 423a5bb5c437c2ce81d4150490d0cccfdbc33a4c Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 12 Jan 2019 11:00:14 -0500 Subject: [PATCH 5/8] add comment explaining what the derive does --- src/libcore/cmp.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 64e37ec5a56cb..72ff9cca9fd86 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -96,6 +96,7 @@ use self::Ordering::*; /// For example, let's tweak our previous code a bit: /// /// ``` +/// // The derive implements == comparisons /// #[derive(PartialEq)] /// enum BookFormat { /// Paperback, From 1445a065ded1bc7e73e7a358f9bc971e908e9181 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 12 Jan 2019 11:17:49 -0500 Subject: [PATCH 6/8] bring back the example i removed, also add symmetry and simplify impl --- src/libcore/cmp.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 72ff9cca9fd86..db72b7bb9d2da 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -132,6 +132,48 @@ use self::Ordering::*; /// By changing `impl PartialEq for Book` to `impl PartialEq for Book`, /// 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, +/// Ebook, +/// } +/// +/// struct Book { +/// isbn: i32, +/// format: BookFormat, +/// } +/// +/// impl PartialEq for Book { +/// fn eq(&self, other: &BookFormat) -> bool { +/// self.format == *other +/// } +/// } +/// +/// impl PartialEq for BookFormat { +/// fn eq(&self, other: &Book) -> bool { +/// *self == other.format +/// } +/// } +/// +/// impl PartialEq for Book { +/// fn eq(&self, other: &Book) -> bool { +/// self.isbn == other.isbn +/// } +/// } +/// +/// let b1 = Book { isbn: 3, format: BookFormat::Paperback }; +/// let b2 = Book { isbn: 3, format: BookFormat::Ebook }; +/// +/// assert!(b1 == BookFormat::Paperback); +/// assert!(b1 != BookFormat::Ebook); +/// assert!(b1 == b2); +/// ``` +/// /// # Examples /// /// ``` From 0394dce7884562651906a84e3a32dc8ad2a50ac8 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sat, 12 Jan 2019 11:19:02 -0500 Subject: [PATCH 7/8] whitespace --- src/libcore/cmp.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index db72b7bb9d2da..65792607a2824 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -142,33 +142,33 @@ use self::Ordering::*; /// Hardback, /// Ebook, /// } -/// +/// /// struct Book { /// isbn: i32, /// format: BookFormat, /// } -/// +/// /// impl PartialEq for Book { /// fn eq(&self, other: &BookFormat) -> bool { /// self.format == *other /// } /// } -/// +/// /// impl PartialEq for BookFormat { /// fn eq(&self, other: &Book) -> bool { /// *self == other.format /// } /// } -/// +/// /// impl PartialEq for Book { /// fn eq(&self, other: &Book) -> bool { /// self.isbn == other.isbn /// } /// } -/// +/// /// let b1 = Book { isbn: 3, format: BookFormat::Paperback }; /// let b2 = Book { isbn: 3, format: BookFormat::Ebook }; -/// +/// /// assert!(b1 == BookFormat::Paperback); /// assert!(b1 != BookFormat::Ebook); /// assert!(b1 == b2); From 32b28340b20ea8b45d3e663dc81bb9092ecdeeb1 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 15 Jan 2019 21:21:24 -0500 Subject: [PATCH 8/8] demonstrate symmetry --- src/libcore/cmp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 65792607a2824..a2f719cf406fe 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -170,7 +170,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); /// ``` ///