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 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

1 // behavior checks
2 static assert(!isDefaultConstructorEnabled!RCAbort);
3 static assert(isCopyConstructorEnabled!RCAbort);
4 static assert(isRefCountedPayloadEnabled!RCAbort);
5 
6 // basics
7 assert(ok!(string, RCAbort)(42) == 42);
8 assert(err!(int, RCAbort)("foo").error == "foo");
9 
10 // checked
11 {
12     auto res = ok!(string, RCAbort)(42);
13     assert(!res.checked);
14     assert(res);
15     assert(res.checked);
16 }
17 
18 // unchecked - throws assert
19 version (D_Exceptions) assertThrown!Throwable({ ok!(string, RCAbort)(42); }());
20 
21 {
22     auto res = ok!(string, RCAbort)(42);
23     {
24         auto res2 = res;
25         assert(!res.checked);
26         assert(res.refCount == 2);
27         assert(res2.refCount == 2);
28     }
29     assert(res.refCount == 1);
30     assert(res.hasValue);
31 }
32 
33 // chaining
34 assert(err!(int, RCAbort)("foo").orElse!(() => ok!(string, RCAbort)(42)) == 42);
35 assert(ok!(string, RCAbort)(42).andThen!(() => err!(int, RCAbort)("foo")).error == "foo");
36 version (D_Exceptions)
37 {
38     assertThrown!Throwable(err!(int, RCAbort)("foo").orElse!(() => ok!(string, RCAbort)(42)));
39     assertThrown!Throwable(ok!(string, RCAbort)(42).andThen!(() => err!(int, RCAbort)("foo")));
40 }

Meta