Skip to content

Commit 04ffa31

Browse files
committed
Add support for const values in StringMap
1 parent 3ad27dd commit 04ffa31

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

source/mir/string_map.d

Lines changed: 18 additions & 4 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
{
@@ -1017,6 +1017,20 @@ struct StringMap(T, U = uint)
10171017
private Impl* implementation;
10181018
}
10191019

1020+
///
1021+
void f()
1022+
{
1023+
class C { this(int x) { this.x = x; } int x; }
1024+
StringMap!(const C) table;
1025+
auto v0 = new C(42);
1026+
auto v1 = new C(43);
1027+
table["0"] = v0;
1028+
table["1"] = v1;
1029+
assert(table.keys == ["0", "1"]);
1030+
assert(table.values == [v0, v1]);
1031+
static assert(is(typeof(table.values) == const(C)[]));
1032+
}
1033+
10201034
version(mir_test)
10211035
///
10221036
unittest

0 commit comments

Comments
 (0)