Skip to content

Commit

Permalink
Fix MutexedAA.uniqueKey not compiling
Browse files Browse the repository at this point in the history
Apparently `cast(AA)aa` is not an lvalue, which lu.array.uniqueKey
requires for the parameter to be ref, and we have to dance around it
by passing `*(cast(AA*)&aa)` instead.

Add tests to make sure it stays working.
  • Loading branch information
zorael committed Jan 4, 2024
1 parent 049d442 commit 836c7d3
Showing 1 changed file with 58 additions and 36 deletions.
94 changes: 58 additions & 36 deletions source/lu/container.d
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@ struct MutexedAA(AA : V[K], V, K)
{
private:
import std.range.primitives : ElementEncodingType;
import std.traits : isIntegral;
import core.sync.mutex : Mutex;

/++
Expand Down Expand Up @@ -1281,47 +1282,51 @@ public:
return value;
}

/++
Reserves a unique key in the associative array.
Note: The key type must be an integral type.
static if (isIntegral!K)
{
/++
Reserves a unique key in the associative array.
Example:
---
MutexedAA!(string[int]) aa;
aa.setup(); // important!
Note: The key type must be an integral type.
int i = aa.uniqueKey;
assert(i > 0);
assert(aa.has(i));
assert(aa[i] == string.init);
---
Example:
---
MutexedAA!(string[int]) aa;
aa.setup(); // important!
Params:
min = Optional minimum key value; defaults to `1``.
max = Optional maximum key value; defaults to `K.max`, where `K` is
the key type of the passed associative array.
value = Optional value to assign to the key; defaults to `V.init`,
where `V` is the value type of the passed associative array.
int i = aa.uniqueKey;
assert(i > 0);
assert(aa.has(i));
assert(aa[i] == string.init);
---
Returns:
A unique key for the passed associative array, for which there is now
a value of `value`.`
Params:
min = Optional minimum key value; defaults to `1``.
max = Optional maximum key value; defaults to `K.max`, where `K` is
the key type of the passed associative array.
value = Optional value to assign to the key; defaults to `V.init`,
where `V` is the value type of the passed associative array.
Returns:
A unique key for the passed associative array, for which there is now
a value of `value`.`
See_Also:
[uniqueKey]
+/
auto uniqueKey()
(K min = 1,
K max = K.max,
V value = V.init)
in (mutex, typeof(this).stringof ~ " has null Mutex")
{
static import lu.array;

See_Also:
[uniqueKey]
+/
auto uniqueKey()
(K min = 1,
K max = K.max,
V value = V.init)
if (isIntegral!K)
in (mutex, typeof(this).stringof ~ " has null Mutex")
{
mutex.lock_nothrow();
auto key = .uniqueKey(cast(AA)aa, min, max, value);
mutex.unlock_nothrow();
return key;
mutex.lock_nothrow();
auto key = lu.array.uniqueKey(*(cast(AA*)&aa), min, max, value);
mutex.unlock_nothrow();
return key;
}
}

/++
Expand Down Expand Up @@ -1911,6 +1916,23 @@ unittest
aa[1] ~= [ 'd', 'e', 'f' ];
assert(aa[1] == "abcdef".dup);
}
{
MutexedAA!(int[int]) aa;
aa.setup();

immutable key = aa.uniqueKey;
assert(key > 0);

assert(aa.has(key));
assert(aa[key] == int.init);
aa.remove(key);
assert(!aa.has(key));

immutable key2 = aa.uniqueKey(1, 2, -1);
assert(key2 == 1);
assert(aa.has(key2));
assert(aa[key2] == -1);
}
static if (__VERSION__ >= 2088L)
{
MutexedAA!(int[int]) aa;
Expand Down

0 comments on commit 836c7d3

Please sign in to comment.