Bug 1769547 - Do not MOZ_CRASH() on missing process r=nika
[gecko.git] / mfbt / ResultVariant.h
blobf93edbd6a99a3ff5756667820f42475d350f5a1f
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 /* A type suitable for returning either a value or an error from a function. */
9 #ifndef mozilla_ResultVariant_h
10 #define mozilla_ResultVariant_h
12 #include "mozilla/MaybeStorageBase.h"
13 #include "mozilla/Result.h"
14 #include "mozilla/Variant.h"
16 namespace mozilla::detail {
18 template <typename V, typename E>
19 class ResultImplementation<V, E, PackingStrategy::Variant> {
20 mozilla::Variant<V, E> mStorage;
22 public:
23 ResultImplementation(ResultImplementation&&) = default;
24 ResultImplementation(const ResultImplementation&) = delete;
25 ResultImplementation& operator=(const ResultImplementation&) = delete;
26 ResultImplementation& operator=(ResultImplementation&&) = default;
28 explicit ResultImplementation(V&& aValue)
29 : mStorage(std::forward<V>(aValue)) {}
30 explicit ResultImplementation(const V& aValue) : mStorage(aValue) {}
31 template <typename... Args>
32 explicit ResultImplementation(std::in_place_t, Args&&... aArgs)
33 : mStorage(VariantType<V>{}, std::forward<Args>(aArgs)...) {}
35 explicit ResultImplementation(const E& aErrorValue) : mStorage(aErrorValue) {}
36 explicit ResultImplementation(E&& aErrorValue)
37 : mStorage(std::forward<E>(aErrorValue)) {}
39 bool isOk() const { return mStorage.template is<V>(); }
41 // The callers of these functions will assert isOk() has the proper value, so
42 // these functions (in all ResultImplementation specializations) don't need
43 // to do so.
44 V unwrap() { return std::move(mStorage.template as<V>()); }
45 const V& inspect() const { return mStorage.template as<V>(); }
47 E unwrapErr() { return std::move(mStorage.template as<E>()); }
48 const E& inspectErr() const { return mStorage.template as<E>(); }
51 } // namespace mozilla::detail
53 #endif // mozilla_ResultVariant_h