Bug 1732642 - Handle complex types in MOZ_TO_RESULT_INVOKE_MEMBER_TYPED; r=dom-storage-reviewers,jari

Differential Revision: https://phabricator.services.mozilla.com/D126785
This commit is contained in:
Jan Varga 2021-11-30 05:05:53 +00:00
Родитель 1af2efb302
Коммит ead7da2d9c
2 изменённых файлов: 89 добавлений и 1 удалений

Просмотреть файл

@ -13,6 +13,7 @@
#include "mozilla/Assertions.h"
#include "nscore.h"
#include "prtypes.h"
#include "mozilla/dom/quota/RemoveParen.h"
namespace mozilla {
@ -360,7 +361,7 @@ auto ToResultInvokeMember(const SmartPtr<const T>& aObj,
// auto existsOrErr =
// MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCOMPtr<nsIFile>, file, Clone);
#define MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(resultType, obj, methodname, ...) \
::mozilla::ToResultInvoke<resultType>( \
::mozilla::ToResultInvoke<MOZ_REMOVE_PAREN(resultType)>( \
::std::mem_fn( \
&::mozilla::detail::DerefedType<decltype(obj)>::methodname), \
(obj), ##__VA_ARGS__)

Просмотреть файл

@ -38,6 +38,14 @@ class TestClass {
}
nsresult NonOverloadedNoInputFailsRef(int& aOut) { return NS_ERROR_FAILURE; }
nsresult NonOverloadedNoInputComplex(std::pair<int, int>* aOut) {
*aOut = std::pair{kTestValue, kTestValue};
return NS_OK;
}
nsresult NonOverloadedNoInputFailsComplex(std::pair<int, int>* aOut) {
return NS_ERROR_FAILURE;
}
nsresult NonOverloadedWithInput(int aIn, int* aOut) {
*aOut = aIn;
return NS_OK;
@ -233,6 +241,32 @@ TEST(ResultExtensions_ToResultInvokeMember, NoInput_Ref)
}
}
TEST(ResultExtensions_ToResultInvokeMember, NoInput_Complex)
{
TestClass foo;
// success
{
auto valOrErr =
ToResultInvokeMember(foo, &TestClass::NonOverloadedNoInputComplex);
static_assert(std::is_same_v<decltype(valOrErr),
Result<std::pair<int, int>, nsresult>>);
ASSERT_TRUE(valOrErr.isOk());
ASSERT_EQ((std::pair{TestClass::kTestValue, TestClass::kTestValue}),
valOrErr.unwrap());
}
// failure
{
auto valOrErr =
ToResultInvokeMember(foo, &TestClass::NonOverloadedNoInputFailsComplex);
static_assert(std::is_same_v<decltype(valOrErr),
Result<std::pair<int, int>, nsresult>>);
ASSERT_TRUE(valOrErr.isErr());
ASSERT_EQ(NS_ERROR_FAILURE, valOrErr.unwrapErr());
}
}
TEST(ResultExtensions_ToResultInvokeMember, WithInput)
{
TestClass foo;
@ -343,6 +377,33 @@ TEST(ResultExtensions_ToResultInvokeMember, NoInput_Ref_Macro)
}
}
TEST(ResultExtensions_ToResultInvokeMember, NoInput_Complex_Macro)
{
TestClass foo;
// success
{
auto valOrErr =
MOZ_TO_RESULT_INVOKE_MEMBER(foo, NonOverloadedNoInputComplex);
static_assert(std::is_same_v<decltype(valOrErr),
Result<std::pair<int, int>, nsresult>>);
ASSERT_TRUE(valOrErr.isOk());
ASSERT_EQ((std::pair{TestClass::kTestValue, TestClass::kTestValue}),
valOrErr.unwrap());
}
// failure
{
auto valOrErr =
MOZ_TO_RESULT_INVOKE_MEMBER(foo, NonOverloadedNoInputFailsComplex);
static_assert(std::is_same_v<decltype(valOrErr),
Result<std::pair<int, int>, nsresult>>);
ASSERT_TRUE(valOrErr.isErr());
ASSERT_EQ(NS_ERROR_FAILURE, valOrErr.unwrapErr());
}
}
TEST(ResultExtensions_ToResultInvokeMember, WithInput_Macro)
{
TestClass foo;
@ -388,6 +449,32 @@ TEST(ResultExtensions_ToResultInvokeMember, NoOutput_Macro)
}
}
TEST(ResultExtensions_ToResultInvokeMember, NoInput_Complex_Macro_Typed)
{
TestClass foo;
// success
{
auto valOrErr = MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
(std::pair<int, int>), foo, NonOverloadedNoInputComplex);
static_assert(std::is_same_v<decltype(valOrErr),
Result<std::pair<int, int>, nsresult>>);
ASSERT_TRUE(valOrErr.isOk());
ASSERT_EQ((std::pair{TestClass::kTestValue, TestClass::kTestValue}),
valOrErr.unwrap());
}
// failure
{
auto valOrErr = MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
(std::pair<int, int>), foo, NonOverloadedNoInputFailsComplex);
static_assert(std::is_same_v<decltype(valOrErr),
Result<std::pair<int, int>, nsresult>>);
ASSERT_TRUE(valOrErr.isErr());
ASSERT_EQ(NS_ERROR_FAILURE, valOrErr.unwrapErr());
}
}
TEST(ResultExtensions_ToResultInvokeMember, RefPtr_NoInput)
{
auto foo = MakeRefPtr<RefCountedTestClass>();