RCAbort

Hook implementation that behaves same as Abort hook, but uses refcounted payload instead, which also enables us to check, if the result was properly checked before it is discarded.

Members

Static functions

onUnchecked
void onUnchecked()
Undocumented in source. Be warned that the author may not have intended to support it.

Static variables

enableCopyConstructor
bool enableCopyConstructor;

Copy constructor is enabled so the reference counting makes sense

enableDefaultConstructor
bool enableDefaultConstructor;

Default constructor for Expected is disabled. Same with the opAssign, so Expected can be only constructed once and not modified afterwards.

enableRefCountedPayload
bool enableRefCountedPayload;

Enabled reference counted payload

Examples

// behavior checks
static assert(!isDefaultConstructorEnabled!RCAbort);
static assert(isCopyConstructorEnabled!RCAbort);
static assert(isRefCountedPayloadEnabled!RCAbort);

// basics
assert(ok!(string, RCAbort)(42) == 42);
assert(err!(int, RCAbort)("foo").error == "foo");

// checked
{
    auto res = ok!(string, RCAbort)(42);
    assert(!res.checked);
    assert(res);
    assert(res.checked);
}

// unchecked - throws assert
version (D_Exceptions) () @trusted { assertThrown!Throwable({ ok!(string, RCAbort)(42); }()); }();

{
    auto res = ok!(string, RCAbort)(42);
    {
        auto res2 = res;
        assert(!res.checked);
        assert(res.refCount == 2);
        assert(res2.refCount == 2);
    }
    assert(res.refCount == 1);
    assert(res.hasValue);
}

// chaining
assert(err!(int, RCAbort)("foo").orElse!(() => ok!(string, RCAbort)(42)) == 42);
assert(ok!(string, RCAbort)(42).andThen!(() => err!(int, RCAbort)("foo")).error == "foo");
version (D_Exceptions)
{
    () @trusted
    {
        assertThrown!Throwable(err!(int, RCAbort)("foo").orElse!(() => ok!(string, RCAbort)(42)));
        assertThrown!Throwable(ok!(string, RCAbort)(42).andThen!(() => err!(int, RCAbort)("foo")));
    }();
}

Meta