Bug 1608587 [wpt PR 21137] - Update wpt metadata, a=testonly
[gecko.git] / mfbt / ResultExtensions.h
blob905c4cad5d603c3295be36d36d0b60c5e8c290ba
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/. */
7 /* Extensions to the Result type to enable simpler handling of XPCOM/NSPR
8 * results. */
10 #ifndef mozilla_ResultExtensions_h
11 #define mozilla_ResultExtensions_h
13 #include "mozilla/Assertions.h"
14 #include "nscore.h"
15 #include "prtypes.h"
17 namespace mozilla {
19 // Allow nsresult errors to automatically convert to nsresult values, so MOZ_TRY
20 // can be used in XPCOM methods with Result<T, nserror> results.
21 template <>
22 class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult> {
23 nsresult mErrorValue;
25 template <typename V, typename E2>
26 friend class Result;
28 public:
29 explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue) {
30 MOZ_ASSERT(NS_FAILED(aErrorValue));
33 operator nsresult() { return mErrorValue; }
36 // Allow MOZ_TRY to handle `PRStatus` values.
37 inline Result<Ok, nsresult> ToResult(PRStatus aValue);
39 } // namespace mozilla
41 #include "mozilla/Result.h"
43 namespace mozilla {
45 inline Result<Ok, nsresult> ToResult(nsresult aValue) {
46 if (NS_FAILED(aValue)) {
47 return Err(aValue);
49 return Ok();
52 inline Result<Ok, nsresult> ToResult(PRStatus aValue) {
53 if (aValue == PR_SUCCESS) {
54 return Ok();
56 return Err(NS_ERROR_FAILURE);
59 } // namespace mozilla
61 #endif // mozilla_ResultExtensions_h