Bug 1651016 - Allow MOZ_TRY/QM_TRY to handle bool values by wrapping them with OkIf; r=dom-workers-and-storage-reviewers,ttung,sg,asuth
☠☠ backed out by ded0022e1004 ☠ ☠
authorJan Varga <jvarga@mozilla.com>
Thu, 23 Jul 2020 18:13:32 +0000
changeset 541800 9142d4249c29f40502d98e34a5b365e1e0515ecc
parent 541799 8d2cc761e5ba28d7d5361160937565f20899481f
child 541801 5d5b249ca66438a1ed37ce92966010d091ff87de
push id37633
push userccoroiu@mozilla.com
push dateFri, 24 Jul 2020 09:32:06 +0000
treeherdermozilla-central@141543043270 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdom-workers-and-storage-reviewers, ttung, sg, asuth
bugs1651016
milestone80.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1651016 - Allow MOZ_TRY/QM_TRY to handle bool values by wrapping them with OkIf; r=dom-workers-and-storage-reviewers,ttung,sg,asuth Differential Revision: https://phabricator.services.mozilla.com/D83928
dom/quota/QuotaCommon.h
dom/quota/test/gtest/TestQuotaCommon.cpp
--- a/dom/quota/QuotaCommon.h
+++ b/dom/quota/QuotaCommon.h
@@ -343,16 +343,27 @@
 
 class nsIEventTarget;
 class nsIFile;
 
 namespace mozilla {
 
 class LogModule;
 
+struct NotOk {};
+
+// Allow MOZ_TRY/QM_TRY to handle `bool` values by wrapping them with OkIf.
+// TODO: Maybe move this to mfbt/ResultExtensions.h
+inline Result<Ok, NotOk> OkIf(bool aValue) {
+  if (aValue) {
+    return Ok();
+  }
+  return Err(NotOk());
+}
+
 namespace dom {
 namespace quota {
 
 extern const char kQuotaGenericDelimiter;
 
 // Telemetry keys to indicate types of errors.
 #ifdef NIGHTLY_BUILD
 extern const nsLiteralCString kQuotaInternalError;
--- a/dom/quota/test/gtest/TestQuotaCommon.cpp
+++ b/dom/quota/test/gtest/TestQuotaCommon.cpp
@@ -145,8 +145,22 @@ TEST(QuotaCommon_TryVar, Failure_NoErr)
     uint32_t x;
     QM_TRY_VAR(x, task(), QM_VOID);
 
     flag = true;
   }();
 
   EXPECT_FALSE(flag);
 }
+
+TEST(QuotaCommon_OkIf, True)
+{
+  auto res = OkIf(true);
+
+  EXPECT_TRUE(res.isOk());
+}
+
+TEST(QuotaCommon_SuccessIf, False)
+{
+  auto res = OkIf(false);
+
+  EXPECT_TRUE(res.isErr());
+}