Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions std/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ if (isIterable!Range && !isNarrowString!Range)
{
if(r.length == 0) return null;

auto result = uninitializedArray!(Unqual!(E)[])(r.length);
auto result = ()@trusted{ return uninitializedArray!(Unqual!E[])(r.length); }();

size_t i = 0;
foreach (e; r)
{
// hacky
static if (is(typeof(e.opAssign(e))))
static if (is(typeof(result[i].opAssign(e))) ||
!is(typeof(result[i] = e)))
{
// this should be in-place construction
emplace!E(result.ptr + i, e);
emplace(result.ptr + i, e);
}
else
{
Expand All @@ -72,6 +73,40 @@ if (isIterable!Range && !isNarrowString!Range)
}
}

@safe pure nothrow unittest
{
auto a = array([1, 2, 3, 4, 5][]);
assert(a == [ 1, 2, 3, 4, 5 ]);
}

@safe pure nothrow unittest
{
struct Foo
{
int a;
}
auto a = array([Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)][]);
assert(equal(a, [Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)]));
}

@system unittest
{
struct Foo
{
int a;
auto opAssign(Foo foo)
{
a = foo.a;
}
auto opEquals(Foo foo)
{
return a == foo.a;
}
}
auto a = array([Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)][]);
assert(equal(a, [Foo(1), Foo(2), Foo(3), Foo(4), Foo(5)]));
}

/**
Convert a narrow string to an array type that fully supports random access.
This is handled as a special case and always returns a $(D dchar[]),
Expand Down