Skip to content
Myr

Include a Debug rendering of lhs and rhs in `ensure!` messages

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 6ca7da9 · Browse files · 5 files+487 added−3 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+487 added−3 removed

  • tests:+5 added−3 removed
  • other:+482 added−0 removed

5 files+487 added−3 removed

added:src/ensure.rs+471 added
1use crate::{anyhow, Error};2use core::fmt::Debug;34#[doc(hidden)]5pub trait BothDebug {6 fn __dispatch_ensure(self, msg: &'static str) -> Error;7}89impl<A, B> BothDebug for (A, B)10where11 A: Debug,12 B: Debug,13{14 fn __dispatch_ensure(self, msg: &'static str) -> Error {15 anyhow!("{} ({:?} vs {:?})", msg, self.0, self.1)16 }17}1819#[doc(hidden)]20pub trait NotBothDebug {21 fn __dispatch_ensure(self, msg: &'static str) -> Error;22}2324impl<A, B> NotBothDebug for &(A, B) {25 fn __dispatch_ensure(self, msg: &'static str) -> Error {26 Error::msg(msg)27 }28}2930#[doc(hidden)]31#[macro_export]32macro_rules! __parse_ensure {33 (atom () $bail:tt {($($rhs:tt)+) ($($lhs:tt)+) $op:tt} $(,)?) => {34 $crate::__fancy_ensure!($($lhs)+, $op, $($rhs)+)35 };3637 // low precedence control flow constructs3839 (0 $stack:tt $bail:tt $parse:tt return $($rest:tt)*) => {40 $crate::__fallback_ensure!($bail)41 };4243 (0 $stack:tt $bail:tt $parse:tt break $($rest:tt)*) => {44 $crate::__fallback_ensure!($bail)45 };4647 (0 $stack:tt $bail:tt $parse:tt continue $($rest:tt)*) => {48 $crate::__fallback_ensure!($bail)49 };5051 (0 $stack:tt $bail:tt $parse:tt yield $($rest:tt)*) => {52 $crate::__fallback_ensure!($bail)53 };5455 (0 $stack:tt $bail:tt $parse:tt move $($rest:tt)*) => {56 $crate::__fallback_ensure!($bail)57 };5859 // unary operators6061 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} * $($rest:tt)*) => {62 $crate::__parse_ensure!(0 $stack $bail {($($buf)* *) $($parse)*} $($rest)*)63 };6465 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ! $($rest:tt)*) => {66 $crate::__parse_ensure!(0 $stack $bail {($($buf)* !) $($parse)*} $($rest)*)67 };6869 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} - $($rest:tt)*) => {70 $crate::__parse_ensure!(0 $stack $bail {($($buf)* -) $($parse)*} $($rest)*)71 };7273 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} let $(|)? $($pat:pat)|+ = $($rest:tt)*) => {74 $crate::__parse_ensure!(0 $stack $bail {($($buf)* let $($pat)|+ =) $($parse)*} $($rest)*)75 };7677 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $life:lifetime : $($rest:tt)*) => {78 $crate::__parse_ensure!(0 $stack $bail {($($buf)* $life :) $($parse)*} $($rest)*)79 };8081 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} &mut $($rest:tt)*) => {82 $crate::__parse_ensure!(0 $stack $bail {($($buf)* &mut) $($parse)*} $($rest)*)83 };8485 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} & $($rest:tt)*) => {86 $crate::__parse_ensure!(0 $stack $bail {($($buf)* &) $($parse)*} $($rest)*)87 };8889 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} &&mut $($rest:tt)*) => {90 $crate::__parse_ensure!(0 $stack $bail {($($buf)* &&mut) $($parse)*} $($rest)*)91 };9293 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} && $($rest:tt)*) => {94 $crate::__parse_ensure!(0 $stack $bail {($($buf)* &&) $($parse)*} $($rest)*)95 };9697 // control flow constructs9899 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} if $($rest:tt)*) => {100 $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* if) $($parse)*} $($rest)*)101 };102103 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} match $($rest:tt)*) => {104 $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* match) $($parse)*} $($rest)*)105 };106107 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} while $($rest:tt)*) => {108 $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* while) $($parse)*} $($rest)*)109 };110111 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} for $(|)? $($pat:pat)|+ in $($rest:tt)*) => {112 $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* for $($pat)|+ in) $($parse)*} $($rest)*)113 };114115 (atom (cond $stack:tt) $bail:tt {($($buf:tt)*) $($parse:tt)*} {$($block:tt)*} $($rest:tt)*) => {116 $crate::__parse_ensure!(cond $stack $bail {($($buf)* {$($block)*}) $($parse)*} $($rest)*)117 };118119 (cond $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} else if $($rest:tt)*) => {120 $crate::__parse_ensure!(0 (cond $stack) $bail {($($buf)* else if) $($parse)*} $($rest)*)121 };122123 (cond $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} else {$($block:tt)*} $($rest:tt)*) => {124 $crate::__parse_ensure!(atom $stack $bail {($($buf)* else {$($block)*}) $($parse)*} $($rest)*)125 };126127 (cond $stack:tt $bail:tt $parse:tt $($rest:tt)*) => {128 $crate::__parse_ensure!(atom $stack $bail $parse $($rest)*)129 };130131 // atomic expressions132133 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ($($paren:tt)*) $($rest:tt)*) => {134 $crate::__parse_ensure!(atom $stack $bail {($($buf)* ($($paren)*)) $($parse)*} $($rest)*)135 };136137 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} [$($array:tt)*] $($rest:tt)*) => {138 $crate::__parse_ensure!(atom $stack $bail {($($buf)* [$($array)*]) $($parse)*} $($rest)*)139 };140141 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} {$($block:tt)*} $($rest:tt)*) => {142 $crate::__parse_ensure!(atom $stack $bail {($($buf)* {$($block)*}) $($parse)*} $($rest)*)143 };144145 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} loop {$($block:tt)*} $($rest:tt)*) => {146 $crate::__parse_ensure!(atom $stack $bail {($($buf)* loop {$($block)*}) $($parse)*} $($rest)*)147 };148149 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} async {$($block:tt)*} $($rest:tt)*) => {150 $crate::__parse_ensure!(atom $stack $bail {($($buf)* async {$($block)*}) $($parse)*} $($rest)*)151 };152153 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} async move {$($block:tt)*} $($rest:tt)*) => {154 $crate::__parse_ensure!(atom $stack $bail {($($buf)* async move {$($block)*}) $($parse)*} $($rest)*)155 };156157 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} unsafe {$($block:tt)*} $($rest:tt)*) => {158 $crate::__parse_ensure!(atom $stack $bail {($($buf)* unsafe {$($block)*}) $($parse)*} $($rest)*)159 };160161 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $lit:literal $($rest:tt)*) => {162 $crate::__parse_ensure!(atom $stack $bail {($($buf)* $lit) $($parse)*} $($rest)*)163 };164165 // path expressions166167 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} :: $($rest:tt)*) => {168 $crate::__parse_ensure!(path $stack $bail {($($buf)* ::) $($parse)*} $($rest)*)169 };170171 (0 $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $ident:ident $($rest:tt)*) => {172 $crate::__parse_ensure!(component $stack $bail {($($buf)* $ident) $($parse)*} $($rest)*)173 };174175 (path $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $ident:ident $($rest:tt)*) => {176 $crate::__parse_ensure!(component $stack $bail {($($buf)* $ident) $($parse)*} $($rest)*)177 };178179 (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} :: < $($rest:tt)*) => {180 $crate::__parse_ensure!(generic (component $stack) $bail {($($buf)* :: <) $($parse)*} $($rest)*)181 };182183 (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} :: << $($rest:tt)*) => {184 $crate::__parse_ensure!(generic (component $stack) $bail {($($buf)* :: <) $($parse)*} < $($rest)*)185 };186187 (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} :: $($rest:tt)*) => {188 $crate::__parse_ensure!(path $stack $bail {($($buf)* ::) $($parse)*} $($rest)*)189 };190191 // macro invocations192193 (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ! ($($mac:tt)*) $($rest:tt)*) => {194 $crate::__parse_ensure!(atom $stack $bail {($($buf)* ! ($($mac)*)) $($parse)*} $($rest)*)195 };196197 (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ! [$($mac:tt)*] $($rest:tt)*) => {198 $crate::__parse_ensure!(atom $stack $bail {($($buf)* ! [$($mac)*]) $($parse)*} $($rest)*)199 };200201 (component $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ! {$($mac:tt)*} $($rest:tt)*) => {202 $crate::__parse_ensure!(atom $stack $bail {($($buf)* ! {$($mac)*}) $($parse)*} $($rest)*)203 };204205 (component $stack:tt $bail:tt $parse:tt $($rest:tt)*) => {206 $crate::__parse_ensure!(atom $stack $bail $parse $($rest)*)207 };208209 // trailer expressions210211 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ($($call:tt)*) $($rest:tt)*) => {212 $crate::__parse_ensure!(atom $stack $bail {($($buf)* ($($call)*)) $($parse)*} $($rest)*)213 };214215 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} [$($index:tt)*] $($rest:tt)*) => {216 $crate::__parse_ensure!(atom $stack $bail {($($buf)* [$($index)*]) $($parse)*} $($rest)*)217 };218219 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} {$($init:tt)*} $($rest:tt)*) => {220 $crate::__parse_ensure!(atom $stack $bail {($($buf)* {$($init)*}) $($parse)*} $($rest)*)221 };222223 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ? $($rest:tt)*) => {224 $crate::__parse_ensure!(atom $stack $bail {($($buf)* ?) $($parse)*} $($rest)*)225 };226227 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} . $ident:ident :: < $($rest:tt)*) => {228 $crate::__parse_ensure!(generic (atom $stack) $bail {($($buf)* . $ident :: <) $($parse)*} $($rest)*)229 };230231 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} . $ident:ident :: << $($rest:tt)*) => {232 $crate::__parse_ensure!(generic (atom $stack) $bail {($($buf)* . $ident :: <) $($parse)*} < $($rest)*)233 };234235 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} . $ident:ident $($rest:tt)*) => {236 $crate::__parse_ensure!(atom $stack $bail {($($buf)* . $ident) $($parse)*} $($rest)*)237 };238239 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} . $lit:tt $($rest:tt)*) => {240 $crate::__parse_ensure!(atom $stack $bail {($($buf)* . $lit) $($parse)*} $($rest)*)241 };242243 // angle bracketed generic arguments244245 (generic ($pop:ident $stack:tt) $bail:tt {($($buf:tt)*) $($parse:tt)*} > $($rest:tt)*) => {246 $crate::__parse_ensure!($pop $stack $bail {($($buf)* >) $($parse)*} $($rest)*)247 };248249 (generic $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $lit:literal $($rest:tt)*) => {250 $crate::__parse_ensure!(arglist $stack $bail {($($buf)* $lit) $($parse)*} $($rest)*)251 };252253 (generic $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} {$($block:tt)*} $($rest:tt)*) => {254 $crate::__parse_ensure!(arglist $stack $bail {($($buf)* {$($block)*}) $($parse)*} $($rest)*)255 };256257 (generic $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $life:lifetime $($rest:tt)*) => {258 $crate::__parse_ensure!(arglist $stack $bail {($($buf)* $life) $($parse)*} $($rest)*)259 };260261 (generic $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} $ty:ty , $($rest:tt)*) => {262 $crate::__parse_ensure!(generic $stack $bail {($($buf)* $ty ,) $($parse)*} $($rest)*)263 };264265 (generic ($pop:ident $stack:tt) $bail:tt {($($buf:tt)*) $($parse:tt)*} $ty:ty > $($rest:tt)*) => {266 $crate::__parse_ensure!($pop $stack $bail {($($buf)* $ty >) $($parse)*} $($rest)*)267 };268269 (arglist $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} , $($rest:tt)*) => {270 $crate::__parse_ensure!(generic $stack $bail {($($buf)* ,) $($parse)*} $($rest)*)271 };272273 (arglist ($pop:ident $stack:tt) $bail:tt {($($buf:tt)*) $($parse:tt)*} > $($rest:tt)*) => {274 $crate::__parse_ensure!($pop $stack $bail {($($buf)* >) $($parse)*} $($rest)*)275 };276277 // high precedence binary operators278279 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} + $($rest:tt)*) => {280 $crate::__parse_ensure!(0 $stack $bail {($($buf)* +) $($parse)*} $($rest)*)281 };282283 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} - $($rest:tt)*) => {284 $crate::__parse_ensure!(0 $stack $bail {($($buf)* -) $($parse)*} $($rest)*)285 };286287 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} * $($rest:tt)*) => {288 $crate::__parse_ensure!(0 $stack $bail {($($buf)* *) $($parse)*} $($rest)*)289 };290291 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} / $($rest:tt)*) => {292 $crate::__parse_ensure!(0 $stack $bail {($($buf)* /) $($parse)*} $($rest)*)293 };294295 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} % $($rest:tt)*) => {296 $crate::__parse_ensure!(0 $stack $bail {($($buf)* %) $($parse)*} $($rest)*)297 };298299 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} ^ $($rest:tt)*) => {300 $crate::__parse_ensure!(0 $stack $bail {($($buf)* ^) $($parse)*} $($rest)*)301 };302303 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} & $($rest:tt)*) => {304 $crate::__parse_ensure!(0 $stack $bail {($($buf)* &) $($parse)*} $($rest)*)305 };306307 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} | $($rest:tt)*) => {308 $crate::__parse_ensure!(0 $stack $bail {($($buf)* |) $($parse)*} $($rest)*)309 };310311 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} << $($rest:tt)*) => {312 $crate::__parse_ensure!(0 $stack $bail {($($buf)* <<) $($parse)*} $($rest)*)313 };314315 (atom $stack:tt $bail:tt {($($buf:tt)*) $($parse:tt)*} >> $($rest:tt)*) => {316 $crate::__parse_ensure!(0 $stack $bail {($($buf)* >>) $($parse)*} $($rest)*)317 };318319 // comparison binary operators320321 (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} == $($rest:tt)*) => {322 $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) ==} $($rest)*)323 };324325 (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} == $($rest:tt)*) => {326 $crate::__parse_ensure!(0 $stack $bail {($($buf)* ==) $($parse)*} $($rest)*)327 };328329 (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} <= $($rest:tt)*) => {330 $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) <=} $($rest)*)331 };332333 (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} <= $($rest:tt)*) => {334 $crate::__parse_ensure!(0 $stack $bail {($($buf)* <=) $($parse)*} $($rest)*)335 };336337 (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} < $($rest:tt)*) => {338 $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) <} $($rest)*)339 };340341 (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} < $($rest:tt)*) => {342 $crate::__parse_ensure!(0 $stack $bail {($($buf)* <) $($parse)*} $($rest)*)343 };344345 (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} != $($rest:tt)*) => {346 $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) !=} $($rest)*)347 };348349 (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} != $($rest:tt)*) => {350 $crate::__parse_ensure!(0 $stack $bail {($($buf)* !=) $($parse)*} $($rest)*)351 };352353 (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} >= $($rest:tt)*) => {354 $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) >=} $($rest)*)355 };356357 (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} >= $($rest:tt)*) => {358 $crate::__parse_ensure!(0 $stack $bail {($($buf)* >=) $($parse)*} $($rest)*)359 };360361 (atom () $bail:tt {($($buf:tt)*) $($parse:tt)*} > $($rest:tt)*) => {362 $crate::__parse_ensure!(0 () $bail {() $($parse)* ($($buf)*) >} $($rest)*)363 };364365 (atom $stack:tt $bail:tt {($($buf:tt)+) $($parse:tt)*} > $($rest:tt)*) => {366 $crate::__parse_ensure!(0 $stack $bail {($($buf)* >) $($parse)*} $($rest)*)367 };368369 // low precedence binary operators370371 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} && $($rest:tt)*) => {372 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* &&) $($parse)*} $($rest)*)373 };374375 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} || $($rest:tt)*) => {376 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* ||) $($parse)*} $($rest)*)377 };378379 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} = $($rest:tt)*) => {380 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* =) $($parse)*} $($rest)*)381 };382383 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} += $($rest:tt)*) => {384 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* +=) $($parse)*} $($rest)*)385 };386387 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} -= $($rest:tt)*) => {388 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* -=) $($parse)*} $($rest)*)389 };390391 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} *= $($rest:tt)*) => {392 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* *=) $($parse)*} $($rest)*)393 };394395 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} /= $($rest:tt)*) => {396 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* /=) $($parse)*} $($rest)*)397 };398399 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} %= $($rest:tt)*) => {400 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* %=) $($parse)*} $($rest)*)401 };402403 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} ^= $($rest:tt)*) => {404 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* ^=) $($parse)*} $($rest)*)405 };406407 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} &= $($rest:tt)*) => {408 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* &=) $($parse)*} $($rest)*)409 };410411 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} |= $($rest:tt)*) => {412 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* |=) $($parse)*} $($rest)*)413 };414415 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} <<= $($rest:tt)*) => {416 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* <<=) $($parse)*} $($rest)*)417 };418419 (atom ($($stack:tt)+) $bail:tt {($($buf:tt)*) $($parse:tt)*} >>= $($rest:tt)*) => {420 $crate::__parse_ensure!(0 ($($stack)*) $bail {($($buf)* >>=) $($parse)*} $($rest)*)421 };422423 // unrecognized expression424425 ($state:tt $stack:tt $bail:tt $($rest:tt)*) => {426 $crate::__fallback_ensure!($bail)427 };428}429430#[doc(hidden)]431#[macro_export]432macro_rules! __fancy_ensure {433 ($lhs:expr, $op:tt, $rhs:expr) => {434 match (&$lhs, &$rhs) {435 (lhs, rhs) => {436 if !(lhs $op rhs) {437 #[allow(unused_imports)]438 use $crate::private::{BothDebug, NotBothDebug};439 return Err((lhs, rhs).__dispatch_ensure(concat!("Condition failed: `", stringify!($lhs), " ", stringify!($op), " ", stringify!($rhs), "`")));440 }441 }442 }443 };444}445446#[doc(hidden)]447#[macro_export]448macro_rules! __fallback_ensure {449 (($cond:expr $(,)?)) => {450 if !$cond {451 return $crate::private::Err($crate::Error::msg(452 $crate::private::concat!("Condition failed: `", $crate::private::stringify!($cond), "`")453 ));454 }455 };456 (($cond:expr, $msg:literal $(,)?)) => {457 if !$cond {458 return $crate::private::Err($crate::anyhow!($msg));459 }460 };461 (($cond:expr, $err:expr $(,)?)) => {462 if !$cond {463 return $crate::private::Err($crate::anyhow!($err));464 }465 };466 (($cond:expr, $fmt:expr, $($arg:tt)*)) => {467 if !$cond {468 return $crate::private::Err($crate::anyhow!($fmt, $($arg)*));469 }470 };471}
modified:src/lib.rs+2 added
235 unmodified lines
236#[macro_use]237mod backtrace;238mod chain;239mod context;240mod ensure;241mod error;242mod fmt;243mod kind;244mod macros;
363 unmodified lines
608 use crate::Error;609 use alloc::fmt;610 use core::fmt::Arguments;611612 pub use crate::ensure::{BothDebug, NotBothDebug};613 pub use alloc::format;614 pub use core::result::Result::Err;615 pub use core::{concat, format_args, stringify};616
26 unmodified lines
modified:src/macros.rs+9 added
131 unmodified lines
132/// ensure!(depth <= MAX_DEPTH, ScienceError::RecursionLimitExceeded);133/// # Ok(())134/// # }135/// ```136#[cfg(doc)]137#[macro_export]138macro_rules! ensure {139 ($cond:expr $(,)?) => {140 if !$cond {
18 unmodified lines
159 }160 };161}162163#[cfg(not(doc))]164#[macro_export]165macro_rules! ensure {166 ($($tt:tt)*) => {167 $crate::__parse_ensure!(0 () ($($tt)*) {()} $($tt)*)168 };169}170171/// Construct an ad-hoc error from a string or existing non-`anyhow` error172/// value.173///174/// This evaluates to an [`Error`][crate::Error]. It can take either just a
41 unmodified lines
modified:tests/ui/empty-ensure.stderr+4 added-2 removed
1error: unexpected end of macro invocation1error: no rules expected the token `)`2 --> tests/ui/empty-ensure.rs:4:53 |44 | ensure!();5 | ^^^^^^^^^ missing tokens in macro arguments5 | ^^^^^^^^^ no rules expected this token in macro call6 |7 = note: this error originates in the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info)
modified:tests/test_macros.rs+1 added-1 removed
45 unmodified lines
46 Ok(())47 };48 assert_eq!(49 f().unwrap_err().to_string(),50 "Condition failed: `v + v == 1`",50 "Condition failed: `v + v == 1` (2 vs 1)",51 );52}5354#[test]
25 unmodified lines