2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: set ts=8 sts=2 et sw=2 tw=80:
|
2016-11-21 14:52:03 +03:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* A type suitable for returning either a value or an error from a function. */
|
|
|
|
|
|
|
|
#ifndef mozilla_Result_h
|
|
|
|
#define mozilla_Result_h
|
|
|
|
|
2020-09-21 16:14:53 +03:00
|
|
|
#include <algorithm>
|
2020-09-22 11:52:51 +03:00
|
|
|
#include <cstdint>
|
2020-09-21 16:14:53 +03:00
|
|
|
#include <cstring>
|
2019-10-04 23:44:50 +03:00
|
|
|
#include <type_traits>
|
2016-11-21 14:52:03 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2020-09-02 20:55:55 +03:00
|
|
|
#include "mozilla/CompactPair.h"
|
2021-03-23 15:35:19 +03:00
|
|
|
#include "mozilla/MaybeStorageBase.h"
|
2016-11-21 14:52:03 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty struct, indicating success for operations that have no return value.
|
|
|
|
* For example, if you declare another empty struct `struct OutOfMemory {};`,
|
|
|
|
* then `Result<Ok, OutOfMemory>` represents either success or OOM.
|
|
|
|
*/
|
|
|
|
struct Ok {};
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
class GenericErrorResult;
|
2016-12-22 00:05:56 +03:00
|
|
|
template <typename V, typename E>
|
|
|
|
class Result;
|
2016-11-21 14:52:03 +03:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2017-02-07 21:57:43 +03:00
|
|
|
enum class PackingStrategy {
|
|
|
|
Variant,
|
|
|
|
NullIsOk,
|
|
|
|
LowBitTagIsError,
|
2017-02-07 21:57:43 +03:00
|
|
|
PackedVariant,
|
2017-02-07 21:57:43 +03:00
|
|
|
};
|
|
|
|
|
2020-09-03 17:14:41 +03:00
|
|
|
template <typename T>
|
|
|
|
struct UnusedZero;
|
|
|
|
|
2017-02-07 21:57:43 +03:00
|
|
|
template <typename V, typename E, PackingStrategy Strategy>
|
|
|
|
class ResultImplementation;
|
2016-11-21 14:52:03 +03:00
|
|
|
|
2020-09-03 17:14:41 +03:00
|
|
|
template <typename V>
|
2021-03-23 15:35:19 +03:00
|
|
|
struct EmptyWrapper : V {
|
|
|
|
constexpr EmptyWrapper() = default;
|
|
|
|
explicit constexpr EmptyWrapper(const V&) {}
|
|
|
|
explicit constexpr EmptyWrapper(std::in_place_t) {}
|
|
|
|
|
2020-11-23 18:48:53 +03:00
|
|
|
constexpr V* addr() { return this; }
|
|
|
|
constexpr const V* addr() const { return this; }
|
2020-09-03 17:14:41 +03:00
|
|
|
};
|
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
// The purpose of AlignedStorageOrEmpty is to make an empty class look like
|
|
|
|
// std::aligned_storage_t for the purposes of the PackingStrategy::NullIsOk
|
|
|
|
// specializations of ResultImplementation below. We can't use
|
|
|
|
// std::aligned_storage_t itself with an empty class, since it would no longer
|
|
|
|
// be empty.
|
2020-09-03 17:14:41 +03:00
|
|
|
template <typename V>
|
2021-03-23 15:35:19 +03:00
|
|
|
using AlignedStorageOrEmpty =
|
|
|
|
std::conditional_t<std::is_empty_v<V>, EmptyWrapper<V>,
|
|
|
|
MaybeStorageBase<V>>;
|
2020-09-02 20:55:55 +03:00
|
|
|
|
2017-02-07 21:57:43 +03:00
|
|
|
template <typename V, typename E>
|
2020-09-03 17:14:41 +03:00
|
|
|
class ResultImplementationNullIsOkBase {
|
|
|
|
protected:
|
|
|
|
using ErrorStorageType = typename UnusedZero<E>::StorageType;
|
|
|
|
|
|
|
|
static constexpr auto kNullValue = UnusedZero<E>::nullValue;
|
2020-09-16 16:58:49 +03:00
|
|
|
|
2020-09-03 17:14:41 +03:00
|
|
|
static_assert(std::is_trivially_copyable_v<ErrorStorageType>);
|
2020-09-29 11:09:02 +03:00
|
|
|
|
|
|
|
// XXX This can't be statically asserted in general, if ErrorStorageType is
|
|
|
|
// not a basic type. With C++20 bit_cast, we could probably re-add such as
|
|
|
|
// assertion. static_assert(kNullValue == decltype(kNullValue)(0));
|
2020-09-03 17:14:41 +03:00
|
|
|
|
|
|
|
CompactPair<AlignedStorageOrEmpty<V>, ErrorStorageType> mValue;
|
2016-11-21 14:52:03 +03:00
|
|
|
|
|
|
|
public:
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr ResultImplementationNullIsOkBase(const V& aSuccessValue)
|
|
|
|
: mValue(aSuccessValue, kNullValue) {}
|
|
|
|
explicit constexpr ResultImplementationNullIsOkBase(V&& aSuccessValue)
|
|
|
|
: mValue(std::move(aSuccessValue), kNullValue) {}
|
2020-09-21 16:15:13 +03:00
|
|
|
template <typename... Args>
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr ResultImplementationNullIsOkBase(std::in_place_t,
|
|
|
|
Args&&... aArgs)
|
|
|
|
: mValue(std::piecewise_construct,
|
|
|
|
std::tuple(std::in_place, std::forward<Args>(aArgs)...),
|
|
|
|
std::tuple(kNullValue)) {}
|
|
|
|
explicit constexpr ResultImplementationNullIsOkBase(E aErrorValue)
|
2020-09-21 16:14:28 +03:00
|
|
|
: mValue(std::piecewise_construct, std::tuple<>(),
|
|
|
|
std::tuple(UnusedZero<E>::Store(std::move(aErrorValue)))) {
|
2020-09-03 17:14:41 +03:00
|
|
|
MOZ_ASSERT(mValue.second() != kNullValue);
|
|
|
|
}
|
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr ResultImplementationNullIsOkBase(
|
|
|
|
ResultImplementationNullIsOkBase&& aOther)
|
2020-09-03 17:14:41 +03:00
|
|
|
: mValue(std::piecewise_construct, std::tuple<>(),
|
2020-09-29 11:09:02 +03:00
|
|
|
std::tuple(aOther.mValue.second())) {
|
2020-09-03 17:14:41 +03:00
|
|
|
if constexpr (!std::is_empty_v<V>) {
|
|
|
|
if (isOk()) {
|
|
|
|
new (mValue.first().addr()) V(std::move(*aOther.mValue.first().addr()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResultImplementationNullIsOkBase& operator=(
|
|
|
|
ResultImplementationNullIsOkBase&& aOther) {
|
|
|
|
if constexpr (!std::is_empty_v<V>) {
|
|
|
|
if (isOk()) {
|
|
|
|
mValue.first().addr()->~V();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mValue.second() = std::move(aOther.mValue.second());
|
|
|
|
if constexpr (!std::is_empty_v<V>) {
|
|
|
|
if (isOk()) {
|
|
|
|
new (mValue.first().addr()) V(std::move(*aOther.mValue.first().addr()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2016-11-21 14:52:03 +03:00
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr bool isOk() const { return mValue.second() == kNullValue; }
|
2016-11-21 14:52:03 +03:00
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr const V& inspect() const { return *mValue.first().addr(); }
|
|
|
|
constexpr V unwrap() { return std::move(*mValue.first().addr()); }
|
2019-08-13 11:26:18 +03:00
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr decltype(auto) inspectErr() const {
|
2020-09-03 17:14:41 +03:00
|
|
|
return UnusedZero<E>::Inspect(mValue.second());
|
|
|
|
}
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr E unwrapErr() { return UnusedZero<E>::Unwrap(mValue.second()); }
|
2020-09-03 17:14:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename V, typename E,
|
|
|
|
bool IsVTriviallyDestructible = std::is_trivially_destructible_v<V>>
|
|
|
|
class ResultImplementationNullIsOk;
|
|
|
|
|
|
|
|
template <typename V, typename E>
|
|
|
|
class ResultImplementationNullIsOk<V, E, true>
|
|
|
|
: public ResultImplementationNullIsOkBase<V, E> {
|
|
|
|
public:
|
|
|
|
using ResultImplementationNullIsOkBase<V,
|
|
|
|
E>::ResultImplementationNullIsOkBase;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename V, typename E>
|
|
|
|
class ResultImplementationNullIsOk<V, E, false>
|
|
|
|
: public ResultImplementationNullIsOkBase<V, E> {
|
|
|
|
public:
|
|
|
|
using ResultImplementationNullIsOkBase<V,
|
|
|
|
E>::ResultImplementationNullIsOkBase;
|
|
|
|
|
|
|
|
ResultImplementationNullIsOk(ResultImplementationNullIsOk&&) = default;
|
|
|
|
ResultImplementationNullIsOk& operator=(ResultImplementationNullIsOk&&) =
|
|
|
|
default;
|
|
|
|
|
|
|
|
~ResultImplementationNullIsOk() {
|
|
|
|
if (this->isOk()) {
|
|
|
|
this->mValue.first().addr()->~V();
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 14:52:03 +03:00
|
|
|
};
|
|
|
|
|
2017-09-01 02:01:43 +03:00
|
|
|
/**
|
2020-09-02 20:55:55 +03:00
|
|
|
* Specialization for when the success type is default-constructible and the
|
|
|
|
* error type is a value type which can never have the value 0 (as determined by
|
|
|
|
* UnusedZero<>).
|
2017-09-01 02:01:43 +03:00
|
|
|
*/
|
|
|
|
template <typename V, typename E>
|
2020-09-03 17:14:41 +03:00
|
|
|
class ResultImplementation<V, E, PackingStrategy::NullIsOk>
|
|
|
|
: public ResultImplementationNullIsOk<V, E> {
|
2017-09-01 02:01:43 +03:00
|
|
|
public:
|
2020-09-03 17:14:41 +03:00
|
|
|
using ResultImplementationNullIsOk<V, E>::ResultImplementationNullIsOk;
|
2017-09-01 02:01:43 +03:00
|
|
|
};
|
|
|
|
|
2020-09-21 16:14:53 +03:00
|
|
|
template <size_t S>
|
|
|
|
using UnsignedIntType = std::conditional_t<
|
|
|
|
S == 1, std::uint8_t,
|
|
|
|
std::conditional_t<
|
|
|
|
S == 2, std::uint16_t,
|
|
|
|
std::conditional_t<S == 3 || S == 4, std::uint32_t,
|
|
|
|
std::conditional_t<S <= 8, std::uint64_t, void>>>>;
|
|
|
|
|
2016-11-21 14:52:03 +03:00
|
|
|
/**
|
2020-09-03 17:14:41 +03:00
|
|
|
* Specialization for when alignment permits using the least significant bit
|
|
|
|
* as a tag bit.
|
2016-11-21 14:52:03 +03:00
|
|
|
*/
|
2017-02-07 21:57:43 +03:00
|
|
|
template <typename V, typename E>
|
2020-09-21 16:14:53 +03:00
|
|
|
class ResultImplementation<V, E, PackingStrategy::LowBitTagIsError> {
|
|
|
|
static_assert(std::is_trivially_copyable_v<V> &&
|
|
|
|
std::is_trivially_destructible_v<V>);
|
|
|
|
static_assert(std::is_trivially_copyable_v<E> &&
|
|
|
|
std::is_trivially_destructible_v<E>);
|
|
|
|
|
|
|
|
static constexpr size_t kRequiredSize = std::max(sizeof(V), sizeof(E));
|
|
|
|
|
|
|
|
using StorageType = UnsignedIntType<kRequiredSize>;
|
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
alignas(std::max(alignof(V), alignof(E))) StorageType mBits;
|
|
|
|
#else
|
|
|
|
// Some gcc versions choke on using std::max with alignas, see
|
|
|
|
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94929 (and this seems to have
|
|
|
|
// regressed in some gcc 9.x version before being fixed again) Keeping the
|
|
|
|
// code above since we would eventually drop this when we no longer support
|
|
|
|
// gcc versions with the bug.
|
|
|
|
alignas(alignof(V) > alignof(E) ? alignof(V) : alignof(E)) StorageType mBits;
|
|
|
|
#endif
|
2016-11-21 14:52:03 +03:00
|
|
|
|
|
|
|
public:
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr ResultImplementation(V aValue) : mBits(0) {
|
2020-09-21 16:14:53 +03:00
|
|
|
if constexpr (!std::is_empty_v<V>) {
|
|
|
|
std::memcpy(&mBits, &aValue, sizeof(V));
|
|
|
|
MOZ_ASSERT((mBits & 1) == 0);
|
|
|
|
} else {
|
|
|
|
(void)aValue;
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr ResultImplementation(E aErrorValue) : mBits(1) {
|
2020-09-21 16:14:53 +03:00
|
|
|
if constexpr (!std::is_empty_v<E>) {
|
|
|
|
std::memcpy(&mBits, &aErrorValue, sizeof(E));
|
|
|
|
MOZ_ASSERT((mBits & 1) == 0);
|
|
|
|
mBits |= 1;
|
|
|
|
} else {
|
|
|
|
(void)aErrorValue;
|
|
|
|
}
|
2020-09-21 14:09:31 +03:00
|
|
|
}
|
2016-11-21 14:52:03 +03:00
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr bool isOk() const { return (mBits & 1) == 0; }
|
2016-11-21 14:52:03 +03:00
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr V inspect() const {
|
2020-09-21 16:14:53 +03:00
|
|
|
V res;
|
|
|
|
std::memcpy(&res, &mBits, sizeof(V));
|
|
|
|
return res;
|
|
|
|
}
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr V unwrap() { return inspect(); }
|
2019-08-13 11:26:18 +03:00
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr E inspectErr() const {
|
2020-09-21 16:14:53 +03:00
|
|
|
const auto bits = mBits ^ 1;
|
|
|
|
E res;
|
|
|
|
std::memcpy(&res, &bits, sizeof(E));
|
|
|
|
return res;
|
|
|
|
}
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr E unwrapErr() { return inspectErr(); }
|
2016-11-21 14:52:03 +03:00
|
|
|
};
|
|
|
|
|
2017-02-07 21:57:43 +03:00
|
|
|
// Return true if any of the struct can fit in a word.
|
|
|
|
template <typename V, typename E>
|
|
|
|
struct IsPackableVariant {
|
|
|
|
struct VEbool {
|
|
|
|
V v;
|
|
|
|
E e;
|
|
|
|
bool ok;
|
|
|
|
};
|
|
|
|
struct EVbool {
|
|
|
|
E e;
|
|
|
|
V v;
|
|
|
|
bool ok;
|
|
|
|
};
|
|
|
|
|
2020-03-28 16:57:18 +03:00
|
|
|
using Impl =
|
|
|
|
std::conditional_t<sizeof(VEbool) <= sizeof(EVbool), VEbool, EVbool>;
|
2017-02-07 21:57:43 +03:00
|
|
|
|
|
|
|
static const bool value = sizeof(Impl) <= sizeof(uintptr_t);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for when both type are not using all the bytes, in order to
|
|
|
|
* use one byte as a tag.
|
|
|
|
*/
|
|
|
|
template <typename V, typename E>
|
|
|
|
class ResultImplementation<V, E, PackingStrategy::PackedVariant> {
|
|
|
|
using Impl = typename IsPackableVariant<V, E>::Impl;
|
|
|
|
Impl data;
|
|
|
|
|
|
|
|
public:
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr ResultImplementation(V aValue) {
|
2019-08-13 11:26:18 +03:00
|
|
|
data.v = std::move(aValue);
|
2017-02-07 21:57:43 +03:00
|
|
|
data.ok = true;
|
|
|
|
}
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr ResultImplementation(E aErrorValue) {
|
2019-08-13 11:26:18 +03:00
|
|
|
data.e = std::move(aErrorValue);
|
2017-02-07 21:57:43 +03:00
|
|
|
data.ok = false;
|
|
|
|
}
|
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr bool isOk() const { return data.ok; }
|
2017-02-07 21:57:43 +03:00
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr const V& inspect() const { return data.v; }
|
|
|
|
constexpr V unwrap() { return std::move(data.v); }
|
2019-08-13 11:26:18 +03:00
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr const E& inspectErr() const { return data.e; }
|
|
|
|
constexpr E unwrapErr() { return std::move(data.e); }
|
2017-02-07 21:57:43 +03:00
|
|
|
};
|
|
|
|
|
2017-02-07 21:57:43 +03:00
|
|
|
// To use nullptr as a special value, we need the counter part to exclude zero
|
|
|
|
// from its range of valid representations.
|
|
|
|
//
|
|
|
|
// By default assume that zero can be represented.
|
|
|
|
template <typename T>
|
|
|
|
struct UnusedZero {
|
|
|
|
static const bool value = false;
|
|
|
|
};
|
|
|
|
|
2020-11-23 18:49:02 +03:00
|
|
|
// This template can be used as a helper for specializing UnusedZero for scoped
|
|
|
|
// enum types which never use 0 as an error value, e.g.
|
|
|
|
//
|
|
|
|
// namespace mozilla::detail {
|
|
|
|
//
|
|
|
|
// template <>
|
|
|
|
// struct UnusedZero<MyEnumType> : UnusedZeroEnum<MyEnumType> {};
|
|
|
|
//
|
|
|
|
// } // namespace mozilla::detail
|
|
|
|
//
|
|
|
|
template <typename T>
|
|
|
|
struct UnusedZeroEnum {
|
|
|
|
using StorageType = std::underlying_type_t<T>;
|
|
|
|
|
|
|
|
static constexpr bool value = true;
|
|
|
|
static constexpr StorageType nullValue = 0;
|
|
|
|
|
|
|
|
static constexpr T Inspect(const StorageType& aValue) {
|
|
|
|
return static_cast<T>(aValue);
|
|
|
|
}
|
|
|
|
static constexpr T Unwrap(StorageType aValue) {
|
|
|
|
return static_cast<T>(aValue);
|
|
|
|
}
|
|
|
|
static constexpr StorageType Store(T aValue) {
|
|
|
|
return static_cast<StorageType>(aValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-21 14:52:03 +03:00
|
|
|
// A bit of help figuring out which of the above specializations to use.
|
|
|
|
//
|
2020-09-21 16:14:53 +03:00
|
|
|
// We begin by safely assuming types don't have a spare bit, unless they are
|
|
|
|
// empty.
|
2016-11-21 14:52:03 +03:00
|
|
|
template <typename T>
|
|
|
|
struct HasFreeLSB {
|
2020-09-21 16:14:53 +03:00
|
|
|
static const bool value = std::is_empty_v<T>;
|
2016-11-21 14:52:03 +03:00
|
|
|
};
|
|
|
|
|
2018-11-20 02:56:54 +03:00
|
|
|
// As an incomplete type, void* does not have a spare bit.
|
|
|
|
template <>
|
|
|
|
struct HasFreeLSB<void*> {
|
|
|
|
static const bool value = false;
|
|
|
|
};
|
|
|
|
|
2016-11-21 14:52:03 +03:00
|
|
|
// The lowest bit of a properly-aligned pointer is always zero if the pointee
|
|
|
|
// type is greater than byte-aligned. That bit is free to use if it's masked
|
|
|
|
// out of such pointers before they're dereferenced.
|
|
|
|
template <typename T>
|
|
|
|
struct HasFreeLSB<T*> {
|
2018-03-23 19:38:14 +03:00
|
|
|
static const bool value = (alignof(T) & 1) == 0;
|
2016-11-21 14:52:03 +03:00
|
|
|
};
|
|
|
|
|
2017-02-07 21:57:43 +03:00
|
|
|
// Select one of the previous result implementation based on the properties of
|
|
|
|
// the V and E types.
|
|
|
|
template <typename V, typename E>
|
|
|
|
struct SelectResultImpl {
|
|
|
|
static const PackingStrategy value =
|
2020-09-02 20:55:55 +03:00
|
|
|
(HasFreeLSB<V>::value && HasFreeLSB<E>::value)
|
|
|
|
? PackingStrategy::LowBitTagIsError
|
2020-11-18 12:05:59 +03:00
|
|
|
: (UnusedZero<E>::value && sizeof(E) <= sizeof(uintptr_t))
|
|
|
|
? PackingStrategy::NullIsOk
|
|
|
|
: (std::is_default_constructible_v<V> &&
|
|
|
|
std::is_default_constructible_v<E> && IsPackableVariant<V, E>::value)
|
|
|
|
? PackingStrategy::PackedVariant
|
|
|
|
: PackingStrategy::Variant;
|
2017-02-07 21:57:43 +03:00
|
|
|
|
2020-09-02 20:55:55 +03:00
|
|
|
using Type = ResultImplementation<V, E, value>;
|
2017-02-07 21:57:43 +03:00
|
|
|
};
|
|
|
|
|
2016-12-22 00:05:56 +03:00
|
|
|
template <typename T>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct IsResult : std::false_type {};
|
2016-12-22 00:05:56 +03:00
|
|
|
|
|
|
|
template <typename V, typename E>
|
2020-03-28 16:57:20 +03:00
|
|
|
struct IsResult<Result<V, E>> : std::true_type {};
|
2016-12-22 00:05:56 +03:00
|
|
|
|
2016-11-21 14:52:03 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2017-08-30 07:28:31 +03:00
|
|
|
template <typename V, typename E>
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr auto ToResult(Result<V, E>&& aValue)
|
2018-06-01 19:30:30 +03:00
|
|
|
-> decltype(std::forward<Result<V, E>>(aValue)) {
|
|
|
|
return std::forward<Result<V, E>>(aValue);
|
2017-08-30 07:28:31 +03:00
|
|
|
}
|
|
|
|
|
2016-11-21 14:52:03 +03:00
|
|
|
/**
|
|
|
|
* Result<V, E> represents the outcome of an operation that can either succeed
|
|
|
|
* or fail. It contains either a success value of type V or an error value of
|
|
|
|
* type E.
|
|
|
|
*
|
|
|
|
* All Result methods are const, so results are basically immutable.
|
|
|
|
* This is just like Variant<V, E> but with a slightly different API, and the
|
|
|
|
* following cases are optimized so Result can be stored more efficiently:
|
|
|
|
*
|
2020-09-21 16:14:28 +03:00
|
|
|
* - If both the success and error types do not use their least significant bit,
|
|
|
|
* are trivially copyable and destructible, Result<V, E> is guaranteed to be as
|
|
|
|
* large as the larger type. This is determined via the HasFreeLSB trait. By
|
|
|
|
* default, empty classes (in particular Ok) and aligned pointer types are
|
|
|
|
* assumed to have a free LSB, but you can specialize this trait for other
|
|
|
|
* types. If the success type is empty, the representation is guaranteed to be
|
|
|
|
* all zero bits on success. Do not change this representation! There is JIT
|
|
|
|
* code that depends on it. (Implementation note: The lowest bit is used as a
|
|
|
|
* tag bit: 0 to indicate the Result's bits are a success value, 1 to indicate
|
|
|
|
* the Result's bits (with the 1 masked out) encode an error value)
|
2016-11-21 14:52:03 +03:00
|
|
|
*
|
2020-09-21 16:14:28 +03:00
|
|
|
* - Else, if the error type can't have a all-zero bits representation and is
|
|
|
|
* not larger than a pointer, a CompactPair is used to represent this rather
|
|
|
|
* than a Variant. This has shown to be better optimizable, and the template
|
|
|
|
* code is much simpler than that of Variant, so it should also compile faster.
|
|
|
|
* Whether an error type can't be all-zero bits, is determined via the
|
|
|
|
* UnusedZero trait. MFBT doesn't declare any public type UnusedZero, but
|
|
|
|
* nsresult is declared UnusedZero in XPCOM.
|
2016-11-21 14:52:03 +03:00
|
|
|
*
|
|
|
|
* The purpose of Result is to reduce the screwups caused by using `false` or
|
|
|
|
* `nullptr` to indicate errors.
|
|
|
|
* What screwups? See <https://bugzilla.mozilla.org/show_bug.cgi?id=912928> for
|
|
|
|
* a partial list.
|
2020-08-28 17:16:35 +03:00
|
|
|
*
|
|
|
|
* Result<const V, E> or Result<V, const E> are not meaningful. The success or
|
|
|
|
* error values in a Result instance are non-modifiable in-place anyway. This
|
|
|
|
* guarantee must also be maintained when evolving Result. They can be
|
|
|
|
* unwrap()ped, but this loses const qualification. However, Result<const V, E>
|
|
|
|
* or Result<V, const E> may be misleading and prevent movability. Just use
|
|
|
|
* Result<V, E>. (Result<const V*, E> may make sense though, just Result<const
|
|
|
|
* V* const, E> is not possible.)
|
2016-11-21 14:52:03 +03:00
|
|
|
*/
|
|
|
|
template <typename V, typename E>
|
|
|
|
class MOZ_MUST_USE_TYPE Result final {
|
2020-08-28 17:16:35 +03:00
|
|
|
// See class comment on Result<const V, E> and Result<V, const E>.
|
|
|
|
static_assert(!std::is_const_v<V>);
|
|
|
|
static_assert(!std::is_const_v<E>);
|
2020-09-21 16:14:28 +03:00
|
|
|
static_assert(!std::is_reference_v<V>);
|
|
|
|
static_assert(!std::is_reference_v<E>);
|
2020-08-28 17:16:35 +03:00
|
|
|
|
2017-02-07 21:57:43 +03:00
|
|
|
using Impl = typename detail::SelectResultImpl<V, E>::Type;
|
|
|
|
|
2016-11-21 14:52:03 +03:00
|
|
|
Impl mImpl;
|
|
|
|
|
|
|
|
public:
|
2020-04-01 18:53:58 +03:00
|
|
|
using ok_type = V;
|
|
|
|
using err_type = E;
|
|
|
|
|
2019-08-13 11:26:18 +03:00
|
|
|
/** Create a success result. */
|
2021-03-23 15:35:19 +03:00
|
|
|
MOZ_IMPLICIT constexpr Result(V&& aValue) : mImpl(std::forward<V>(aValue)) {
|
2019-08-13 11:26:18 +03:00
|
|
|
MOZ_ASSERT(isOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a success result. */
|
2021-03-23 15:35:19 +03:00
|
|
|
MOZ_IMPLICIT constexpr Result(const V& aValue) : mImpl(aValue) {
|
|
|
|
MOZ_ASSERT(isOk());
|
|
|
|
}
|
2020-09-21 16:15:13 +03:00
|
|
|
|
|
|
|
/** Create a success result in-place. */
|
|
|
|
template <typename... Args>
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr Result(std::in_place_t, Args&&... aArgs)
|
2020-09-21 16:15:13 +03:00
|
|
|
: mImpl(std::in_place, std::forward<Args>(aArgs)...) {
|
|
|
|
MOZ_ASSERT(isOk());
|
|
|
|
}
|
2016-11-21 14:52:03 +03:00
|
|
|
|
2019-08-13 11:26:18 +03:00
|
|
|
/** Create an error result. */
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr Result(E aErrorValue) : mImpl(std::move(aErrorValue)) {
|
2019-08-13 11:26:18 +03:00
|
|
|
MOZ_ASSERT(isErr());
|
|
|
|
}
|
2016-11-21 14:52:03 +03:00
|
|
|
|
2020-09-22 14:24:08 +03:00
|
|
|
/**
|
|
|
|
* Create a (success/error) result from another (success/error) result with a
|
|
|
|
* different but convertible error type. */
|
|
|
|
template <typename E2,
|
|
|
|
typename = std::enable_if_t<std::is_convertible_v<E2, E>>>
|
2021-03-23 15:35:19 +03:00
|
|
|
MOZ_IMPLICIT constexpr Result(Result<V, E2>&& aOther)
|
2020-09-22 14:24:08 +03:00
|
|
|
: mImpl(aOther.isOk() ? Impl{aOther.unwrap()}
|
|
|
|
: Impl{aOther.unwrapErr()}) {}
|
|
|
|
|
2019-09-20 04:18:40 +03:00
|
|
|
/**
|
|
|
|
* Implementation detail of MOZ_TRY().
|
|
|
|
* Create an error result from another error result.
|
|
|
|
*/
|
|
|
|
template <typename E2>
|
2021-03-23 15:35:19 +03:00
|
|
|
MOZ_IMPLICIT constexpr Result(GenericErrorResult<E2>&& aErrorResult)
|
2020-09-21 16:14:28 +03:00
|
|
|
: mImpl(std::move(aErrorResult.mErrorValue)) {
|
2020-03-28 16:57:17 +03:00
|
|
|
static_assert(std::is_convertible_v<E2, E>, "E2 must be convertible to E");
|
2019-09-20 04:18:40 +03:00
|
|
|
MOZ_ASSERT(isErr());
|
|
|
|
}
|
|
|
|
|
2016-11-21 14:52:03 +03:00
|
|
|
/**
|
|
|
|
* Implementation detail of MOZ_TRY().
|
|
|
|
* Create an error result from another error result.
|
|
|
|
*/
|
|
|
|
template <typename E2>
|
2021-03-23 15:35:19 +03:00
|
|
|
MOZ_IMPLICIT constexpr Result(const GenericErrorResult<E2>& aErrorResult)
|
2016-11-21 14:52:03 +03:00
|
|
|
: mImpl(aErrorResult.mErrorValue) {
|
2020-03-28 16:57:17 +03:00
|
|
|
static_assert(std::is_convertible_v<E2, E>, "E2 must be convertible to E");
|
2016-11-21 14:52:03 +03:00
|
|
|
MOZ_ASSERT(isErr());
|
|
|
|
}
|
|
|
|
|
2020-09-02 20:56:23 +03:00
|
|
|
Result(const Result&) = delete;
|
2019-08-13 11:26:18 +03:00
|
|
|
Result(Result&&) = default;
|
2020-09-02 20:56:23 +03:00
|
|
|
Result& operator=(const Result&) = delete;
|
2019-08-13 11:26:18 +03:00
|
|
|
Result& operator=(Result&&) = default;
|
2016-11-21 14:52:03 +03:00
|
|
|
|
|
|
|
/** True if this Result is a success result. */
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr bool isOk() const { return mImpl.isOk(); }
|
2016-11-21 14:52:03 +03:00
|
|
|
|
|
|
|
/** True if this Result is an error result. */
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr bool isErr() const { return !mImpl.isOk(); }
|
2016-11-21 14:52:03 +03:00
|
|
|
|
2019-08-13 11:26:18 +03:00
|
|
|
/** Take the success value from this Result, which must be a success result.
|
|
|
|
*/
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr V unwrap() {
|
2016-11-21 14:52:03 +03:00
|
|
|
MOZ_ASSERT(isOk());
|
|
|
|
return mImpl.unwrap();
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:28 +03:00
|
|
|
/**
|
2019-08-13 11:26:18 +03:00
|
|
|
* Take the success value from this Result, which must be a success result.
|
|
|
|
* If it is an error result, then return the aValue.
|
2017-11-22 12:14:28 +03:00
|
|
|
*/
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr V unwrapOr(V aValue) {
|
2019-10-11 02:46:42 +03:00
|
|
|
return MOZ_LIKELY(isOk()) ? mImpl.unwrap() : std::move(aValue);
|
|
|
|
}
|
2017-11-22 12:14:28 +03:00
|
|
|
|
2019-08-13 11:26:18 +03:00
|
|
|
/** Take the error value from this Result, which must be an error result. */
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr E unwrapErr() {
|
2016-11-21 14:52:03 +03:00
|
|
|
MOZ_ASSERT(isErr());
|
|
|
|
return mImpl.unwrapErr();
|
|
|
|
}
|
2016-12-22 00:05:56 +03:00
|
|
|
|
2019-08-13 11:26:18 +03:00
|
|
|
/** See the success value from this Result, which must be a success result. */
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr decltype(auto) inspect() const {
|
2020-09-21 16:14:53 +03:00
|
|
|
static_assert(!std::is_reference_v<
|
|
|
|
std::invoke_result_t<decltype(&Impl::inspect), Impl>> ||
|
|
|
|
std::is_const_v<std::remove_reference_t<
|
|
|
|
std::invoke_result_t<decltype(&Impl::inspect), Impl>>>);
|
|
|
|
MOZ_ASSERT(isOk());
|
|
|
|
return mImpl.inspect();
|
|
|
|
}
|
2019-08-13 11:26:18 +03:00
|
|
|
|
|
|
|
/** See the error value from this Result, which must be an error result. */
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr decltype(auto) inspectErr() const {
|
2020-09-21 16:14:53 +03:00
|
|
|
static_assert(
|
|
|
|
!std::is_reference_v<
|
|
|
|
std::invoke_result_t<decltype(&Impl::inspectErr), Impl>> ||
|
|
|
|
std::is_const_v<std::remove_reference_t<
|
|
|
|
std::invoke_result_t<decltype(&Impl::inspectErr), Impl>>>);
|
2019-08-13 11:26:18 +03:00
|
|
|
MOZ_ASSERT(isErr());
|
|
|
|
return mImpl.inspectErr();
|
|
|
|
}
|
|
|
|
|
2020-05-13 18:44:31 +03:00
|
|
|
/** Propagate the error value from this Result, which must be an error result.
|
|
|
|
*
|
|
|
|
* This can be used to propagate an error from a function call to the caller
|
|
|
|
* with a different value type, but the same error type:
|
|
|
|
*
|
|
|
|
* Result<T1, E> Func1() {
|
|
|
|
* Result<T2, E> res = Func2();
|
|
|
|
* if (res.isErr()) { return res.propagateErr(); }
|
|
|
|
* }
|
|
|
|
*/
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr GenericErrorResult<E> propagateErr() {
|
2020-05-13 18:44:31 +03:00
|
|
|
MOZ_ASSERT(isErr());
|
|
|
|
return GenericErrorResult<E>{mImpl.unwrapErr()};
|
|
|
|
}
|
|
|
|
|
2016-12-22 00:05:56 +03:00
|
|
|
/**
|
2020-08-20 14:04:12 +03:00
|
|
|
* Map a function V -> V2 over this result's success variant. If this result
|
|
|
|
* is an error, do not invoke the function and propagate the error.
|
2016-12-22 00:05:56 +03:00
|
|
|
*
|
|
|
|
* Mapping over success values invokes the function to produce a new success
|
|
|
|
* value:
|
|
|
|
*
|
|
|
|
* // Map Result<int, E> to another Result<int, E>
|
|
|
|
* Result<int, E> res(5);
|
|
|
|
* Result<int, E> res2 = res.map([](int x) { return x * x; });
|
2020-08-20 14:04:12 +03:00
|
|
|
* MOZ_ASSERT(res.isOk());
|
2016-12-22 00:05:56 +03:00
|
|
|
* MOZ_ASSERT(res2.unwrap() == 25);
|
|
|
|
*
|
|
|
|
* // Map Result<const char*, E> to Result<size_t, E>
|
|
|
|
* Result<const char*, E> res("hello, map!");
|
|
|
|
* Result<size_t, E> res2 = res.map(strlen);
|
2020-08-20 14:04:12 +03:00
|
|
|
* MOZ_ASSERT(res.isOk());
|
2016-12-22 00:05:56 +03:00
|
|
|
* MOZ_ASSERT(res2.unwrap() == 11);
|
|
|
|
*
|
2020-05-13 18:44:31 +03:00
|
|
|
* Mapping over an error does not invoke the function and propagates the
|
|
|
|
* error:
|
2016-12-22 00:05:56 +03:00
|
|
|
*
|
|
|
|
* Result<V, int> res(5);
|
|
|
|
* MOZ_ASSERT(res.isErr());
|
2020-08-20 14:04:12 +03:00
|
|
|
* Result<V2, int> res2 = res.map([](V v) { ... });
|
2016-12-22 00:05:56 +03:00
|
|
|
* MOZ_ASSERT(res2.isErr());
|
|
|
|
* MOZ_ASSERT(res2.unwrapErr() == 5);
|
|
|
|
*/
|
|
|
|
template <typename F>
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr auto map(F f) -> Result<std::result_of_t<F(V)>, E> {
|
2020-09-08 17:39:41 +03:00
|
|
|
using RetResult = Result<std::result_of_t<F(V)>, E>;
|
2019-10-11 02:46:42 +03:00
|
|
|
return MOZ_LIKELY(isOk()) ? RetResult(f(unwrap())) : RetResult(unwrapErr());
|
2016-12-22 00:05:56 +03:00
|
|
|
}
|
2016-12-22 00:05:56 +03:00
|
|
|
|
2019-10-04 23:44:50 +03:00
|
|
|
/**
|
2020-08-20 14:04:12 +03:00
|
|
|
* Map a function E -> E2 over this result's error variant. If this result is
|
2019-10-04 23:44:50 +03:00
|
|
|
* a success, do not invoke the function and move the success over.
|
|
|
|
*
|
|
|
|
* Mapping over error values invokes the function to produce a new error
|
|
|
|
* value:
|
|
|
|
*
|
|
|
|
* // Map Result<V, int> to another Result<V, int>
|
|
|
|
* Result<V, int> res(5);
|
|
|
|
* Result<V, int> res2 = res.mapErr([](int x) { return x * x; });
|
2020-08-20 14:04:12 +03:00
|
|
|
* MOZ_ASSERT(res2.isErr());
|
2019-10-04 23:44:50 +03:00
|
|
|
* MOZ_ASSERT(res2.unwrapErr() == 25);
|
|
|
|
*
|
|
|
|
* // Map Result<V, const char*> to Result<V, size_t>
|
2020-08-20 14:04:12 +03:00
|
|
|
* Result<V, const char*> res("hello, mapErr!");
|
|
|
|
* Result<V, size_t> res2 = res.mapErr(strlen);
|
|
|
|
* MOZ_ASSERT(res2.isErr());
|
|
|
|
* MOZ_ASSERT(res2.unwrapErr() == 14);
|
2019-10-04 23:44:50 +03:00
|
|
|
*
|
2020-08-20 14:04:12 +03:00
|
|
|
* Mapping over a success does not invoke the function and moves the success:
|
2019-10-04 23:44:50 +03:00
|
|
|
*
|
2020-08-20 14:04:12 +03:00
|
|
|
* Result<int, E> res(5);
|
2019-10-04 23:44:50 +03:00
|
|
|
* MOZ_ASSERT(res.isOk());
|
2020-08-20 14:04:12 +03:00
|
|
|
* Result<int, E2> res2 = res.mapErr([](E e) { ... });
|
2019-10-04 23:44:50 +03:00
|
|
|
* MOZ_ASSERT(res2.isOk());
|
|
|
|
* MOZ_ASSERT(res2.unwrap() == 5);
|
|
|
|
*/
|
|
|
|
template <typename F>
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr auto mapErr(F f) {
|
2019-10-04 23:44:50 +03:00
|
|
|
using RetResult = Result<V, std::result_of_t<F(E)>>;
|
2020-08-20 14:04:12 +03:00
|
|
|
return MOZ_UNLIKELY(isErr()) ? RetResult(f(unwrapErr()))
|
|
|
|
: RetResult(unwrap());
|
2019-10-04 23:44:50 +03:00
|
|
|
}
|
|
|
|
|
2020-08-24 17:44:50 +03:00
|
|
|
/**
|
|
|
|
* Map a function E -> Result<V, E2> over this result's error variant. If
|
|
|
|
* this result is a success, do not invoke the function and move the success
|
|
|
|
* over.
|
|
|
|
*
|
|
|
|
* `orElse`ing over error values invokes the function to produce a new
|
|
|
|
* result:
|
|
|
|
*
|
|
|
|
* // `orElse` Result<V, int> error variant to another Result<V, int>
|
|
|
|
* // error variant or Result<V, int> success variant
|
|
|
|
* auto orElse = [](int x) -> Result<V, int> {
|
|
|
|
* if (x != 6) {
|
|
|
|
* return Err(x * x);
|
|
|
|
* }
|
|
|
|
* return V(...);
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* Result<V, int> res(5);
|
|
|
|
* auto res2 = res.orElse(orElse);
|
|
|
|
* MOZ_ASSERT(res2.isErr());
|
|
|
|
* MOZ_ASSERT(res2.unwrapErr() == 25);
|
|
|
|
*
|
|
|
|
* Result<V, int> res3(6);
|
|
|
|
* auto res4 = res3.orElse(orElse);
|
|
|
|
* MOZ_ASSERT(res4.isOk());
|
|
|
|
* MOZ_ASSERT(res4.unwrap() == ...);
|
|
|
|
*
|
|
|
|
* // `orElse` Result<V, const char*> error variant to Result<V, size_t>
|
|
|
|
* // error variant or Result<V, size_t> success variant
|
|
|
|
* auto orElse = [](const char* s) -> Result<V, size_t> {
|
|
|
|
* if (strcmp(s, "foo")) {
|
|
|
|
* return Err(strlen(s));
|
|
|
|
* }
|
|
|
|
* return V(...);
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* Result<V, const char*> res("hello, orElse!");
|
|
|
|
* auto res2 = res.orElse(orElse);
|
|
|
|
* MOZ_ASSERT(res2.isErr());
|
|
|
|
* MOZ_ASSERT(res2.unwrapErr() == 14);
|
|
|
|
*
|
|
|
|
* Result<V, const char*> res3("foo");
|
|
|
|
* auto res4 = ress.orElse(orElse);
|
|
|
|
* MOZ_ASSERT(res4.isOk());
|
|
|
|
* MOZ_ASSERT(res4.unwrap() == ...);
|
|
|
|
*
|
|
|
|
* `orElse`ing over a success does not invoke the function and moves the
|
|
|
|
* success:
|
|
|
|
*
|
|
|
|
* Result<int, E> res(5);
|
|
|
|
* MOZ_ASSERT(res.isOk());
|
|
|
|
* Result<int, E2> res2 = res.orElse([](E e) { ... });
|
|
|
|
* MOZ_ASSERT(res2.isOk());
|
|
|
|
* MOZ_ASSERT(res2.unwrap() == 5);
|
|
|
|
*/
|
|
|
|
template <typename F>
|
|
|
|
auto orElse(F f) -> Result<V, typename std::result_of_t<F(E)>::err_type> {
|
|
|
|
return MOZ_UNLIKELY(isErr()) ? f(unwrapErr()) : unwrap();
|
|
|
|
}
|
|
|
|
|
2016-12-22 00:05:56 +03:00
|
|
|
/**
|
2020-08-20 14:04:12 +03:00
|
|
|
* Given a function V -> Result<V2, E>, apply it to this result's success
|
|
|
|
* value and return its result. If this result is an error value, it is
|
|
|
|
* propagated.
|
2016-12-22 00:05:56 +03:00
|
|
|
*
|
|
|
|
* This is sometimes called "flatMap" or ">>=" in other contexts.
|
|
|
|
*
|
|
|
|
* `andThen`ing over success values invokes the function to produce a new
|
|
|
|
* result:
|
|
|
|
*
|
|
|
|
* Result<const char*, Error> res("hello, andThen!");
|
|
|
|
* Result<HtmlFreeString, Error> res2 = res.andThen([](const char* s) {
|
|
|
|
* return containsHtmlTag(s)
|
|
|
|
* ? Result<HtmlFreeString, Error>(Error("Invalid: contains HTML"))
|
|
|
|
* : Result<HtmlFreeString, Error>(HtmlFreeString(s));
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* MOZ_ASSERT(res2.isOk());
|
|
|
|
* MOZ_ASSERT(res2.unwrap() == HtmlFreeString("hello, andThen!");
|
|
|
|
*
|
|
|
|
* `andThen`ing over error results does not invoke the function, and just
|
2020-05-13 18:44:31 +03:00
|
|
|
* propagates the error result:
|
2016-12-22 00:05:56 +03:00
|
|
|
*
|
|
|
|
* Result<int, const char*> res("some error");
|
|
|
|
* auto res2 = res.andThen([](int x) { ... });
|
|
|
|
* MOZ_ASSERT(res2.isErr());
|
|
|
|
* MOZ_ASSERT(res.unwrapErr() == res2.unwrapErr());
|
|
|
|
*/
|
2020-03-28 16:35:31 +03:00
|
|
|
template <typename F, typename = std::enable_if_t<detail::IsResult<
|
2020-10-06 12:03:14 +03:00
|
|
|
std::invoke_result_t<F, V&&>>::value>>
|
2021-03-23 15:35:19 +03:00
|
|
|
constexpr auto andThen(F f) -> std::invoke_result_t<F, V&&> {
|
2020-05-13 18:44:31 +03:00
|
|
|
return MOZ_LIKELY(isOk()) ? f(unwrap()) : propagateErr();
|
2016-12-22 00:05:56 +03:00
|
|
|
}
|
2016-11-21 14:52:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A type that auto-converts to an error Result. This is like a Result without
|
|
|
|
* a success type. It's the best return type for functions that always return
|
|
|
|
* an error--functions designed to build and populate error objects. It's also
|
|
|
|
* useful in error-handling macros; see MOZ_TRY for an example.
|
|
|
|
*/
|
|
|
|
template <typename E>
|
|
|
|
class MOZ_MUST_USE_TYPE GenericErrorResult {
|
|
|
|
E mErrorValue;
|
|
|
|
|
|
|
|
template <typename V, typename E2>
|
|
|
|
friend class Result;
|
|
|
|
|
|
|
|
public:
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr GenericErrorResult(const E& aErrorValue)
|
2020-09-21 16:12:48 +03:00
|
|
|
: mErrorValue(aErrorValue) {}
|
|
|
|
|
2021-03-23 15:35:19 +03:00
|
|
|
explicit constexpr GenericErrorResult(E&& aErrorValue)
|
2020-09-21 16:12:48 +03:00
|
|
|
: mErrorValue(std::move(aErrorValue)) {}
|
|
|
|
};
|
|
|
|
|
2016-11-21 14:52:03 +03:00
|
|
|
template <typename E>
|
2021-03-23 15:35:19 +03:00
|
|
|
inline constexpr auto Err(E&& aErrorValue) {
|
2020-09-21 16:12:48 +03:00
|
|
|
return GenericErrorResult<std::decay_t<E>>(std::forward<E>(aErrorValue));
|
2016-11-21 14:52:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MOZ_TRY(expr) is the C++ equivalent of Rust's `try!(expr);`. First, it
|
|
|
|
* evaluates expr, which must produce a Result value. On success, it
|
|
|
|
* discards the result altogether. On error, it immediately returns an error
|
|
|
|
* Result from the enclosing function.
|
|
|
|
*/
|
2020-05-13 18:44:31 +03:00
|
|
|
#define MOZ_TRY(expr) \
|
|
|
|
do { \
|
|
|
|
auto mozTryTempResult_ = ::mozilla::ToResult(expr); \
|
|
|
|
if (MOZ_UNLIKELY(mozTryTempResult_.isErr())) { \
|
|
|
|
return mozTryTempResult_.propagateErr(); \
|
|
|
|
} \
|
2016-11-21 14:52:03 +03:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MOZ_TRY_VAR(target, expr) is the C++ equivalent of Rust's `target =
|
|
|
|
* try!(expr);`. First, it evaluates expr, which must produce a Result value. On
|
|
|
|
* success, the result's success value is assigned to target. On error,
|
2020-08-04 11:50:26 +03:00
|
|
|
* immediately returns the error result. |target| must be an lvalue.
|
2016-11-21 14:52:03 +03:00
|
|
|
*/
|
2020-05-13 18:44:31 +03:00
|
|
|
#define MOZ_TRY_VAR(target, expr) \
|
|
|
|
do { \
|
|
|
|
auto mozTryVarTempResult_ = (expr); \
|
|
|
|
if (MOZ_UNLIKELY(mozTryVarTempResult_.isErr())) { \
|
|
|
|
return mozTryVarTempResult_.propagateErr(); \
|
|
|
|
} \
|
|
|
|
(target) = mozTryVarTempResult_.unwrap(); \
|
2016-11-21 14:52:03 +03:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#endif // mozilla_Result_h
|