mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-28 21:28:55 +00:00
Bug 1645328 - Add in-place constructor to Maybe. r=froydnj
Differential Revision: https://phabricator.services.mozilla.com/D79448
This commit is contained in:
parent
d321aa4f0b
commit
af931b95e6
20
mfbt/Maybe.h
20
mfbt/Maybe.h
@ -265,6 +265,11 @@ struct MaybeStorage<T, false> {
|
||||
explicit MaybeStorage(const T& aVal) : mStorage{aVal}, mIsSome{true} {}
|
||||
explicit MaybeStorage(T&& aVal) : mStorage{std::move(aVal)}, mIsSome{true} {}
|
||||
|
||||
template <typename... Args>
|
||||
explicit MaybeStorage(Args&&... aArgs) : mIsSome{true} {
|
||||
::new (KnownNotNull, &mStorage.val) T(std::forward<Args>(aArgs)...);
|
||||
}
|
||||
|
||||
// Copy and move operations are no-ops, since copying is moving is implemented
|
||||
// by Maybe_CopyMove_Enabler.
|
||||
|
||||
@ -299,6 +304,14 @@ struct MaybeStorage<T, true> {
|
||||
: mStorage{aVal}, mIsSome{true} {}
|
||||
constexpr explicit MaybeStorage(T&& aVal)
|
||||
: mStorage{std::move(aVal)}, mIsSome{true} {}
|
||||
|
||||
// XXX This should be constexpr, but then we can't use placement new.
|
||||
// Delegating this to a Union constructor with the same signature doesn't
|
||||
// build out of the box.
|
||||
template <typename... Args>
|
||||
explicit MaybeStorage(Args&&... aArgs) : mIsSome{true} {
|
||||
::new (KnownNotNull, &mStorage.val) T(std::forward<Args>(aArgs)...);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
@ -350,9 +363,6 @@ constexpr Maybe<U> Some(T&& aValue);
|
||||
* Boost. The most important differences between Maybe and std::optional are:
|
||||
*
|
||||
* - std::optional<T> may be compared with T. We deliberately forbid that.
|
||||
* - std::optional allows in-place construction without a separate call to
|
||||
* |emplace()| by using a dummy |in_place_t| value to tag the appropriate
|
||||
* constructor.
|
||||
* - std::optional has |valueOr()|, equivalent to Maybe's |valueOr()|, but
|
||||
* lacks corresponding methods for |refOr()| and |ptrOr()|.
|
||||
* - std::optional lacks |map()| and |apply()|, making it less suitable for
|
||||
@ -389,6 +399,10 @@ class MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS Maybe
|
||||
|
||||
MOZ_ALLOW_TEMPORARY MOZ_IMPLICIT constexpr Maybe(Nothing) : Maybe{} {}
|
||||
|
||||
template <typename... Args>
|
||||
constexpr explicit Maybe(std::in_place_t, Args&&... aArgs)
|
||||
: detail::MaybeStorage<T>(std::forward<Args>(aArgs)...) {}
|
||||
|
||||
/**
|
||||
* Maybe<T> can be copy-constructed from a Maybe<U> if T is constructible from
|
||||
* a const U&.
|
||||
|
@ -156,7 +156,7 @@ struct UncopyableUnmovableValue {
|
||||
|
||||
~UncopyableUnmovableValue() { --sUndestroyedObjects; }
|
||||
|
||||
Status GetStatus() { return mStatus; }
|
||||
Status GetStatus() const { return mStatus; }
|
||||
|
||||
private:
|
||||
UncopyableUnmovableValue(const UncopyableUnmovableValue& aOther) = delete;
|
||||
@ -261,6 +261,14 @@ static bool TestBasicFeatures() {
|
||||
mayValue.reset();
|
||||
MOZ_RELEASE_ASSERT(!mayValue);
|
||||
|
||||
{
|
||||
// Check that Maybe(std::in_place, T1) calls the correct constructor.
|
||||
const auto mayValueConstructed = Maybe<BasicValue>(std::in_place, 1);
|
||||
MOZ_RELEASE_ASSERT(mayValueConstructed);
|
||||
MOZ_RELEASE_ASSERT(mayValueConstructed->GetStatus() == eWasConstructed);
|
||||
MOZ_RELEASE_ASSERT(mayValueConstructed->GetTag() == 1);
|
||||
}
|
||||
|
||||
// Check that Some() and Nothing() work.
|
||||
mayValue = Some(BasicValue(2));
|
||||
MOZ_RELEASE_ASSERT(mayValue);
|
||||
@ -497,6 +505,13 @@ static bool TestCopyAndMove() {
|
||||
MOZ_RELEASE_ASSERT(0 == sUndestroyedObjects);
|
||||
|
||||
{ // Check that types that support neither moves or copies work.
|
||||
{
|
||||
const auto mayUncopyableUnmovableValueConstructed =
|
||||
Maybe<UncopyableUnmovableValue>{std::in_place};
|
||||
MOZ_RELEASE_ASSERT(mayUncopyableUnmovableValueConstructed->GetStatus() ==
|
||||
eWasDefaultConstructed);
|
||||
}
|
||||
|
||||
Maybe<UncopyableUnmovableValue> mayUncopyableUnmovableValue;
|
||||
mayUncopyableUnmovableValue.emplace();
|
||||
MOZ_RELEASE_ASSERT(mayUncopyableUnmovableValue->GetStatus() ==
|
||||
|
Loading…
x
Reference in New Issue
Block a user