Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 24285 - Cannot swap a std.typecons.Tuple #8864

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions std/algorithm/mutation.d
Original file line number Diff line number Diff line change
Expand Up @@ -2885,6 +2885,16 @@ if (isBlitAssignable!T && !is(typeof(lhs.proxySwap(rhs))))
{
if (&lhs != &rhs)
{
static if (__traits(compiles, lhs.tupleof = rhs.tupleof))
{
if (__ctfe)
{
// can't reinterpret cast
foreach (i, ref e; lhs.tupleof)
swap(e, rhs.tupleof[i]);
return;
}
}
// For structs with non-trivial assignment, move memory directly
ubyte[T.sizeof] t = void;
auto a = (cast(ubyte*) &lhs)[0 .. T.sizeof];
Expand Down Expand Up @@ -3128,6 +3138,25 @@ if (isBlitAssignable!T && !is(typeof(lhs.proxySwap(rhs))))
swap(a3, a4);
}

// https://issues.dlang.org/show_bug.cgi?id=21429
@safe unittest
{
enum e = (){
Tuple!int a = 5, b = 6;
swap(a, b);
assert(a[0] == 6);
assert(b[0] == 5);
return 0;
}();
}

/// ditto
void swap(T)(ref T lhs, ref T rhs)
if (is(typeof(lhs.proxySwap(rhs))))
{
lhs.proxySwap(rhs);
}

/**
Swaps two elements in-place of a range `r`,
specified by their indices `i1` and `i2`.
Expand Down
Loading