зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1680724 - Add a utility function that converts a T* to a Maybe<T&>. r=jwalden
Differential Revision: https://phabricator.services.mozilla.com/D98783
This commit is contained in:
Родитель
9c3afcc70e
Коммит
8c5c02b43d
|
@ -880,6 +880,11 @@ constexpr Maybe<T&> SomeRef(T& aValue) {
|
|||
return value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr Maybe<T&> ToMaybeRef(T* const aPtr) {
|
||||
return aPtr ? SomeRef(*aPtr) : Nothing{};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Maybe<std::remove_cv_t<std::remove_reference_t<T>>> ToMaybe(T* aPtr) {
|
||||
if (aPtr) {
|
||||
|
|
|
@ -19,6 +19,7 @@ using mozilla::Nothing;
|
|||
using mozilla::Some;
|
||||
using mozilla::SomeRef;
|
||||
using mozilla::ToMaybe;
|
||||
using mozilla::ToMaybeRef;
|
||||
|
||||
#define RUN_TEST(t) \
|
||||
do { \
|
||||
|
@ -1420,6 +1421,25 @@ static bool TestReference() {
|
|||
MOZ_RELEASE_ASSERT(!mapped);
|
||||
}
|
||||
|
||||
{
|
||||
int foo = 42;
|
||||
auto someRef = ToMaybeRef(&foo);
|
||||
|
||||
static_assert(std::is_same_v<decltype(someRef), Maybe<int&>>);
|
||||
|
||||
MOZ_RELEASE_ASSERT(someRef.isSome());
|
||||
MOZ_RELEASE_ASSERT(&foo == &someRef.ref());
|
||||
}
|
||||
|
||||
{
|
||||
int* fooPtr = nullptr;
|
||||
auto someRef = ToMaybeRef(fooPtr);
|
||||
|
||||
static_assert(std::is_same_v<decltype(someRef), Maybe<int&>>);
|
||||
|
||||
MOZ_RELEASE_ASSERT(someRef.isNothing());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче