Comments (8)
The __parse_ensure
macro is used to allow the following error messages:
let (x, y) = (5, 6);
anyhow::ensure!(x == y);
into
Error: Condition failed: `x == y` (5 vs 6)
An example output of the macro
match (&x, &y) {
(lhs, rhs) => {
if !(lhs == rhs) {
#[allow(unused_imports)]
use ::anyhow::__private::{BothDebug, NotBothDebug};
return Err((lhs, rhs).__dispatch_ensure("Condition failed: `x == y`"));
}
}
}
It is complicated but necessary for these nicer error messages.
from anyhow.
Sure, this is valid. But the documentation is still misleading. (Especially since clicking source
leads to the wrong definition) and could the implementation of this not have been simpler?
from anyhow.
It's not really misleading, it's documentation on how to use the macro. The actual implementation would generate very useless documentation that would not serve the job of documentation - describing how the macro can be used.
from anyhow.
Sure, that can be in the docs itself though, rather than pointing to a completely different macro entirely.
The compile time influence of this is still unconsidered. See: std
having different assert
macros for special-cased error messages, rather than overloading a single macro.
from anyhow.
The current solution is not very accurate either, look at the expansion of this:
use anyhow::ensure;
ensure!(true == true || false == true);
use anyhow::ensure;
if !(true == true || false == true) {
return ::anyhow::__private::Err(::anyhow::Error::msg(
"Condition failed: `true == true || false == true`",
));
};
It doesn't detect the ==
operation occurring and add the specialized diagnostic.
The condition is parsed by syn
as true == (true || (false == true))
A proc macro for this should be much better, imo. It would both more accurately perform this and should hopefully be faster than the decl macro implementation right now. 🤷🏽♀️
from anyhow.
#325 (comment) is accurate about why it's set up this way. The input patterns shown are indeed the 4 patterns that the macro accepts. The real macro implementation just parses the same things token-by-token instead of through macro_rules's built-in $:expr parser.
from anyhow.
Sure. But the "source" of the macro according to the docs is still completely misleading.
When I click "source" on the docs, there's no comment or no indication at all (neither in the docs themselves nor in any comment in the source code) to the actual implementation of the macro. It just points to a pseudo-macro.
If I hadn't noticed the tiny cfgs, I would've been completely oblivious to this too.
This is still ignoring
- The compile time affect of this
- The accuracy of this (#325 (comment))
Hacking together a semi-parser of Rust code in a declarative macro seems unnecessary and a misuse of them. A proc-macro seems much better suited here?
from anyhow.
If the point of the cfg(docs) macro is to simply be a place holder for the docs, why implement at all?
Especially since the implementation of the only-docs macro is not the expansion of the actual macro
from anyhow.
Related Issues (20)
- Nightly feature wrongly enabled on stable toolchain HOT 3
- Should `anyhow::Error::chain` return `dyn Error + Send + Sync` HOT 2
- Customize backtrace logic?
- `anyhow!(e)` doesn't preserve `source` for `&Error` HOT 1
- A way to disable anyhow stacktraces (without disabling stacktraces from other crates) HOT 2
- Default Ok-type to `()` in `anyhow::Result` typedef HOT 2
- Updating from version 1.0.76 breaks backtraces HOT 2
- Make backtrace support optional HOT 10
- Possible performance regression on Windows HOT 5
- as_ref() type must be known at this point
- Depending on `CARGO_ENCODED_RUSTFLAGS` may produce stale builds HOT 1
- rust-analyzer nightly throws needless_return warning on bail! HOT 2
- Implement Context for Error
- Question regarding stacktrace
- anyhow::Error to Box<dyn Error> isn't compatible with other libraries HOT 2
- Are you open to de-duplicating the build.rs build probe code? HOT 7
- `anyhow::ensure!` doesn't work with custom error type HOT 1
- Short backtrace HOT 2
- Feature suggestion: ensure_some!() for unwrapping options HOT 3
- Cannot compile when using `ensure!` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anyhow.