Skip to content

Commit

Permalink
Add an lvalue overload for opCmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Geod24 committed Aug 30, 2019
1 parent 9417884 commit 95875a7
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions source/geod24/bitblob.d
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ public struct BitBlob (size_t Bits)
assert(0, "Unexpected char in string passed to BitBlob");
}

/// Support for comparison
public int opCmp (ref const typeof(this) s) const
{
// Reverse because little endian
Expand All @@ -242,6 +243,12 @@ public struct BitBlob (size_t Bits)
return b - s.data[idx];
return 0;
}

/// Support for comparison (rvalue overload)
public int opCmp (const typeof(this) s) const
{
return this.opCmp(s);
}
}

pure @safe nothrow @nogc unittest
Expand Down Expand Up @@ -359,6 +366,17 @@ unittest
static assert(CTFEability[] == GenesisBlockHash);
}

// Support for rvalue opCmp
unittest
{
alias Hash = BitBlob!(256);
import std.algorithm.sorting : sort;

static Hash getLValue(int) { return Hash.init; }
int[] array = [1, 2];
array.sort!((a, b) => getLValue(a) < getLValue(b));
}

version (unittest)
{
private:
Expand Down

0 comments on commit 95875a7

Please sign in to comment.