Skip to content

Commit 7be6505

Browse files
committed
RCString with Nullable
1 parent 5abf1ee commit 7be6505

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

source/bc/string/string.d

+15-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module bc.string.string;
55

66
import bc.core.intrinsics;
77
import bc.core.memory : enforceMalloc, enforceRealloc, heapAlloc, heapDealloc;
8-
import core.atomic : atomicOp;
98
import std.range : ElementEncodingType, hasLength, isInputRange;
109
import std.traits : ForeachType, isSomeChar, isSomeString, isStaticArray, Unqual;
1110
// debug import core.stdc.stdio;
@@ -256,7 +255,7 @@ private struct StringImpl(C, RC rc, Zero zero)
256255
{
257256
struct Payload
258257
{
259-
shared size_t refs;
258+
size_t refs;
260259
size_t len;
261260
C[] buf;
262261

@@ -271,19 +270,16 @@ private struct StringImpl(C, RC rc, Zero zero)
271270
}
272271

273272
/// Copy constructor
274-
this(ref return scope StringImpl rhs) pure @safe
273+
this(ref return scope inout StringImpl rhs) pure @safe inout
275274
{
276-
if (rhs.pay)
277-
{
278-
this.pay = rhs.pay;
279-
atomicOp!"+="(this.pay.refs, 1);
280-
}
275+
pay = rhs.pay;
276+
if (pay) () @trusted { (cast(Payload*)pay).refs++; }();
281277
}
282278

283279
/// Destructor
284280
~this()
285281
{
286-
if (pay && atomicOp!"-="(pay.refs, 1) == 0) heapDealloc(pay);
282+
if (pay && --pay.refs == 0) heapDealloc(pay);
287283
}
288284
}
289285
else
@@ -678,6 +674,16 @@ auto rcString(C = char, S)(auto ref S str)
678674
}
679675
}
680676

677+
version (D_Exceptions)
678+
{
679+
@("RCString with Nullable")
680+
@nogc @safe unittest
681+
{
682+
import std.typecons : Nullable;
683+
Nullable!RCString sn = RCString("foo");
684+
}
685+
}
686+
681687
@("rcString")
682688
@nogc @safe unittest
683689
{

0 commit comments

Comments
 (0)