mirror of
https://github.com/dlang/phobos.git
synced 2025-05-04 00:54:05 +03:00
Merge pull request #3072 from quickfur/swap_docs
Use swap() unittests as ddoc'd examples.
This commit is contained in:
commit
9469e04844
1 changed files with 18 additions and 9 deletions
|
@ -1995,24 +1995,18 @@ if (isBlitAssignable!T && !is(typeof(lhs.proxySwap(rhs))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not yet documented
|
///
|
||||||
void swap(T)(ref T lhs, ref T rhs) if (is(typeof(lhs.proxySwap(rhs))))
|
|
||||||
{
|
|
||||||
lhs.proxySwap(rhs);
|
|
||||||
}
|
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
debug(std_algorithm) scope(success)
|
// Swapping POD (plain old data) types:
|
||||||
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
|
|
||||||
int a = 42, b = 34;
|
int a = 42, b = 34;
|
||||||
swap(a, b);
|
swap(a, b);
|
||||||
assert(a == 34 && b == 42);
|
assert(a == 34 && b == 42);
|
||||||
|
|
||||||
|
// Swapping structs with indirection:
|
||||||
static struct S { int x; char c; int[] y; }
|
static struct S { int x; char c; int[] y; }
|
||||||
S s1 = { 0, 'z', [ 1, 2 ] };
|
S s1 = { 0, 'z', [ 1, 2 ] };
|
||||||
S s2 = { 42, 'a', [ 4, 6 ] };
|
S s2 = { 42, 'a', [ 4, 6 ] };
|
||||||
//writeln(s2.tupleof.stringof);
|
|
||||||
swap(s1, s2);
|
swap(s1, s2);
|
||||||
assert(s1.x == 42);
|
assert(s1.x == 42);
|
||||||
assert(s1.c == 'a');
|
assert(s1.c == 'a');
|
||||||
|
@ -2022,12 +2016,15 @@ void swap(T)(ref T lhs, ref T rhs) if (is(typeof(lhs.proxySwap(rhs))))
|
||||||
assert(s2.c == 'z');
|
assert(s2.c == 'z');
|
||||||
assert(s2.y == [ 1, 2 ]);
|
assert(s2.y == [ 1, 2 ]);
|
||||||
|
|
||||||
|
// Immutables cannot be swapped:
|
||||||
immutable int imm1, imm2;
|
immutable int imm1, imm2;
|
||||||
static assert(!__traits(compiles, swap(imm1, imm2)));
|
static assert(!__traits(compiles, swap(imm1, imm2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
|
// Non-copyable types can still be swapped.
|
||||||
static struct NoCopy
|
static struct NoCopy
|
||||||
{
|
{
|
||||||
this(this) { assert(0); }
|
this(this) { assert(0); }
|
||||||
|
@ -2037,14 +2034,17 @@ void swap(T)(ref T lhs, ref T rhs) if (is(typeof(lhs.proxySwap(rhs))))
|
||||||
NoCopy nc1, nc2;
|
NoCopy nc1, nc2;
|
||||||
nc1.n = 127; nc1.s = "abc";
|
nc1.n = 127; nc1.s = "abc";
|
||||||
nc2.n = 513; nc2.s = "uvwxyz";
|
nc2.n = 513; nc2.s = "uvwxyz";
|
||||||
|
|
||||||
swap(nc1, nc2);
|
swap(nc1, nc2);
|
||||||
assert(nc1.n == 513 && nc1.s == "uvwxyz");
|
assert(nc1.n == 513 && nc1.s == "uvwxyz");
|
||||||
assert(nc2.n == 127 && nc2.s == "abc");
|
assert(nc2.n == 127 && nc2.s == "abc");
|
||||||
|
|
||||||
swap(nc1, nc1);
|
swap(nc1, nc1);
|
||||||
swap(nc2, nc2);
|
swap(nc2, nc2);
|
||||||
assert(nc1.n == 513 && nc1.s == "uvwxyz");
|
assert(nc1.n == 513 && nc1.s == "uvwxyz");
|
||||||
assert(nc2.n == 127 && nc2.s == "abc");
|
assert(nc2.n == 127 && nc2.s == "abc");
|
||||||
|
|
||||||
|
// Types containing non-copyable fields can also be swapped.
|
||||||
static struct NoCopyHolder
|
static struct NoCopyHolder
|
||||||
{
|
{
|
||||||
NoCopy noCopy;
|
NoCopy noCopy;
|
||||||
|
@ -2052,14 +2052,17 @@ void swap(T)(ref T lhs, ref T rhs) if (is(typeof(lhs.proxySwap(rhs))))
|
||||||
NoCopyHolder h1, h2;
|
NoCopyHolder h1, h2;
|
||||||
h1.noCopy.n = 31; h1.noCopy.s = "abc";
|
h1.noCopy.n = 31; h1.noCopy.s = "abc";
|
||||||
h2.noCopy.n = 65; h2.noCopy.s = null;
|
h2.noCopy.n = 65; h2.noCopy.s = null;
|
||||||
|
|
||||||
swap(h1, h2);
|
swap(h1, h2);
|
||||||
assert(h1.noCopy.n == 65 && h1.noCopy.s == null);
|
assert(h1.noCopy.n == 65 && h1.noCopy.s == null);
|
||||||
assert(h2.noCopy.n == 31 && h2.noCopy.s == "abc");
|
assert(h2.noCopy.n == 31 && h2.noCopy.s == "abc");
|
||||||
|
|
||||||
swap(h1, h1);
|
swap(h1, h1);
|
||||||
swap(h2, h2);
|
swap(h2, h2);
|
||||||
assert(h1.noCopy.n == 65 && h1.noCopy.s == null);
|
assert(h1.noCopy.n == 65 && h1.noCopy.s == null);
|
||||||
assert(h2.noCopy.n == 31 && h2.noCopy.s == "abc");
|
assert(h2.noCopy.n == 31 && h2.noCopy.s == "abc");
|
||||||
|
|
||||||
|
// Const types cannot be swapped.
|
||||||
const NoCopy const1, const2;
|
const NoCopy const1, const2;
|
||||||
static assert(!__traits(compiles, swap(const1, const2)));
|
static assert(!__traits(compiles, swap(const1, const2)));
|
||||||
}
|
}
|
||||||
|
@ -2155,6 +2158,12 @@ unittest
|
||||||
swap(b1, b2);
|
swap(b1, b2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Not yet documented
|
||||||
|
void swap(T)(ref T lhs, ref T rhs) if (is(typeof(lhs.proxySwap(rhs))))
|
||||||
|
{
|
||||||
|
lhs.proxySwap(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
void swapFront(R1, R2)(R1 r1, R2 r2)
|
void swapFront(R1, R2)(R1 r1, R2 r2)
|
||||||
if (isInputRange!R1 && isInputRange!R2)
|
if (isInputRange!R1 && isInputRange!R2)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue