lazy_errors/
err.rs

1/// Creates an ad-hoc [`Error`](crate::Error)
2/// from some message or format string.
3///
4/// Use this macro if you want to bail early from a function
5/// that otherwise may return multiple errors
6/// or when your codebase is generally using the
7/// return type
8#[cfg_attr(
9    any(feature = "rust-v1.81", feature = "std"),
10    doc = r##"
11[`lazy_errors::Result`](crate::Result)
12or
13[`lazy_errors::surrogate_error_trait::Result`].
14
15 "##
16)]
17#[cfg_attr(
18    not(any(feature = "rust-v1.81", feature = "std")),
19    doc = r##"
20[`lazy_errors::surrogate_error_trait::Result`]
21(or `lazy_errors::Result` if any of
22the `rust-v1.81` or
23the `std` features is enabled).
24
25 "##
26)]
27/// [`lazy_errors::surrogate_error_trait::Result`]:
28/// crate::surrogate_error_trait::Result
29///
30/// A guard clause is a typical use case for this macro:
31///
32/// ```
33/// #[cfg(any(feature = "rust-v1.81", feature = "std"))]
34/// use lazy_errors::{err, Result};
35///
36/// #[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
37/// use lazy_errors::{err, surrogate_error_trait::Result};
38///
39/// fn handle_ascii(text: &str) -> Result<()> {
40///     if !text.is_ascii() {
41///         return Err(err!("Not ASCII: '{text}'"));
42///     }
43///
44///     // ... code that does the actual handling ...
45///     todo!()
46/// }
47///
48/// assert!(handle_ascii("🦀").is_err());
49/// ```
50#[macro_export]
51macro_rules! err {
52    ($($arg:tt)*) => {{
53        extern crate alloc;
54        $crate::Error::from_message(alloc::format!($($arg)*))
55    }};
56}