Skip to content

Commit b3fee62

Browse files
committed
fix variant
1 parent 6a95e2f commit b3fee62

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

source/mir/variant.d

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Compatible with BetterC mode.
2222
struct Variant(Types...)
2323
if (Types.length)
2424
{
25-
import core.lifetime: move;
25+
import mir.utility: swap;
2626
import mir.conv: emplaceRef;
2727
import std.meta: anySatisfy;
2828
import std.traits: Largest, hasElaborateDestructor, hasElaborateAssign;
@@ -77,30 +77,24 @@ pure nothrow @nogc:
7777
type = 0;
7878
}
7979

80-
///
81-
void opAssign(Variant value)
82-
{
83-
static if (hasDestructor)
84-
__dtor;
85-
emplaceRef(this, move(value));
86-
}
87-
8880
static foreach (i, T; Types)
8981
///
9082
void opAssign(T value)
9183
{
9284
static if (hasDestructor)
9385
__dtor;
9486
type = i + 1;
95-
emplaceRef(trustedGet!T, move(value));
87+
emplaceRef(trustedGet!T);
88+
swap(trustedGet!T, value);
9689
}
9790

9891
static foreach (i, T; Types)
9992
///
10093
this(T value)
10194
{
10295
type = i + 1;
103-
emplaceRef(trustedGet!T, move(value));
96+
emplaceRef(trustedGet!T);
97+
swap(trustedGet!T, value);
10498
}
10599

106100
static foreach (i, T; Types)
@@ -133,11 +127,21 @@ pure nothrow @nogc:
133127
return *cast(inout(T)*)payload.ptr;
134128
}
135129

136-
///
137-
bool empty() const @property
130+
/++
131+
Returns: true for the unassigned instance.
132+
+/
133+
bool empty() nothrow const @property
138134
{
139135
return type == 0;
140136
}
137+
138+
/++
139+
Returns: zero if the instance is unassigned and type index starting with 1 otherwise.
140+
+/
141+
uint typeId() nothrow const @property
142+
{
143+
return type;
144+
}
141145
}
142146

143147
/++
@@ -149,7 +153,7 @@ template visit(alias visitor, bool forceAllTypes = true)
149153
{
150154
import std.traits: Unqual;
151155
///
152-
auto ref visit(V, Args)(auto ref V variant, auto ref Args args)
156+
auto ref visit(V, Args...)(auto ref V variant, auto ref Args args)
153157
if (is(Unqual!V : Variant!Types, Types))
154158
{
155159
import mir.functional: forward;
@@ -186,7 +190,7 @@ template optionalVisit(alias visitor)
186190
{
187191
import std.traits: Unqual;
188192
///
189-
bool optionalVisit(Result, V, Args)(out Result result, auto ref V variant, auto ref Args args)
193+
bool optionalVisit(Result, V, Args...)(out Result result, auto ref V variant, auto ref Args args)
190194
if (is(Unqual!V : Variant!Types, Types))
191195
{
192196
import mir.functional: forward;

0 commit comments

Comments
 (0)