Skip to content

Commit a60026e

Browse files
committed
add StringMap.opIndexAssign generic overload
1 parent c8e0fcb commit a60026e

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

source/mir/algebraic_alias/json.d

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,43 +58,38 @@ unittest
5858
assert(value.kind == JsonAlgebraic.Kind.null_);
5959

6060
// Boolean
61-
value = true;
62-
object["bool"] = value;
61+
value = object["bool"] = true;
6362
assert(!value.isNull);
6463
assert(value == true);
6564
assert(value.kind == JsonAlgebraic.Kind.boolean);
6665
assert(value.get!bool == true);
6766
assert(value.get!(JsonAlgebraic.Kind.boolean) == true);
6867

6968
// Null
70-
value = null;
71-
object["null"] = value;
69+
value = object["null"] = null;
7270
assert(value.isNull);
7371
assert(value == null);
7472
assert(value.kind == JsonAlgebraic.Kind.null_);
7573
assert(value.get!(typeof(null)) == null);
7674
assert(value.get!(JsonAlgebraic.Kind.null_) == null);
7775

7876
// String
79-
value = "s";
80-
object["string"] = value;
77+
value = object["string"] = "s";
8178
assert(value.kind == JsonAlgebraic.Kind.string);
8279
assert(value == "s");
8380
assert(value.get!string == "s");
8481
assert(value.get!(JsonAlgebraic.Kind.string) == "s");
8582

8683
// Integer
87-
value = 4;
88-
object["integer"] = value;
84+
value = object["integer"] = 4;
8985
assert(value.kind == JsonAlgebraic.Kind.integer);
9086
assert(value == 4);
9187
assert(value != 4.0);
9288
assert(value.get!long == 4);
9389
assert(value.get!(JsonAlgebraic.Kind.integer) == 4);
9490

9591
// Float
96-
value = 3.0;
97-
object["float"] = value;
92+
value = object["float"] = 3.0;
9893
assert(value.kind == JsonAlgebraic.Kind.float_);
9994
assert(value != 3);
10095
assert(value == 3.0);
@@ -104,8 +99,7 @@ unittest
10499
// Array
105100
JsonAlgebraic[] arr = [0, 1, 2, 3, 4].map!JsonAlgebraic.array;
106101

107-
value = arr;
108-
object["array"] = value;
102+
value = object["array"] = arr;
109103
assert(value.kind == JsonAlgebraic.Kind.array);
110104
assert(value == arr);
111105
assert(value.get!(JsonAlgebraic[])[3] == 3);
@@ -116,8 +110,7 @@ unittest
116110
assert(object["bool"] == "false"); // it is a string now
117111
object.remove("bool"); // remove the member
118112

119-
value = object;
120-
object["array"] = value;
113+
value = object["array"] = object;
121114
assert(value.kind == JsonAlgebraic.Kind.object);
122115
assert(value.get!(StringMap!JsonAlgebraic).keys is object.keys);
123116
assert(value.get!(StringMap!JsonAlgebraic).values is object.values);

source/mir/string_map.d

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ struct StringMap(T, U = uint)
527527
/++
528528
Complexity: `O(log(s))` (exist) or `O(n)` (not exist), where `s` is the count of the strings with the same length as they key.
529529
+/
530+
ref T opIndexAssign(R)(auto ref R value, string key) @trusted pure nothrow
531+
{
532+
import core.lifetime: forward, move;
533+
T v;
534+
v = forward!value;
535+
return opIndexAssign(move(v), key);
536+
}
537+
538+
/// ditto
530539
ref T opIndexAssign()(T value, string key) @trusted pure nothrow
531540
{
532541
size_t index;

0 commit comments

Comments
 (0)