Skip to content

Commit 2a6cb95

Browse files
committed
improve rcarray
1 parent f94fd01 commit 2a6cb95

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

source/mir/rcarray.d

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct mir_rcarray(T)
7878
return *cast(inout(Context*)*)&_payload;
7979
}
8080

81-
private void dec()() scope
81+
private void dec()() scope nothrow @nogc @safe
8282
{
8383
import core.atomic: atomicOp;
8484
if (_context !is null) with(*_context)
@@ -136,7 +136,7 @@ struct mir_rcarray(T)
136136
}
137137

138138
///
139-
this(ref typeof(this) rhs) @safe pure nothrow @nogc
139+
this(ref typeof(this) rhs) pure nothrow @nogc
140140
{
141141
this._context = rhs._context;
142142
this.__xpostblit;
@@ -207,7 +207,7 @@ struct mir_rcarray(T)
207207
+/
208208
static typeof(this) create(V[] values...) @safe @nogc
209209
{
210-
auto ret = typeof(this)(values.length, T.alignof, true, hasElaborateAssign!T);
210+
auto ret = typeof(this)(values.length, T.alignof, true, hasElaborateDestructor!T);
211211
static if (!hasElaborateAssign!T)
212212
{
213213
()@trusted {
@@ -220,19 +220,18 @@ struct mir_rcarray(T)
220220
import mir.conv: emplaceRef;
221221
auto lhs = ret[];
222222
foreach (i, ref e; values)
223-
lhs[i] = e;
223+
lhs[i].emplaceRef(e);
224224
}
225-
import std.algorithm.mutation: move;
226-
return ret.move;
225+
return ret;
227226
}
228227

229228
static if (!hasIndirections!T)
230229
/++
231230
Contructor is defined if `hasIndirections!T == false`.
232231
+/
233-
static typeof(this) create(scope V[] values...) @safe @nogc
232+
static typeof(this) create(scope V[] values...) @nogc
234233
{
235-
auto ret = typeof(this)(values.length, T.alignof, true, hasElaborateAssign!T);
234+
auto ret = typeof(this)(values.length, T.alignof, true, hasElaborateDestructor!T);
236235
static if (!hasElaborateAssign!T)
237236
{
238237
()@trusted {
@@ -245,10 +244,9 @@ struct mir_rcarray(T)
245244
import mir.conv: emplaceRef;
246245
auto lhs = ret[];
247246
foreach (i, ref e; values)
248-
lhs[i] = e;
247+
lhs[i].emplaceRef(e);
249248
}
250-
import std.algorithm.mutation: move;
251-
return ret.move;
249+
return ret;
252250
}
253251

254252
private bool initializeImpl()(size_t length, uint alignment, bool deallocate, bool initialize) scope @trusted nothrow @nogc
@@ -287,7 +285,8 @@ struct mir_rcarray(T)
287285

288286
_context.length = length;
289287
_context.counter = deallocate; // 0
290-
if (initialize ||hasElaborateAssign!T)
288+
// hasElaborateDestructor is required for safe destruction in case of exceptions
289+
if (initialize || hasElaborateDestructor!T)
291290
{
292291
import mir.conv: uninitializedFillDefault;
293292
import std.traits: Unqual;
@@ -557,3 +556,38 @@ unittest
557556
return d;
558557
}
559558
}
559+
560+
version(mir_test)
561+
@safe unittest
562+
{
563+
import core.stdc.stdio;
564+
565+
struct S
566+
{
567+
uint s;
568+
this(this) @nogc nothrow @safe
569+
{
570+
() @trusted {
571+
// puts("this(this)\n");
572+
} ();
573+
}
574+
575+
~this() nothrow @nogc @safe
576+
{
577+
() @trusted {
578+
// if (s)
579+
// puts("~this()\n");
580+
// else
581+
// puts("~this() - zero\n");
582+
} ();
583+
}
584+
}
585+
586+
struct C
587+
{
588+
S s;
589+
}
590+
591+
S[1] d = [S(1)];
592+
auto r = RCArray!S.create(d[]);
593+
}

0 commit comments

Comments
 (0)