Skip to content
Myr

Switch to custom Not trait instead of '!' operator

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent 79d1684 · Browse files · 5 files+123 added−43 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+123 added−43 removed

  • tests:+91 added−39 removed
  • other:+32 added−4 removed

5 files+123 added−43 removed

modified:src/ensure.rs+4 added-4 removed
813 unmodified lines
814#[doc(hidden)]815#[macro_export]816macro_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 lines
650651// Not public API. Referenced by macro-generated code.652#[doc(hidden)]653pub mod __private {654 use self::not::Bool;655 use crate::Error;656 use alloc::fmt;657 use core::fmt::Arguments;658
40 unmodified lines
699 #[must_use]700 pub fn must_use(error: Error) -> Error {701 error702 }703704 #[doc(hidden)]705 #[inline]706 pub fn not(cond: impl Bool) -> bool {707 cond.not()708 }709710 mod not {711 #[doc(hidden)]712 pub trait Bool {713 fn not(self) -> bool;714 }715716 impl Bool for bool {717 #[inline]718 fn not(self) -> bool {719 !self720 }721 }722723 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
1use anyhow::{ensure, Result};2use std::ops::Deref;2use std::ops::{Deref, Not};34struct Bool(bool);56struct DerefBool(bool);78struct NotBool(bool);910impl Deref for DerefBool {11 type Target = bool;12 fn deref(&self) -> &Self::Target {13 &self.014 }15}1617impl Not for NotBool {18 type Output = bool;19 fn not(self) -> Self::Output {20 !self.021 }22}2324fn main() -> Result<()> {25 ensure!("...");2627 let mut s = Bool(true);
4 unmodified lines
32 let db = DerefBool(true);33 ensure!(db);34 ensure!(&db);3536 let nb = NotBool(true);37 ensure!(nb);3839 Ok(())40}
modified:tests/ui/ensure-nonbool.stderr+78 added-28 removed
1error[E0600]: cannot apply unary operator `!` to type `&'static str`2 --> tests/ui/ensure-nonbool.rs:16:51error[E0277]: the trait bound `&str: __private::not::Bool` is not satisfied2 --> tests/ui/ensure-nonbool.rs:25:133 |416 | ensure!("...");5 | ^^^^^^^^^^^^^^ cannot apply unary operator `!`425 | 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 bool13note: 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`189error[E0600]: cannot apply unary operator `!` to type `&mut bool`10 --> tests/ui/ensure-nonbool.rs:20:2319error[E0277]: the trait bound `&mut bool: __private::not::Bool` is not satisfied20 --> tests/ui/ensure-nonbool.rs:29:3121 |1220 | Bool(cond) => ensure!(cond),13 | ^^^^^^^^^^^^^ cannot apply unary operator `!`2229 | 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`32note: 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`3717error[E0600]: cannot apply unary operator `!` to type `DerefBool`18 --> tests/ui/ensure-nonbool.rs:24:538error[E0277]: the trait bound `DerefBool: __private::not::Bool` is not satisfied39 --> tests/ui/ensure-nonbool.rs:33:1340 |2024 | ensure!(db);21 | ^^^^^^^^^^^ cannot apply unary operator `!`4133 | ensure!(db);42 | --------^^-43 | | |44 | | the trait `__private::not::Bool` is not implemented for `DerefBool`45 | required by a bound introduced by this call46 |23note: 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 bool50note: required by a bound in `anyhow::__private::not`51 --> src/lib.rs52 |266 | struct DerefBool(bool);27 | ^^^^^^^^^^^^^^^^ must implement `Not`28note: 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`5556error[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)5934 | ensure!(&db);60 | --------^^^-61 | | |62 | | the trait `__private::not::Bool` is not implemented for `&DerefBool`63 | required by a bound introduced by this call64 |65note: 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`70help: consider dereferencing here71 |7234 | ensure!(&*db);73 | +7435error[E0600]: cannot apply unary operator `!` to type `&DerefBool`36 --> tests/ui/ensure-nonbool.rs:25:575error[E0277]: the trait bound `NotBool: __private::not::Bool` is not satisfied76 --> tests/ui/ensure-nonbool.rs:37:1377 |3825 | ensure!(&db);39 | ^^^^^^^^^^^^ cannot apply unary operator `!`7837 | 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 bool87note: 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`
modified:tests/test_macros.rs-10 removed
13 unmodified lines
14use self::common::*;15use anyhow::{anyhow, ensure, Result};16use std::cell::Cell;17use std::future;18use std::ops::Not;1819#[test]20fn test_messages() {21 assert_eq!("oh no!", bail_literal().unwrap_err().to_string());
37 unmodified lines
59 struct Struct {60 condition: bool,61 }6264 impl Not for Struct {65 type Output = bool;66 fn not(self) -> Self::Output {67 !self.condition68 }69 }7063 let s = Struct { condition: true };64 match &s {65 Struct { condition } => ensure!(condition), // &bool66 }6776 ensure!(s);7768 Ok(())69}7071#[test]
25 unmodified lines