From 3db2efbb5eb57b15b991dc4c6fd1ce3ca3ee6dc9 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Thu, 4 Mar 2021 19:31:11 -0700 Subject: [PATCH 1/5] add the bool::not method. --- compiler/rustc_feature/src/active.rs | 3 +++ library/core/src/bool.rs | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 8ee995a59d80..a7c3c7e6bfa4 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -644,6 +644,9 @@ declare_features! ( /// Allows associated types in inherent impls. (active, inherent_associated_types, "1.52.0", Some(8995), None), + /// Adds the `bool::not` method so that you don't need `core::ops::Not` imported. + (active, bool_not_method, "1.52.0", None, None), + // ------------------------------------------------------------------------- // feature-group-end: actual feature gates // ------------------------------------------------------------------------- diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index 00164c631b30..0a06eed3423a 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -31,4 +31,9 @@ impl bool { pub fn then T>(self, f: F) -> Option { if self { Some(f()) } else { None } } + + #[unstable(feature = "bool_not_method", issue = "none")] + pub fn not(self) -> Self { + !self + } } From a25f6f7105d404b49ca4a79b2ca515e0af48d0ec Mon Sep 17 00:00:00 2001 From: Lokathor Date: Thu, 4 Mar 2021 19:38:33 -0700 Subject: [PATCH 2/5] document the method. --- library/core/src/bool.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index 0a06eed3423a..b9ee1f02c72b 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -32,6 +32,14 @@ impl bool { if self { Some(f()) } else { None } } + /// Returns the opposite value. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(false.not(), true); + /// assert_eq!(true.not(), false); + /// ``` #[unstable(feature = "bool_not_method", issue = "none")] pub fn not(self) -> Self { !self From 828aa80806e6b3e106659754289d6bd92d76149a Mon Sep 17 00:00:00 2001 From: Lokathor Date: Thu, 4 Mar 2021 21:15:38 -0700 Subject: [PATCH 3/5] take out the new feature, we don't need a compiler feature. --- compiler/rustc_feature/src/active.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index a7c3c7e6bfa4..8ee995a59d80 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -644,9 +644,6 @@ declare_features! ( /// Allows associated types in inherent impls. (active, inherent_associated_types, "1.52.0", Some(8995), None), - /// Adds the `bool::not` method so that you don't need `core::ops::Not` imported. - (active, bool_not_method, "1.52.0", None, None), - // ------------------------------------------------------------------------- // feature-group-end: actual feature gates // ------------------------------------------------------------------------- From 663ce13ff6a7ad01bfdbebdbcf4d46418031b91a Mon Sep 17 00:00:00 2001 From: Lokathor Date: Wed, 10 Mar 2021 19:41:18 -0700 Subject: [PATCH 4/5] add const modifier and inline attribute to bool::not --- library/core/src/bool.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index b9ee1f02c72b..38b4d871250a 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -41,7 +41,8 @@ impl bool { /// assert_eq!(true.not(), false); /// ``` #[unstable(feature = "bool_not_method", issue = "none")] - pub fn not(self) -> Self { + #[inline] + pub const fn not(self) -> Self { !self } } From 00c542688e0111c83eeaa30557731a15071c0616 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Sun, 28 Mar 2021 11:06:01 -0600 Subject: [PATCH 5/5] improve documentation. --- library/core/src/bool.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index 38b4d871250a..01f53b3138a2 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -34,11 +34,16 @@ impl bool { /// Returns the opposite value. /// + /// This is identical in functionality to usage of the `!` operator, it's + /// simply an alternate way to write it. Because it's post-fix instead of + /// pre-fix, it is often more readable within chained function call + /// expressions. + /// /// # Examples /// /// ``` - /// assert_eq!(false.not(), true); - /// assert_eq!(true.not(), false); + /// assert_eq!(!true, true.not()); + /// assert_eq!(!false, false.not()); /// ``` #[unstable(feature = "bool_not_method", issue = "none")] #[inline]