Merge pull request #26 from s-ludwig/non_copyable_types

Fix using non-copyable types with TaggedUnion.
This commit is contained in:
Sönke Ludwig 2019-02-24 10:53:32 +01:00 committed by GitHub
commit c1e3e5a6a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -76,12 +76,12 @@ struct TaggedUnion(U) if (is(U == union) || is(U == struct) || is(U == enum))
static if (!isUnitType!(FieldTypes[ti])) {
this(FieldTypes[ti] value)
{
set!(cast(Kind)ti)(value);
set!(cast(Kind)ti)(move(value));
}
void opAssign(FieldTypes[ti] value)
{
set!(cast(Kind)ti)(value);
set!(cast(Kind)ti)(move(value));
}
}
@ -394,6 +394,21 @@ unittest { // test woraround for Phobos issue 19696
static assert(is(TU.FieldTypes[0] == T));
}
unittest { // non-copyable types
import std.traits : isCopyable;
struct S { @disable this(this); }
struct U {
int i;
S s;
}
alias TU = TaggedUnion!U;
static assert(!isCopyable!TU);
auto tu = TU(42);
tu.setS(S.init);
}
/** Dispatches the value contained on a `TaggedUnion` to a set of visitors.
@ -741,7 +756,7 @@ package void rawEmplace(T)(void[] dst, ref T src)
} else {
import std.conv : emplace;
emplace!T(&tdst[0]);
tdst[0] = src;
rawSwap(tdst[0], src);
}
}