From 8f33b4eed15a6c6e620e502e74f8c01e6f0702e9 Mon Sep 17 00:00:00 2001 From: Ariel Davis Date: Sat, 15 Jan 2022 16:25:09 -0800 Subject: [PATCH 1/3] Copy an example to PartialOrd as well --- library/core/src/cmp.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index deed9901cc9e4..ef81e8736ed47 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -668,7 +668,7 @@ impl Clone for Reverse { /// Here's an example: /// /// ``` -/// #[derive(PartialEq, PartialOrd)] +/// #[derive(PartialEq, Eq, PartialOrd, Ord)] /// enum Size { /// Small, /// Large, @@ -898,6 +898,18 @@ impl PartialOrd for Ordering { /// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a /// lexicographic ordering based on the top-to-bottom declaration order of the struct's members. /// When `derive`d on enums, variants are ordered by their top-to-bottom discriminant order. +/// This means variants at the top are less than variants at the bottom. +/// Here's an example: +/// +/// ``` +/// #[derive(PartialEq, PartialOrd)] +/// enum Size { +/// Small, +/// Large, +/// } +/// +/// assert!(Size::Small < Size::Large); +/// ``` /// /// ## How can I implement `PartialOrd`? /// @@ -970,8 +982,8 @@ impl PartialOrd for Ordering { /// # Examples /// /// ``` -/// let x : u32 = 0; -/// let y : u32 = 1; +/// let x: u32 = 0; +/// let y: u32 = 1; /// /// assert_eq!(x < y, true); /// assert_eq!(x.lt(&y), true); From 828febf9e0fbd653cdcb2a949ec3defded69dc3f Mon Sep 17 00:00:00 2001 From: Ariel Davis Date: Sat, 15 Jan 2022 20:24:38 -0800 Subject: [PATCH 2/3] Clear up discriminants with more examples --- library/core/src/cmp.rs | 70 ++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index ef81e8736ed47..a404b699de161 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -661,20 +661,37 @@ impl Clone for Reverse { /// /// ## Derivable /// -/// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a -/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering based on the top-to-bottom declaration order of the struct's members. -/// When `derive`d on enums, variants are ordered by their top-to-bottom discriminant order. -/// This means variants at the top are less than variants at the bottom. -/// Here's an example: +/// This trait can be used with `#[derive]`. /// +/// When `derive`d on structs, it will produce a +/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering +/// based on the top-to-bottom declaration order of the struct's members. +/// +/// When `derive`d on enums, variants are ordered by their discriminants. +/// By default, the discriminant is smallest for variants at the top, and +/// largest for variants at the bottom. Here's an example: +/// +/// ``` +/// #[derive(PartialEq, Eq, PartialOrd, Ord)] +/// enum E { +/// Top, +/// Bottom, +/// } +/// +/// assert!(E::Top < E::Bottom); +/// ``` +/// +/// However, manually setting the discriminants can override this default +/// behavior: +//// /// ``` /// #[derive(PartialEq, Eq, PartialOrd, Ord)] -/// enum Size { -/// Small, -/// Large, +/// enum E { +/// Top = 2, +/// Bottom = 1, /// } /// -/// assert!(Size::Small < Size::Large); +/// assert!(E::Bottom < E::Top); /// ``` /// /// ## Lexicographical comparison @@ -895,20 +912,37 @@ impl PartialOrd for Ordering { /// /// ## Derivable /// -/// This trait can be used with `#[derive]`. When `derive`d on structs, it will produce a -/// lexicographic ordering based on the top-to-bottom declaration order of the struct's members. -/// When `derive`d on enums, variants are ordered by their top-to-bottom discriminant order. -/// This means variants at the top are less than variants at the bottom. -/// Here's an example: +/// This trait can be used with `#[derive]`. +/// +/// When `derive`d on structs, it will produce a +/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering +/// based on the top-to-bottom declaration order of the struct's members. +/// +/// When `derive`d on enums, variants are ordered by their discriminants. +/// By default, the discriminant is smallest for variants at the top, and +/// largest for variants at the bottom. Here's an example: +/// +/// ``` +/// #[derive(PartialEq, PartialOrd)] +/// enum E { +/// Top, +/// Bottom, +/// } +/// +/// assert!(E::Top < E::Bottom); +/// ``` /// +/// However, manually setting the discriminants can override this default +/// behavior: +//// /// ``` /// #[derive(PartialEq, PartialOrd)] -/// enum Size { -/// Small, -/// Large, +/// enum E { +/// Top = 2, +/// Bottom = 1, /// } /// -/// assert!(Size::Small < Size::Large); +/// assert!(E::Bottom < E::Top); /// ``` /// /// ## How can I implement `PartialOrd`? From bfe0a4e06e7714c2e0e3b42e1b4ec3b6e5ca121d Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 15 Jan 2022 20:44:47 -0800 Subject: [PATCH 3/3] Touch up stray comment in PR 92953 --- library/core/src/cmp.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index a404b699de161..f89cf812e970a 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -683,7 +683,7 @@ impl Clone for Reverse { /// /// However, manually setting the discriminants can override this default /// behavior: -//// +/// /// ``` /// #[derive(PartialEq, Eq, PartialOrd, Ord)] /// enum E { @@ -934,7 +934,7 @@ impl PartialOrd for Ordering { /// /// However, manually setting the discriminants can override this default /// behavior: -//// +/// /// ``` /// #[derive(PartialEq, PartialOrd)] /// enum E {