Commit 2ae7ca9b authored by Kenton Varda's avatar Kenton Varda

Add Maybe<Own<T>>::emplace() to avoid assign-then-assert-nonnull.

I have this pattern:

    Maybe<Own<T>> foo;

    // ...

    foo = heap<T>();
    KJ_ASSERT_NONNULL(foo)->doSomething();

The assertion feels non-type-safe.

Now you can do:

    auto& ref = foo.emplace(heap<T>());
    ref.doSomething();
parent 0bf96571
......@@ -318,6 +318,13 @@ public:
inline Maybe(decltype(nullptr)) noexcept: ptr(nullptr) {}
inline Own<T>& emplace(Own<T> value) {
// Assign the Maybe to the given value and return the content. This avoids the need to do a
// KJ_ASSERT_NONNULL() immediately after setting the Maybe just to read it back again.
ptr = kj::mv(value);
return ptr;
}
inline operator Maybe<T&>() { return ptr.get(); }
inline operator Maybe<const T&>() const { return ptr.get(); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment