Bug 1659680 - QM: Introduce ToResultGet and use it in EnsureStorageIsInitialized; r=dom-workers-and-storage-reviewers,sg

Differential Revision: https://phabricator.services.mozilla.com/D87422
This commit is contained in:
Jan Varga 2020-08-24 17:41:30 +00:00
Родитель b5a52cf1f5
Коммит 57c6350d78
3 изменённых файлов: 81 добавлений и 9 удалений

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

@ -6426,15 +6426,9 @@ nsresult QuotaManager::EnsureStorageIsInitialized() {
}
}
// TODO: Convert to QM_TRY_VAR once we have an adapter for it.
nsCOMPtr<mozIStorageService> ss;
{
nsresult rv;
ss = do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
QM_TRY_VAR(auto ss, ToResultGet<nsCOMPtr<mozIStorageService>>(
MOZ_SELECT_OVERLOAD(do_GetService),
MOZ_STORAGE_SERVICE_CONTRACTID));
QM_TRY_VAR(auto connection,
ToResultInvoke<nsCOMPtr<mozIStorageConnection>>(

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

@ -21,6 +21,13 @@
#define MOZ_REMOVE_PAREN_HELPER3(...) MOZ_REMOVE_PAREN_HELPER4_##__VA_ARGS__
#define MOZ_REMOVE_PAREN_HELPER4_MOZ_REMOVE_PAREN_HELPER
// See https://florianjw.de/en/passing_overloaded_functions.html
// TODO: Add a test for this macro.
#define MOZ_SELECT_OVERLOAD(func) \
[](auto&&... aArgs) -> decltype(auto) { \
return func(std::forward<decltype(aArgs)>(aArgs)...); \
}
#define BEGIN_QUOTA_NAMESPACE \
namespace mozilla { \
namespace dom { \
@ -447,6 +454,17 @@ auto ErrToOkOrErr(nsresult aValue) -> Result<V, nsresult> {
return Err(aValue);
}
// TODO: Maybe move this to mfbt/ResultExtensions.h
template <typename R, typename Func, typename... Args>
Result<R, nsresult> ToResultGet(const Func& aFunc, Args&&... aArgs) {
nsresult rv;
R res = aFunc(std::forward<Args>(aArgs)..., &rv);
if (NS_FAILED(rv)) {
return Err(rv);
}
return res;
}
namespace dom {
namespace quota {

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

@ -379,3 +379,63 @@ TEST(QuotaCommon_ErrToOkOrErr, NsresultToNsCOMPtr_nullptr_Err)
EXPECT_TRUE(res.isErr());
EXPECT_EQ(res.unwrapErr(), NS_ERROR_UNEXPECTED);
}
TEST(QuotaCommon_ToResultGet, Lambda_NoInput)
{
auto task = [](nsresult* aRv) -> int32_t {
*aRv = NS_OK;
return 42;
};
auto res = ToResultGet<int32_t>(task);
static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);
EXPECT_TRUE(res.isOk());
EXPECT_EQ(res.unwrap(), 42);
}
TEST(QuotaCommon_ToResultGet, Lambda_NoInput_Err)
{
auto task = [](nsresult* aRv) -> int32_t {
*aRv = NS_ERROR_FAILURE;
return -1;
};
auto res = ToResultGet<int32_t>(task);
static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);
EXPECT_TRUE(res.isErr());
EXPECT_EQ(res.unwrapErr(), NS_ERROR_FAILURE);
}
TEST(QuotaCommon_ToResultGet, Lambda_WithInput)
{
auto task = [](int32_t aValue, nsresult* aRv) -> int32_t {
*aRv = NS_OK;
return aValue * 2;
};
auto res = ToResultGet<int32_t>(task, 42);
static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);
EXPECT_TRUE(res.isOk());
EXPECT_EQ(res.unwrap(), 84);
}
TEST(QuotaCommon_ToResultGet, Lambda_WithInput_Err)
{
auto task = [](int32_t aValue, nsresult* aRv) -> int32_t {
*aRv = NS_ERROR_FAILURE;
return -1;
};
auto res = ToResultGet<int32_t>(task, 42);
static_assert(std::is_same_v<decltype(res), Result<int32_t, nsresult>>);
EXPECT_TRUE(res.isErr());
EXPECT_EQ(res.unwrapErr(), NS_ERROR_FAILURE);
}