From 8e3fa3f4a58bfe86f44856c4fb8796c663322074 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Wed, 27 Apr 2022 19:08:39 +0000 Subject: [PATCH] Don't assume allocators can be compared to null (#185) --- src/containers/cyclicbuffer.d | 3 ++- src/containers/dynamicarray.d | 9 ++++++++- src/containers/hashmap.d | 9 ++++++--- src/containers/hashset.d | 6 ++++-- src/containers/openhashset.d | 6 ++++-- src/containers/simdset.d | 3 ++- src/containers/slist.d | 3 ++- src/containers/ttree.d | 3 ++- src/containers/unrolledlist.d | 3 ++- 9 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/containers/cyclicbuffer.d b/src/containers/cyclicbuffer.d index 8a317f7..ee16790 100644 --- a/src/containers/cyclicbuffer.d +++ b/src/containers/cyclicbuffer.d @@ -40,7 +40,8 @@ struct CyclicBuffer(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange this(Allocator allocator) nothrow pure @safe @nogc in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do { diff --git a/src/containers/dynamicarray.d b/src/containers/dynamicarray.d index e5a164a..90c16f4 100644 --- a/src/containers/dynamicarray.d +++ b/src/containers/dynamicarray.d @@ -53,7 +53,8 @@ struct DynamicArray(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange this(Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do { @@ -719,3 +720,9 @@ version(emsi_containers_unittest) unittest arr[1 .. 4] = [12, 13, 14]; assert(arr[] == [1, 12, 13, 14, 5]); } + +version(emsi_containers_unittest) unittest +{ + import std.experimental.allocator : RCIAllocator; + auto a = DynamicArray!(int, RCIAllocator)(RCIAllocator.init); +} diff --git a/src/containers/hashmap.d b/src/containers/hashmap.d index f9d03d9..4afd9b7 100644 --- a/src/containers/hashmap.d +++ b/src/containers/hashmap.d @@ -41,7 +41,8 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K this(Allocator allocator) pure nothrow @nogc @safe in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do { @@ -55,7 +56,8 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K this(size_t bucketCount, Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); assert((bucketCount & (bucketCount - 1)) == 0, "bucketCount must be a power of two"); } do @@ -66,7 +68,8 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K invariant { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } } else diff --git a/src/containers/hashset.d b/src/containers/hashset.d index 3f01c69..cf1c3e8 100644 --- a/src/containers/hashset.d +++ b/src/containers/hashset.d @@ -39,7 +39,8 @@ struct HashSet(T, Allocator = Mallocator, alias hashFunction = generateHash!T, this(Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do { @@ -53,7 +54,8 @@ struct HashSet(T, Allocator = Mallocator, alias hashFunction = generateHash!T, this(size_t bucketCount, Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); assert ((bucketCount & (bucketCount - 1)) == 0, "bucketCount must be a power of two"); } do diff --git a/src/containers/openhashset.d b/src/containers/openhashset.d index 69c0454..e27b007 100644 --- a/src/containers/openhashset.d +++ b/src/containers/openhashset.d @@ -40,7 +40,8 @@ struct OpenHashSet(T, Allocator = Mallocator, this(Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do { @@ -56,7 +57,8 @@ struct OpenHashSet(T, Allocator = Mallocator, this(size_t initialCapacity, Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); assert ((initialCapacity & initialCapacity - 1) == 0, "initialCapacity must be a power of 2"); } do diff --git a/src/containers/simdset.d b/src/containers/simdset.d index cbdb984..8effa3a 100644 --- a/src/containers/simdset.d +++ b/src/containers/simdset.d @@ -39,7 +39,8 @@ version (D_InlineAsm_X86_64) struct SimdSet(T, Allocator = Mallocator) this(Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do { diff --git a/src/containers/slist.d b/src/containers/slist.d index 3c6097b..a0d1de8 100644 --- a/src/containers/slist.d +++ b/src/containers/slist.d @@ -36,7 +36,8 @@ struct SList(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange!T) this(Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do { diff --git a/src/containers/ttree.d b/src/containers/ttree.d index 8e312bf..5e7e908 100644 --- a/src/containers/ttree.d +++ b/src/containers/ttree.d @@ -54,7 +54,8 @@ struct TTree(T, Allocator = Mallocator, bool allowDuplicates = false, this(Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do { diff --git a/src/containers/unrolledlist.d b/src/containers/unrolledlist.d index 2d53936..15d963b 100644 --- a/src/containers/unrolledlist.d +++ b/src/containers/unrolledlist.d @@ -47,7 +47,8 @@ struct UnrolledList(T, Allocator = Mallocator, this(Allocator allocator) in { - assert(allocator !is null, "Allocator must not be null"); + static if (is(typeof(allocator is null))) + assert(allocator !is null, "Allocator must not be null"); } do {