diff --git a/mfbt/Maybe.h b/mfbt/Maybe.h index 4ef56526c603..ce3ee1e1b7a1 100644 --- a/mfbt/Maybe.h +++ b/mfbt/Maybe.h @@ -315,7 +315,11 @@ public: bool isNothing() const { return !mIsSome; } /* Returns the contents of this Maybe by value. Unsafe unless |isSome()|. */ - T value() const; + T value() const + { + MOZ_ASSERT(mIsSome); + return ref(); + } /* * Returns the contents of this Maybe by value. If |isNothing()|, returns @@ -344,8 +348,17 @@ public: } /* Returns the contents of this Maybe by pointer. Unsafe unless |isSome()|. */ - T* ptr(); - const T* ptr() const; + T* ptr() + { + MOZ_ASSERT(mIsSome); + return &ref(); + } + + const T* ptr() const + { + MOZ_ASSERT(mIsSome); + return &ref(); + } /* * Returns the contents of this Maybe by pointer. If |isNothing()|, @@ -389,12 +402,30 @@ public: return aFunc(); } - T* operator->(); - const T* operator->() const; + T* operator->() + { + MOZ_ASSERT(mIsSome); + return ptr(); + } + + const T* operator->() const + { + MOZ_ASSERT(mIsSome); + return ptr(); + } /* Returns the contents of this Maybe by ref. Unsafe unless |isSome()|. */ - T& ref(); - const T& ref() const; + T& ref() + { + MOZ_ASSERT(mIsSome); + return *static_cast(data()); + } + + const T& ref() const + { + MOZ_ASSERT(mIsSome); + return *static_cast(data()); + } /* * Returns the contents of this Maybe by ref. If |isNothing()|, returns @@ -438,8 +469,17 @@ public: return aFunc(); } - T& operator*(); - const T& operator*() const; + T& operator*() + { + MOZ_ASSERT(mIsSome); + return ref(); + } + + const T& operator*() const + { + MOZ_ASSERT(mIsSome); + return ref(); + } /* If |isSome()|, runs the provided function or functor on the contents of * this Maybe. */ @@ -504,7 +544,12 @@ public: * arguments to |emplace()| are the parameters to T's constructor. */ template - void emplace(Args&&... aArgs); + void emplace(Args&&... aArgs) + { + MOZ_ASSERT(!mIsSome); + ::new (KnownNotNull, data()) T(Forward(aArgs)...); + mIsSome = true; + } friend std::ostream& operator<<(std::ostream& aStream, const Maybe& aMaybe) @@ -518,88 +563,6 @@ public: } }; -template -T -Maybe::value() const -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return ref(); -} - -template -T* -Maybe::ptr() -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return &ref(); -} - -template -const T* -Maybe::ptr() const -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return &ref(); -} - -template -T* -Maybe::operator->() -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return ptr(); -} - -template -const T* -Maybe::operator->() const -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return ptr(); -} - -template -T& -Maybe::ref() -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return *static_cast(data()); -} - -template -const T& -Maybe::ref() const -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return *static_cast(data()); -} - -template -T& -Maybe::operator*() -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return ref(); -} - -template -const T& -Maybe::operator*() const -{ - MOZ_DIAGNOSTIC_ASSERT(mIsSome); - return ref(); -} - -template -template -void -Maybe::emplace(Args&&... aArgs) -{ - MOZ_DIAGNOSTIC_ASSERT(!mIsSome); - ::new (KnownNotNull, data()) T(Forward(aArgs)...); - mIsSome = true; -} - /* * Some() creates a Maybe value containing the provided T value. If T has a * move constructor, it's used to make this as efficient as possible.