Merge pull request #367 from dtolnay/notbool
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Switch to custom Not trait instead of `!` operator
authored by David Tolnay · committed by GitHub · parents 2b42f62, cbd91b8 · Browse files · 4 files·+123 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+123 added−33 removed
- tests:+91 added−29 removed
- other:+32 added−4 removed
Provenance
- Commit
20a9ff7- Tree
9251665- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2b42f62- Parent 2
cbd91b8
4 files changed+123 added−33 removed
modified:src/ensure.rs+4 added-4 removed
813 unmodified lines814 #[doc(hidden)]815 #[macro_export]816 macro_rules! __fallback_ensure {817 ($cond:expr $(,)?) => {818- if !$cond {818+ if $crate::__private::not($cond) {819 return $crate::__private::Err($crate::Error::msg(820 $crate::__private::concat!("Condition failed: `", $crate::__private::stringify!($cond), "`")821 ));822 }823 };824 ($cond:expr, $msg:literal $(,)?) => {825- if !$cond {825+ if $crate::__private::not($cond) {826 return $crate::__private::Err($crate::__anyhow!($msg));827 }828 };829 ($cond:expr, $err:expr $(,)?) => {830- if !$cond {830+ if $crate::__private::not($cond) {831 return $crate::__private::Err($crate::__anyhow!($err));832 }833 };834 ($cond:expr, $fmt:expr, $($arg:tt)*) => {835- if !$cond {835+ if $crate::__private::not($cond) {836 return $crate::__private::Err($crate::__anyhow!($fmt, $($arg)*));837 }838 };839 }modified:src/lib.rs+28 added
649 unmodified lines650 651 // Not public API. Referenced by macro-generated code.652 #[doc(hidden)]653 pub mod __private {654+ use self::not::Bool;655 use crate::Error;656 use alloc::fmt;657 use core::fmt::Arguments;658 40 unmodified lines699 #[must_use]700 pub fn must_use(error: Error) -> Error {701 error702 }703+704+ #[doc(hidden)]705+ #[inline]706+ pub fn not(cond: impl Bool) -> bool {707+ cond.not()708+ }709+710+ mod not {711+ #[doc(hidden)]712+ pub trait Bool {713+ fn not(self) -> bool;714+ }715+716+ impl Bool for bool {717+ #[inline]718+ fn not(self) -> bool {719+ !self720+ }721+ }722+723+ impl Bool for &bool {724+ #[inline]725+ fn not(self) -> bool {726+ !*self727+ }728+ }729+ }730 }modified:tests/ui/ensure-nonbool.rs+13 added-1 removed
1 use anyhow::{ensure, Result};2-use std::ops::Deref;2+use std::ops::{Deref, Not};3 4 struct Bool(bool);5 6 struct DerefBool(bool);7 8+struct NotBool(bool);9+10 impl Deref for DerefBool {11 type Target = bool;12 fn deref(&self) -> &Self::Target {13 &self.014 }15 }16 17+impl Not for NotBool {18+ type Output = bool;19+ fn not(self) -> Self::Output {20+ !self.021+ }22+}23+24 fn main() -> Result<()> {25 ensure!("...");26 27 let mut s = Bool(true);4 unmodified lines32 let db = DerefBool(true);33 ensure!(db);34 ensure!(&db);35 36+ let nb = NotBool(true);37+ ensure!(nb);38+39 Ok(())40 }modified:tests/ui/ensure-nonbool.stderr+78 added-28 removed
1-error[E0600]: cannot apply unary operator `!` to type `&'static str`2- --> tests/ui/ensure-nonbool.rs:16:51+error[E0277]: the trait bound `&str: __private::not::Bool` is not satisfied2+ --> tests/ui/ensure-nonbool.rs:25:133 |4-16 | ensure!("...");5- | ^^^^^^^^^^^^^^ cannot apply unary operator `!`4+25 | ensure!("...");5+ | --------^^^^^-6+ | | |7+ | | the trait `__private::not::Bool` is not implemented for `&str`8+ | required by a bound introduced by this call9 |7- = note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)10+ = help: the following other types implement trait `__private::not::Bool`:11+ &bool12+ bool13+note: required by a bound in `anyhow::__private::not`14+ --> src/lib.rs15+ |16+ | pub fn not(cond: impl Bool) -> bool {17+ | ^^^^ required by this bound in `not`18 9-error[E0600]: cannot apply unary operator `!` to type `&mut bool`10- --> tests/ui/ensure-nonbool.rs:20:2319+error[E0277]: the trait bound `&mut bool: __private::not::Bool` is not satisfied20+ --> tests/ui/ensure-nonbool.rs:29:3121 |12-20 | Bool(cond) => ensure!(cond),13- | ^^^^^^^^^^^^^ cannot apply unary operator `!`22+29 | Bool(cond) => ensure!(cond),23+ | --------^^^^-24+ | | |25+ | | the trait `__private::not::Bool` is not implemented for `&mut bool`26+ | required by a bound introduced by this call27 |15- = note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)28+ = help: the following other types implement trait `__private::not::Bool`:29+ &bool30+ bool31+ = note: `__private::not::Bool` is implemented for `&bool`, but not for `&mut bool`32+note: required by a bound in `anyhow::__private::not`33+ --> src/lib.rs34+ |35+ | pub fn not(cond: impl Bool) -> bool {36+ | ^^^^ required by this bound in `not`37 17-error[E0600]: cannot apply unary operator `!` to type `DerefBool`18- --> tests/ui/ensure-nonbool.rs:24:538+error[E0277]: the trait bound `DerefBool: __private::not::Bool` is not satisfied39+ --> tests/ui/ensure-nonbool.rs:33:1340 |20-24 | ensure!(db);21- | ^^^^^^^^^^^ cannot apply unary operator `!`41+33 | ensure!(db);42+ | --------^^-43+ | | |44+ | | the trait `__private::not::Bool` is not implemented for `DerefBool`45+ | required by a bound introduced by this call46 |23-note: an implementation of `Not` might be missing for `DerefBool`24- --> tests/ui/ensure-nonbool.rs:6:147+ = help: the following other types implement trait `__private::not::Bool`:48+ &bool49+ bool50+note: required by a bound in `anyhow::__private::not`51+ --> src/lib.rs52 |26-6 | struct DerefBool(bool);27- | ^^^^^^^^^^^^^^^^ must implement `Not`28-note: the trait `Not` must be implemented29- --> $RUST/core/src/ops/bit.rs53+ | pub fn not(cond: impl Bool) -> bool {54+ | ^^^^ required by this bound in `not`55+56+error[E0277]: the trait bound `&DerefBool: __private::not::Bool` is not satisfied57+ --> tests/ui/ensure-nonbool.rs:34:1358 |31- | pub trait Not {32- | ^^^^^^^^^^^^^33- = note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)59+34 | ensure!(&db);60+ | --------^^^-61+ | | |62+ | | the trait `__private::not::Bool` is not implemented for `&DerefBool`63+ | required by a bound introduced by this call64+ |65+note: required by a bound in `anyhow::__private::not`66+ --> src/lib.rs67+ |68+ | pub fn not(cond: impl Bool) -> bool {69+ | ^^^^ required by this bound in `not`70+help: consider dereferencing here71+ |72+34 | ensure!(&*db);73+ | +74 35-error[E0600]: cannot apply unary operator `!` to type `&DerefBool`36- --> tests/ui/ensure-nonbool.rs:25:575+error[E0277]: the trait bound `NotBool: __private::not::Bool` is not satisfied76+ --> tests/ui/ensure-nonbool.rs:37:1377 |38-25 | ensure!(&db);39- | ^^^^^^^^^^^^ cannot apply unary operator `!`78+37 | ensure!(nb);79+ | --------^^-80+ | | |81+ | | the trait `__private::not::Bool` is not implemented for `NotBool`82+ | required by a bound introduced by this call83 |41- = note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)84+ = help: the following other types implement trait `__private::not::Bool`:85+ &bool86+ bool87+note: required by a bound in `anyhow::__private::not`88+ --> src/lib.rs89+ |90+ | pub fn not(cond: impl Bool) -> bool {91+ | ^^^^ required by this bound in `not`