From 95875a7e47f9a902e7d9748fb28f1709d4f9e761 Mon Sep 17 00:00:00 2001 From: Geod24 Date: Fri, 30 Aug 2019 11:17:41 +0900 Subject: [PATCH] Add an lvalue overload for opCmp --- source/geod24/bitblob.d | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/source/geod24/bitblob.d b/source/geod24/bitblob.d index fd51e23..bc7d03e 100644 --- a/source/geod24/bitblob.d +++ b/source/geod24/bitblob.d @@ -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 @@ -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 @@ -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: