1 /// A deliberately problematic type for lifetime, constness, etc. 2 module rebindable.ProblematicType; 3 4 /** 5 * A deliberately problematic type for lifetime, constness etc. 6 * 7 * Use to test containers like so: 8 * 9 * --- 10 * int refs; 11 * int* refsPtr = () @trusted { return &refs; }(); 12 * ... do things with ProblematicType(refsPtr) ... 13 * assert(refs == 0); 14 * --- 15 */ 16 public struct ProblematicType 17 { 18 import std.datetime : SysTime; 19 20 immutable int[] i; 21 22 SysTime st; 23 24 @disable this(); 25 26 bool properlyInitialized = false; 27 28 invariant (properlyInitialized); 29 30 void opAssign(ProblematicType) 31 { 32 assert(false); 33 } 34 35 // count references to confirm that every constructor call matches one destructor call 36 int* refs; 37 38 this(int* refs) pure @safe @nogc 39 { 40 this.properlyInitialized = true; 41 this.refs = refs; 42 (*refs)++; 43 } 44 45 this(this) pure @safe @nogc 46 { 47 (*refs)++; 48 } 49 50 // Since a destructor is defined, we will definitely 51 // assert out if the .init value is ever destructed. 52 ~this() pure @safe @nogc 53 in (refs) 54 out (; *refs >= 0) 55 { 56 (*refs)--; 57 } 58 }