9
9
*/
10
10
module core.internal.array.construction ;
11
11
12
+ import core.internal.traits : Unqual;
13
+
12
14
/**
13
15
* Does array initialization (not assignment) from another array of the same element type.
14
16
* Params:
@@ -21,7 +23,8 @@ module core.internal.array.construction;
21
23
* purity, and throwabilty checks. To prevent breaking existing code, this function template
22
24
* is temporarily declared `@trusted` until the implementation can be brought up to modern D expectations.
23
25
*/
24
- Tarr _d_arrayctor (Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
26
+ Tarr1 _d_arrayctor (Tarr1 : T1 [], Tarr2 : T2 [], T1 , T2 )(scope Tarr2 from, size_t length) @trusted
27
+ if (is (Unqual! T1 == Unqual! T2 ))
25
28
{
26
29
pragma (inline, false );
27
30
import core.internal.traits : hasElaborateCopyConstructor, Unqual;
@@ -30,7 +33,15 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
30
33
import core.stdc.stdint : uintptr_t ;
31
34
debug (PRINTF ) import core.stdc.stdio : printf;
32
35
33
- debug (PRINTF ) printf(" _d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n " , from.ptr, from.length, to.ptr, to.length, T.tsize);
36
+ debug (PRINTF ) printf(" _d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n " , from.ptr, from.length, to.ptr, to.length, T1 .tsize);
37
+
38
+ Tarr1 to;
39
+ // Check if Tarr1 is a dynamic array.
40
+ // Otherwise, to.length is already set.
41
+ static if (__traits(compiles, {to.length = length;}))
42
+ {
43
+ to.length = length;
44
+ }
34
45
35
46
void [] vFrom = (cast (void * )from.ptr)[0 .. from.length];
36
47
void [] vTo = (cast (void * )to.ptr)[0 .. to.length];
@@ -46,13 +57,11 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
46
57
(cast (Type)&enforceRawArraysConformableNogc)(action, elementSize, a1, a2, false );
47
58
}
48
59
49
- enforceRawArraysConformable(" initialization" , T .sizeof, vFrom, vTo);
60
+ enforceRawArraysConformable(" initialization" , T1 .sizeof, vFrom, vTo);
50
61
51
- static if (hasElaborateCopyConstructor! T )
62
+ static if (hasElaborateCopyConstructor! T1 )
52
63
{
53
- // Use uint instead of size_t as a temporary workaround until this bug is fixed:
54
- // https://issues.dlang.org/show_bug.cgi?id=22372
55
- uint i;
64
+ size_t i;
56
65
try
57
66
{
58
67
for (i = 0 ; i < to.length; i++ )
@@ -64,7 +73,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
64
73
*/
65
74
while (i-- )
66
75
{
67
- auto elem = cast (Unqual! T * )&to[i];
76
+ auto elem = cast (Unqual! T1 * )&to[i];
68
77
destroy (* elem);
69
78
}
70
79
@@ -74,7 +83,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
74
83
else
75
84
{
76
85
// blit all elements at once
77
- memcpy(cast (void * ) to.ptr, from.ptr, to.length * T .sizeof);
86
+ memcpy(cast (void * ) to.ptr, from.ptr, to.length * T1 .sizeof);
78
87
}
79
88
80
89
return to;
@@ -92,7 +101,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
92
101
93
102
S[4 ] arr1;
94
103
S[4 ] arr2 = [S(0 ), S(1 ), S(2 ), S(3 )];
95
- _d_arrayctor(arr1[], arr2[]);
104
+ arr1 = _d_arrayctor! (S[ 4 ])( arr2[], arr1.length );
96
105
97
106
assert (counter == 4 );
98
107
assert (arr1 == arr2);
@@ -115,7 +124,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
115
124
116
125
S[4 ] arr1;
117
126
S[4 ] arr2 = [S(0 ), S(1 ), S(2 ), S(3 )];
118
- _d_arrayctor(arr1[], arr2[]);
127
+ arr1 = _d_arrayctor! (S[ 4 ])( arr2[], arr1.length );
119
128
120
129
assert (counter == 4 );
121
130
assert (arr1 == arr2);
@@ -141,7 +150,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
141
150
{
142
151
Throw[4 ] a;
143
152
Throw[4 ] b = [Throw(1 ), Throw(2 ), Throw(3 ), Throw(4 )];
144
- _d_arrayctor(a[], b[]);
153
+ a = _d_arrayctor! (Throw[ 4 ])( b[], a.length );
145
154
}
146
155
catch (Exception )
147
156
{
@@ -166,7 +175,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
166
175
{
167
176
NoThrow[4 ] a;
168
177
NoThrow[4 ] b = [NoThrow(1 ), NoThrow(2 ), NoThrow(3 ), NoThrow(4 )];
169
- _d_arrayctor(a[], b[]);
178
+ a = _d_arrayctor! (NoThrow[ 4 ])( b[], a.length );
170
179
}
171
180
catch (Exception )
172
181
{
@@ -190,7 +199,6 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
190
199
void _d_arraysetctor (Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
191
200
{
192
201
pragma (inline, false );
193
- import core.internal.traits : Unqual;
194
202
import core.lifetime : copyEmplace;
195
203
196
204
size_t i;
@@ -273,7 +281,7 @@ void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
273
281
{
274
282
Throw[4 ] a;
275
283
Throw[4 ] b = [Throw(1 ), Throw(2 ), Throw(3 ), Throw(4 )];
276
- _d_arrayctor(a[], b[]);
284
+ a = _d_arrayctor! (Throw[ 4 ])( b[], a.length );
277
285
}
278
286
catch (Exception )
279
287
{
0 commit comments