| 1 | //! Contains the macros `try_or!`, `try_or_else!`, `try_opt_or!` and |
| 2 | //! `try_opt_or_else!`. These are helper macros for unwrapping a `Result` or an |
| 3 | //! `Option` while returning early for `Err` and `None` values. The semantics |
| 4 | //! are similar to `unwrap_or` and `unwrap_or_else`. |
| 5 | //! |
| 6 | //! If you want a `try_opt!` macro, there's already another crate (`try_opt`) |
| 7 | //! for that. |
| 8 | |
| 9 | #![no_std] |
| 10 | |
| 11 | |
| 12 | /// Helper macro for unwrapping `Result` values. Returns early with the value |
| 13 | /// of the second parameter if the first parameter is `Err`. |
| 14 | #[macro_export] |
| 15 | macro_rules! try_orrust-analyzer cargo try_or 0.2.0 try_or! { |
| 16 | ( $expr:expr , $or:expr ) => {{ |
| 17 | match { $expr } { |
| 18 | Ok(res) => res, |
| 19 | Err(_) => { return { $or }; }, |
| 20 | } |
| 21 | }} |
| 22 | } |
| 23 | |
| 24 | /// Unwraps a `Result`. If the result is `Err`, calls the function `$or_fn` with |
| 25 | /// its value and returns early with its result. |
| 26 | #[macro_export] |
| 27 | macro_rules! try_or_elserust-analyzer cargo try_or 0.2.0 try_or_else! { |
| 28 | ( $expr:expr , $or_fn:expr ) => {{ |
| 29 | match { $expr } { |
| 30 | Ok(res) => res, |
| 31 | Err(err) => { return { $or_fn }(err); }, |
| 32 | } |
| 33 | }} |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /*/// Unwraps an `Option`, returning `None` if the option is `None`. |
| 38 | #[macro_export] |
| 39 | macro_rules! try_opt { |
| 40 | ( $expr:expr ) => {{ |
| 41 | match { $expr } { |
| 42 | Some(val) => val, |
| 43 | None => return None, |
| 44 | } |
| 45 | }} |
| 46 | }*/ |
| 47 | |
| 48 | /// Unwraps an `Option`, returning the second parameter if the option is `None`. |
| 49 | #[macro_export] |
| 50 | macro_rules! try_opt_orrust-analyzer cargo try_or 0.2.0 try_opt_or! { |
| 51 | ( $expr:expr , $or:expr ) => {{ |
| 52 | match { $expr } { |
| 53 | Some(res) => res, |
| 54 | None => return { $or }, |
| 55 | } |
| 56 | }} |
| 57 | } |
| 58 | |
| 59 | /// Unwraps an `Option`. If the result is `None`, calls the function `$or_fn` |
| 60 | /// and returns its result. |
| 61 | #[macro_export] |
| 62 | macro_rules! try_opt_or_elserust-analyzer cargo try_or 0.2.0 try_opt_or_else! { |
| 63 | ( $expr:expr , $or_fn:expr ) => {{ |
| 64 | match { $expr } { |
| 65 | Some(res) => res, |
| 66 | None => return { $or_fn }(), |
| 67 | } |
| 68 | }} |
| 69 | } |
| 70 | |
| 71 | |
| 72 | #[cfg(test)] |
| 73 | mod testsrust-analyzer cargo try_or 0.2.0 tests/ { |
| 74 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 75 | fn it_worksrust-analyzer cargo try_or 0.2.0 tests/it_works().() { |
| 76 | // test try_or! |
| 77 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!({ || try_orrust-analyzer cargo try_or 0.2.0 try_or!!("5".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u32>(), 1) } (), 5); |
| 78 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!({ || try_orrust-analyzer cargo try_or 0.2.0 try_or!!("a".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u32>(), 1) } (), 1); |
| 79 | |
| 80 | // test try_or_else! |
| 81 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!({ || try_or_elserust-analyzer cargo try_or 0.2.0 try_or_else!!("1".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u32>(), |_| 5) } (), 1); |
| 82 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!({ || try_or_elserust-analyzer cargo try_or 0.2.0 try_or_else!!("a".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u32>(), |_| 5) } (), 5); |
| 83 | |
| 84 | // test try_opt_or! |
| 85 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!({ || try_opt_orrust-analyzer cargo try_or 0.2.0 try_opt_or!!(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(1), 2) } (), 1); |
| 86 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!({ || try_opt_orrust-analyzer cargo try_or 0.2.0 try_opt_or!!(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, 2) } (), 2); |
| 87 | |
| 88 | // test try_or_else! |
| 89 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!({ || try_opt_or_elserust-analyzer cargo try_or 0.2.0 try_opt_or_else!!(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(1), || 2) } (), 1); |
| 90 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!({ || try_opt_or_elserust-analyzer cargo try_or 0.2.0 try_opt_or_else!!(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, || 2) } (), 2); |
| 91 | } |
| 92 | } |