Debian Codegraph @

mfbt/Try.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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 #ifndef mozilla_Try_h
8 #define mozilla_Try_h
9
10 #include "mozilla/Result.h"
11
12 /**
13 * MOZ_TRY(expr) is the C++ equivalent of Rust's `target = try!(expr);`, using
14 * gcc's statement expressions [0]. First, it evaluates expr, which must produce
15 * a Result value. On success, the result's success value is 'returned' as
16 * rvalue. On error, immediately returns the error result. This pattern allows
17 * to directly assign the success value:
18 *
19 * ```
20 * SuccessValue val = MOZ_TRY(Func());
21 * ```
22 *
23 * Where `Func()` returns a `Result<SuccessValue, E>` and is called in a
24 * function that returns `Result<T, E>`.
25 *
26 * [0]: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
27 */
28 #define MOZ_TRY(expr) \
29 __extension__({ \
30 auto mozTryVarTempResult = ::mozilla::ToResult(expr); \
31 if (MOZ_UNLIKELY(mozTryVarTempResult.isErr())) { \
32 return mozTryVarTempResult.propagateErr(); \
33 } \
34 mozTryVarTempResult.unwrap(); \
35 })
36 /**
37 * MOZ_TRY_VAR(target, expr) is the C++ equivalent of Rust's `target =
38 * try!(expr);`. First, it evaluates expr, which must produce a Result value. On
39 * success, the result's success value is assigned to target. On error,
40 * immediately returns the error result. |target| must be an lvalue.
41 *
42 * This macro is obsolete and its usages should be replaced with `MOZ_TRY`.
43 */
44 #define MOZ_TRY_VAR(target, expr) (target) = MOZ_TRY(expr);
45
46 #endif // mozilla_Try_h