diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index d748e6e27..340d78b74 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -4491,6 +4491,12 @@ void test_maybe() { do_test(m4 && *m4 == "hi"); maybe_t m5 = m0; do_test(!m5); + + maybe_t acquire_test("def"); + do_test(acquire_test); + std::string res = acquire_test.acquire(); + do_test(!acquire_test); + do_test(res == "def"); } void test_layout_cache() { diff --git a/src/maybe.h b/src/maybe.h index 898731f9d..9e8301ed5 100644 --- a/src/maybe.h +++ b/src/maybe.h @@ -64,6 +64,14 @@ class maybe_t { return *reinterpret_cast(storage); } + // Transfer the value to the caller. + T acquire() { + assert(filled && "maybe_t does not have a value"); + T res = std::move(value()); + reset(); + return res; + } + // Clear the value. void reset() { if (filled) {