| 1 |
module imports.link14541traits; |
| 2 |
|
| 3 |
template hasElaborateAssign(S) |
| 4 |
{ |
| 5 |
static if (is(S == struct)) |
| 6 |
{ |
| 7 |
extern __gshared S lvalue; |
| 8 |
|
| 9 |
enum hasElaborateAssign = is(typeof(S.init.opAssign(S.init))) || |
| 10 |
is(typeof(S.init.opAssign(lvalue))); |
| 11 |
} |
| 12 |
else |
| 13 |
{ |
| 14 |
enum bool hasElaborateAssign = false; |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
void swap(T)(ref T lhs, ref T rhs) @trusted pure nothrow @nogc |
| 19 |
{ |
| 20 |
static if (hasElaborateAssign!T) |
| 21 |
{ |
| 22 |
} |
| 23 |
else |
| 24 |
{ |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
template Tuple(Types...) |
| 29 |
{ |
| 30 |
struct Tuple |
| 31 |
{ |
| 32 |
Types field; |
| 33 |
alias field this; |
| 34 |
|
| 35 |
this(Types values) |
| 36 |
{ |
| 37 |
field[] = values[]; |
| 38 |
} |
| 39 |
|
| 40 |
void opAssign(R)(auto ref R rhs) |
| 41 |
{ |
| 42 |
static if (is(R : Tuple!Types) && !__traits(isRef, rhs)) |
| 43 |
{ |
| 44 |
// Use swap-and-destroy to optimize rvalue assignment |
| 45 |
swap!(Tuple!Types)(this, rhs); |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
// Do not swap; opAssign should be called on the fields. |
| 50 |
field[] = rhs.field[]; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |