]>
git.proxmox.com Git - rustc.git/blob - vendor/anyhow/src/macros.rs
1 /// Return early with an error.
3 /// This macro is equivalent to `return Err(`[`anyhow!($args...)`][anyhow!]`)`.
5 /// The surrounding function's or closure's return value is required to be
6 /// `Result<_,`[`anyhow::Error`][crate::Error]`>`.
11 /// # use anyhow::{bail, Result};
13 /// # fn has_permission(user: usize, resource: usize) -> bool {
17 /// # fn main() -> Result<()> {
19 /// # let resource = 0;
21 /// if !has_permission(user, resource) {
22 /// bail!("permission denied for accessing {}", resource);
29 /// # use anyhow::{bail, Result};
30 /// # use thiserror::Error;
32 /// # const MAX_DEPTH: usize = 1;
34 /// #[derive(Error, Debug)]
35 /// enum ScienceError {
36 /// #[error("recursion limit exceeded")]
37 /// RecursionLimitExceeded,
39 /// # More = (stringify! {
44 /// # fn main() -> Result<()> {
47 /// if depth > MAX_DEPTH {
48 /// bail!(ScienceError::RecursionLimitExceeded);
55 ($msg
:literal $
(,)?
) => {
56 return $
crate::private
::Err($
crate::anyhow
!($msg
))
58 ($err
:expr $
(,)?
) => {
59 return $
crate::private
::Err($
crate::anyhow
!($err
))
61 ($fmt
:expr
, $
($arg
:tt
)*) => {
62 return $
crate::private
::Err($
crate::anyhow
!($fmt
, $
($arg
)*))
66 /// Return early with an error if a condition is not satisfied.
68 /// This macro is equivalent to `if !$cond { return
69 /// Err(`[`anyhow!($args...)`][anyhow!]`); }`.
71 /// The surrounding function's or closure's return value is required to be
72 /// `Result<_,`[`anyhow::Error`][crate::Error]`>`.
74 /// Analogously to `assert!`, `ensure!` takes a condition and exits the function
75 /// if the condition fails. Unlike `assert!`, `ensure!` returns an `Error`
76 /// rather than panicking.
81 /// # use anyhow::{ensure, Result};
83 /// # fn main() -> Result<()> {
86 /// ensure!(user == 0, "only user 0 is allowed");
92 /// # use anyhow::{ensure, Result};
93 /// # use thiserror::Error;
95 /// # const MAX_DEPTH: usize = 1;
97 /// #[derive(Error, Debug)]
98 /// enum ScienceError {
99 /// #[error("recursion limit exceeded")]
100 /// RecursionLimitExceeded,
101 /// # #[error("...")]
102 /// # More = (stringify! {
107 /// # fn main() -> Result<()> {
110 /// ensure!(depth <= MAX_DEPTH, ScienceError::RecursionLimitExceeded);
115 macro_rules
! ensure
{
116 ($cond
:expr
, $msg
:literal $
(,)?
) => {
118 return $
crate::private
::Err($
crate::anyhow
!($msg
));
121 ($cond
:expr
, $err
:expr $
(,)?
) => {
123 return $
crate::private
::Err($
crate::anyhow
!($err
));
126 ($cond
:expr
, $fmt
:expr
, $
($arg
:tt
)*) => {
128 return $
crate::private
::Err($
crate::anyhow
!($fmt
, $
($arg
)*));
133 /// Construct an ad-hoc error from a string or existing non-`anyhow` error
136 /// This evaluates to an [`Error`][crate::Error]. It can take either just a
137 /// string, or a format string with arguments. It also can take any custom type
138 /// which implements `Debug` and `Display`.
140 /// If called with a single argument whose type implements `std::error::Error`
141 /// (in addition to `Debug` and `Display`, which are always required), then that
142 /// Error impl's `source` is preserved as the `source` of the resulting
150 /// use anyhow::{anyhow, Result};
152 /// fn lookup(key: &str) -> Result<V> {
153 /// if key.len() != 16 {
154 /// return Err(anyhow!("key length must be 16 characters, got {:?}", key));
162 macro_rules
! anyhow
{
163 ($msg
:literal $
(,)?
) => {
164 // Handle $:literal as a special case to make cargo-expanded code more
165 // concise in the common case.
166 $
crate::private
::new_adhoc($msg
)
168 ($err
:expr $
(,)?
) => ({
169 use $
crate::private
::kind
::*;
171 (&error
).anyhow_kind().new(error
)
173 ($fmt
:expr
, $
($arg
:tt
)*) => {
174 $
crate::private
::new_adhoc(format
!($fmt
, $
($arg
)*))