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:
15
- * to = what array to initialize
16
17
* from = what data the array should be initialized with
17
18
* Returns:
18
- * The constructed `to`
19
+ * The created and initialized array `to`
19
20
* Bugs:
20
21
* This function template was ported from a much older runtime hook that bypassed safety,
21
22
* purity, and throwabilty checks. To prevent breaking existing code, this function template
22
23
* is temporarily declared `@trusted` until the implementation can be brought up to modern D expectations.
23
24
*/
24
- Tarr _d_arrayctor (Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
25
+ Tarr1 _d_arrayctor (Tarr1 : T1 [], Tarr2 : T2 [], T1 , T2 )(scope Tarr2 from) @trusted
26
+ if (is (Unqual! T1 == Unqual! T2 ))
25
27
{
26
28
pragma (inline, false );
27
- import core.internal.traits : hasElaborateCopyConstructor, Unqual ;
29
+ import core.internal.traits : hasElaborateCopyConstructor;
28
30
import core.lifetime : copyEmplace;
29
31
import core.stdc.string : memcpy;
30
32
import core.stdc.stdint : uintptr_t ;
31
33
debug (PRINTF ) import core.stdc.stdio : printf;
32
34
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);
35
+ debug (PRINTF ) printf(" _d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n " , from.ptr, from.length, to.ptr, to.length, T1 .tsize);
36
+
37
+ Tarr1 to = void ;
34
38
35
39
void [] vFrom = (cast (void * )from.ptr)[0 .. from.length];
36
40
void [] vTo = (cast (void * )to.ptr)[0 .. to.length];
@@ -46,13 +50,11 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
46
50
(cast (Type)&enforceRawArraysConformableNogc)(action, elementSize, a1, a2, false );
47
51
}
48
52
49
- enforceRawArraysConformable(" initialization" , T .sizeof, vFrom, vTo);
53
+ enforceRawArraysConformable(" initialization" , T1 .sizeof, vFrom, vTo);
50
54
51
- static if (hasElaborateCopyConstructor! T )
55
+ static if (hasElaborateCopyConstructor! T1 )
52
56
{
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;
57
+ size_t i;
56
58
try
57
59
{
58
60
for (i = 0 ; i < to.length; i++ )
@@ -64,7 +66,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
64
66
*/
65
67
while (i-- )
66
68
{
67
- auto elem = cast (Unqual! T * )&to[i];
69
+ auto elem = cast (Unqual! T1 * )&to[i];
68
70
destroy (* elem);
69
71
}
70
72
@@ -74,7 +76,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
74
76
else
75
77
{
76
78
// blit all elements at once
77
- memcpy(cast (void * ) to.ptr, from.ptr, to.length * T .sizeof);
79
+ memcpy(cast (void * ) to.ptr, from.ptr, to.length * T1 .sizeof);
78
80
}
79
81
80
82
return to;
@@ -92,7 +94,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
92
94
93
95
S[4 ] arr1;
94
96
S[4 ] arr2 = [S(0 ), S(1 ), S(2 ), S(3 )];
95
- _d_arrayctor( arr1[], arr2[]);
97
+ arr1 = _d_arrayctor! ( typeof ( arr1))( arr2[]);
96
98
97
99
assert (counter == 4 );
98
100
assert (arr1 == arr2);
@@ -115,7 +117,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
115
117
116
118
S[4 ] arr1;
117
119
S[4 ] arr2 = [S(0 ), S(1 ), S(2 ), S(3 )];
118
- _d_arrayctor( arr1[], arr2[]);
120
+ arr1 = _d_arrayctor! ( typeof ( arr1))( arr2[]);
119
121
120
122
assert (counter == 4 );
121
123
assert (arr1 == arr2);
@@ -141,7 +143,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
141
143
{
142
144
Throw[4 ] a;
143
145
Throw[4 ] b = [Throw(1 ), Throw(2 ), Throw(3 ), Throw(4 )];
144
- _d_arrayctor(a[], b[]);
146
+ a = _d_arrayctor! ( typeof (a))( b[]);
145
147
}
146
148
catch (Exception )
147
149
{
@@ -166,7 +168,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
166
168
{
167
169
NoThrow[4 ] a;
168
170
NoThrow[4 ] b = [NoThrow(1 ), NoThrow(2 ), NoThrow(3 ), NoThrow(4 )];
169
- _d_arrayctor(a[], b[]);
171
+ a = _d_arrayctor! ( typeof (a))( b[]);
170
172
}
171
173
catch (Exception )
172
174
{
@@ -190,7 +192,6 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
190
192
void _d_arraysetctor (Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
191
193
{
192
194
pragma (inline, false );
193
- import core.internal.traits : Unqual;
194
195
import core.lifetime : copyEmplace;
195
196
196
197
size_t i;
@@ -273,7 +274,7 @@ void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
273
274
{
274
275
Throw[4 ] a;
275
276
Throw[4 ] b = [Throw(1 ), Throw(2 ), Throw(3 ), Throw(4 )];
276
- _d_arrayctor(a[], b[]);
277
+ a = _d_arrayctor! ( typeof (a))( b[]);
277
278
}
278
279
catch (Exception )
279
280
{
0 commit comments