tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Try.h (1477B)


      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/Likely.h"
     11 #include "mozilla/Result.h"
     12 
     13 /**
     14 * MOZ_TRY(expr) is the C++ equivalent of Rust's `target = try!(expr);`, using
     15 * gcc's statement expressions [0]. First, it evaluates expr, which must produce
     16 * a Result value. On success, the result's success value is 'returned' as
     17 * rvalue. On error, immediately returns the error result. This pattern allows
     18 * to directly assign the success value:
     19 *
     20 * ```
     21 * SuccessValue val = MOZ_TRY(Func());
     22 * ```
     23 *
     24 * Where `Func()` returns a `Result<SuccessValue, E>` and is called in a
     25 * function that returns `Result<T, E>`.
     26 *
     27 * [0]: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
     28 */
     29 #define MOZ_TRY(expr)                                     \
     30  __extension__({                                         \
     31    auto mozTryVarTempResult = ::mozilla::ToResult(expr); \
     32    if (MOZ_UNLIKELY(mozTryVarTempResult.isErr())) {      \
     33      return mozTryVarTempResult.propagateErr();          \
     34    }                                                     \
     35    mozTryVarTempResult.unwrap();                         \
     36  })
     37 
     38 #endif  // mozilla_Try_h