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/. */
10 #include "mozilla/Result.h"
13 * MOZ_TRY(expr) is the C++ equivalent of Rust's `try!(expr);`. First, it
14 * evaluates expr, which must produce a Result value. On success, it
15 * discards the result altogether. On error, it immediately returns an error
16 * Result from the enclosing function.
18 #define MOZ_TRY(expr) \
20 auto mozTryTempResult_ = ::mozilla::ToResult(expr); \
21 if (MOZ_UNLIKELY(mozTryTempResult_.isErr())) { \
22 return mozTryTempResult_.propagateErr(); \
27 * MOZ_TRY_VAR(target, expr) is the C++ equivalent of Rust's `target =
28 * try!(expr);`. First, it evaluates expr, which must produce a Result value. On
29 * success, the result's success value is assigned to target. On error,
30 * immediately returns the error result. |target| must be an lvalue.
32 #define MOZ_TRY_VAR(target, expr) \
34 auto mozTryVarTempResult_ = (expr); \
35 if (MOZ_UNLIKELY(mozTryVarTempResult_.isErr())) { \
36 return mozTryVarTempResult_.propagateErr(); \
38 (target) = mozTryVarTempResult_.unwrap(); \
41 #endif // mozilla_Try_h