Skip to content

Commit a748661

Browse files
Cleanup Rc and Arc APIs
1 parent 4f5406d commit a748661

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

source/numem/rc.d

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ public:
2929
this(T value) @trusted {
3030
this.rc = nogc_new!__rc(1, value);
3131
}
32+
33+
/**
34+
Retains a reference to the stored value.
35+
*/
36+
auto ref retain()() @trusted {
37+
this.__retain();
38+
return this;
39+
}
40+
41+
/**
42+
Releases a reference to the stored value.
43+
This invalidates the Rc instance.
44+
*/
45+
auto ref release()() @trusted {
46+
this.__release();
47+
return this;
48+
}
3249
}
3350

3451
/**
@@ -41,7 +58,7 @@ public:
4158

4259
/// Destructor
4360
~this() {
44-
this.release();
61+
this.__release();
4562
}
4663

4764
/**
@@ -59,6 +76,7 @@ public:
5976
*/
6077
this(ref return scope inout(typeof(this)) rhs) inout @trusted {
6178
this.rc = rhs.rc;
79+
(cast(Arc!T)this).__retain();
6280
}
6381
}
6482

@@ -74,29 +92,16 @@ private:
7492
}
7593

7694
__rc* rc;
77-
public:
78-
alias value this;
79-
80-
/**
81-
The value stored in the rc type.
82-
*/
83-
@property T value() @safe => rc ? rc.value : T.init;
8495

85-
/**
86-
Retains a reference to the stored value.
87-
*/
88-
auto ref retain()() @trusted {
96+
pragma(inline, true)
97+
void __retain() @trusted nothrow {
8998
if (rc) {
9099
nu_atomic_add_32(rc.refs, 1);
91100
}
92-
return this;
93101
}
94102

95-
/**
96-
Releases a reference to the stored value.
97-
This invalidates the Rc instance.
98-
*/
99-
auto ref release()() @trusted {
103+
pragma(inline, true)
104+
void __release() @trusted nothrow {
100105
if (rc) {
101106
if (nu_atomic_sub_32(rc.refs, 1) == 1) {
102107
nogc_delete(rc.value);
@@ -105,4 +110,12 @@ public:
105110
}
106111
this.rc = null;
107112
}
113+
114+
public:
115+
alias value this;
116+
117+
/**
118+
The value stored in the rc type.
119+
*/
120+
@property T value() @safe => rc ? rc.value : T.init;
108121
}

tests/ut/rc.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module tests.ut.rc;
22
import numem.rc;
3+
import numem.lifetime : nogc_delete;
34

45
@("Rc!int")
56
unittest {
@@ -15,6 +16,6 @@ unittest {
1516
Arc!int a = 24;
1617
assert(a == 24);
1718

18-
a.release();
19+
nogc_delete(a);
1920
assert(a == 0);
2021
}

0 commit comments

Comments
 (0)