Skip to content

Commit e6b3cb2

Browse files
authored
Add support for const values in StringMap (#418)
1 parent 3ad27dd commit e6b3cb2

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

source/mir/string_map.d

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Params:
3030
U = an unsigned type that can hold an index of keys. `U.max` must be less then the maximum possible number of struct members.
3131
+/
3232
struct StringMap(T, U = uint)
33-
if (isMutable!T && !is(typeof(T.opPostMove)) && __traits(isUnsigned, U))
33+
if (!is(typeof(T.opPostMove)) && __traits(isUnsigned, U))
3434
{
3535
import mir.utility: _expect;
3636
import core.lifetime: move;
@@ -632,7 +632,7 @@ struct StringMap(T, U = uint)
632632
assert (index < length);
633633
index = implementation._indices[index];
634634
assert (index < length);
635-
implementation._values[index] = move(value);
635+
move(cast()value, cast()(implementation._values[index]));
636636
return implementation._values[index];
637637
}
638638
assert (index <= length);
@@ -643,7 +643,7 @@ struct StringMap(T, U = uint)
643643
}
644644
}
645645
assert (index <= length);
646-
implementation.insertAt(key, move(value), index);
646+
implementation.insertAt(key, move(cast()value), index);
647647
index = implementation._indices[index];
648648
return implementation._values[index];
649649
}
@@ -843,7 +843,7 @@ struct StringMap(T, U = uint)
843843
}
844844
{
845845
auto a = values;
846-
a ~= move(value);
846+
a ~= move(cast()value);
847847
_values = a.ptr;
848848
}
849849
{
@@ -1019,7 +1019,33 @@ struct StringMap(T, U = uint)
10191019

10201020
version(mir_test)
10211021
///
1022-
unittest
1022+
@safe unittest
1023+
{
1024+
class C
1025+
{
1026+
this(int x) { this.x = x; }
1027+
int x;
1028+
bool opEquals(scope const C rhs) const scope @safe pure nothrow @nogc
1029+
{
1030+
return x == rhs.x;
1031+
}
1032+
}
1033+
StringMap!(const C) table;
1034+
const v0 = new C(42);
1035+
const v1 = new C(43);
1036+
table["0"] = v0;
1037+
table["1"] = v1;
1038+
assert(table.keys == ["0", "1"]);
1039+
static if (__VERSION__ > 2098) // See https://github.com/libmir/mir-algorithm/runs/6809888795?check_suite_focus=true#step:5:17
1040+
{
1041+
assert(table.values == [v0, v1]); // TODO: qualify unittest as `pure` when this is inferred `pure`
1042+
}
1043+
static assert(is(typeof(table.values) == const(C)[]));
1044+
}
1045+
1046+
version(mir_test)
1047+
///
1048+
@safe pure unittest
10231049
{
10241050
StringMap!int table;
10251051
table["L"] = 3;
@@ -1054,7 +1080,7 @@ unittest
10541080

10551081
version(mir_test)
10561082
///
1057-
@safe unittest
1083+
@safe pure unittest
10581084
{
10591085
static void testEquals(X, Y)()
10601086
{
@@ -1089,7 +1115,7 @@ version(mir_test)
10891115
}
10901116

10911117
version(mir_test)
1092-
@safe unittest
1118+
@safe pure unittest
10931119
{
10941120
import mir.algebraic_alias.json: JsonAlgebraic;
10951121
import mir.string_map: StringMap;

0 commit comments

Comments
 (0)