af_core_macros/
util.rs

1// Copyright © 2020 Alexandra Frydl
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7/// Runs a block of code and catches `?` operator returns.
8///
9/// This is just a cleaner version of creating a closure and immediately calling
10/// it. It is intended to serve as a replacement for `try { .. }` until it is
11/// stable.
12#[macro_export]
13macro_rules! attempt {
14  ($($tokens:tt)+) => {
15    (|| { $($tokens)+ })()
16  };
17}
18
19/// Runs a block of async code and catches `?` operator returns.
20///
21/// This is just a cleaner version of creating a closure and immediately calling
22/// it. It is intended to serve as a replacement for `try { .. }` until it is
23/// stable.
24#[macro_export]
25macro_rules! attempt_async {
26  ($($tokens:tt)+) => {
27    (|| async { $($tokens)+ })().await
28  };
29}