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
10 #ifndef mozilla_ResultExtensions_h
11 #define mozilla_ResultExtensions_h
13 #include "mozilla/Assertions.h"
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.
22 class MOZ_MUST_USE_TYPE GenericErrorResult
<nsresult
> {
25 template <typename V
, typename E2
>
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"
45 inline Result
<Ok
, nsresult
> ToResult(nsresult aValue
) {
46 if (NS_FAILED(aValue
)) {
52 inline Result
<Ok
, nsresult
> ToResult(PRStatus aValue
) {
53 if (aValue
== PR_SUCCESS
) {
56 return Err(NS_ERROR_FAILURE
);
59 } // namespace mozilla
61 #endif // mozilla_ResultExtensions_h