The allocator is global, so no need to pass it in to the constructor
Non-singleton allocator, must be passed in
Borrow the owned pointer. Can be @safe with DIP1000 and if used in a scope fashion.
Move from another smart pointer
"Truthiness" cast
release ownership
Releases ownership and transfers it to the returned Unique object.
Factory method so can construct with zero args.
Factory method. Not necessary with non-global allocator but included for symmetry.
struct S { private ulong zeroArgsCtorTest = 3; } auto s = Unique!S.construct(); static assert(is(typeof(s) == Unique!S)); assert(s._object !is null); assert(s.zeroArgsCtorTest == 3);
import std.experimental.allocator: dispose; import core.exception: AssertError; try { auto allocator = TestAllocator(); auto ptr = Unique!(Struct, TestAllocator*)(&allocator, 42); ptr.release; assert(Struct.numStructs == 1); } catch(AssertError e) { // TestAllocator should throw due to memory leak version(unitThreadedLight) {} else "Memory leak in TestAllocator".should.be in e.msg; return; } assert(0); // should throw above
A unique pointer similar to C++'s std::unique_ptr.