Skip to content

Commit 7cef4f7

Browse files
authored
Generalize StringMap.opEquals to other values (#378)
* Generalize StringMap.opEquals to other values * Make StringMap.opEquals @safe along with test * Use scalar comparison instead of array comparison in static assert
1 parent 73cbb22 commit 7cef4f7

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

source/mir/string_map.d

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ struct StringMap(T, U = uint)
4141

4242
///
4343
// current implementation is workaround for linking bugs when used in self referencing algebraic types
44-
bool opEquals(const StringMap rhs) const
44+
bool opEquals(V)(scope const StringMap!(V, U) rhs) const
4545
{
46+
// NOTE: moving this to template restriction fails with recursive template instanation
47+
static assert(is(typeof(T.init == V.init) : bool),
48+
"Unsupported rhs of type " ~ typeof(rhs).stringof); // TODO:
4649
if (keys != rhs.keys)
4750
return false;
4851
if (implementation)
4952
foreach (const i; 0 .. implementation._length)
50-
if (implementation._values[i] != rhs.implementation._values[i])
53+
if (implementation.values[i] != rhs.implementation.values[i]) // needs `values` instead of `_values` to be @safe
5154
return false;
5255
return true;
5356
}
@@ -738,6 +741,23 @@ unittest
738741
assert(table == table);
739742
}
740743

744+
version(mir_test)
745+
///
746+
@safe unittest
747+
{
748+
StringMap!int x;
749+
x["L"] = 3;
750+
x["A"] = 2;
751+
x["val"] = 1;
752+
753+
StringMap!uint y;
754+
y["L"] = 3;
755+
y["A"] = 2;
756+
y["val"] = 1;
757+
758+
assert(x == y);
759+
}
760+
741761
private struct StructImpl(T, U = uint)
742762
if (!__traits(hasMember, T, "opPostMove") && __traits(isUnsigned, U))
743763
{

0 commit comments

Comments
 (0)