Skip to content

Commit a5c8440

Browse files
authored
Merge pull request #4681 from wilzbach/allocator_safe_4
Add attributes to makeArray - part 1 (length)
2 parents c8e0c6e + 6d9918e commit a5c8440

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

std/experimental/allocator/package.d

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ T[] makeArray(T, Allocator)(auto ref Allocator alloc, size_t length)
618618
auto m = alloc.allocate(T.sizeof * length);
619619
if (!m.ptr) return null;
620620
alias U = Unqual!T;
621-
return cast(T[]) uninitializedFillDefault(cast(U[]) m);
621+
return () @trusted { return cast(T[]) uninitializedFillDefault(cast(U[]) m); }();
622622
}
623623

624624
unittest
@@ -629,25 +629,28 @@ unittest
629629
assert(a.length == 0 && a.ptr is null);
630630
a = alloc.makeArray!int(5);
631631
assert(a.length == 5);
632-
assert(a == [ 0, 0, 0, 0, 0]);
632+
static immutable cheatsheet = [0, 0, 0, 0, 0];
633+
assert(a == cheatsheet);
633634
}
634635

635636
void test2(A)(auto ref A alloc)
636637
{
637638
static struct S { int x = 42; @disable this(this); }
638639
S[] arr = alloc.makeArray!S(5);
639640
assert(arr.length == 5);
640-
assert((cast(int*)arr.ptr)[0 .. 5] == [ 42, 42, 42, 42, 42]);
641+
int[] arrInt = () @trusted { return (cast(int*) arr.ptr)[0 .. 5]; }();
642+
static immutable res = [42, 42, 42, 42, 42];
643+
assert(arrInt == res);
641644
}
642645

643646
import std.experimental.allocator.gc_allocator : GCAllocator;
644-
test1(GCAllocator.instance);
645-
test1(theAllocator);
646-
test2(GCAllocator.instance);
647+
import std.experimental.allocator.mallocator : Mallocator;
648+
(alloc) /*pure nothrow*/ @safe { test1(alloc); test2(alloc);} (GCAllocator.instance);
649+
(alloc) nothrow @safe @nogc { test1(alloc); test2(alloc);} (Mallocator.instance);
647650
test2(theAllocator);
648651
}
649652

650-
unittest
653+
@system unittest
651654
{
652655
import std.algorithm.comparison : equal;
653656
auto a = theAllocator.makeArray!(shared int)(5);

0 commit comments

Comments
 (0)