about summary refs log tree commit diff
path: root/src/rt
diff options
context:
space:
mode:
Diffstat (limited to 'src/rt')
-rw-r--r--src/rt/rust_internal.h21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/rt/rust_internal.h b/src/rt/rust_internal.h
index d5c4f459f1c..9d7d714064f 100644
--- a/src/rt/rust_internal.h
+++ b/src/rt/rust_internal.h
@@ -97,19 +97,18 @@ static intptr_t const CONST_REFCOUNT = 0x7badface;
 static size_t const BUF_BYTES = 2048;
 
 // Every reference counted object should derive from this base class.
+// Or use this macro. The macro is preferred as the base class will be
+// disappearing.
 
-template <typename T> struct rc_base {
-    intptr_t ref_count;
-
-    void ref() {
-        ++ref_count;
-    }
+#define RUST_REFCOUNTED(T) \
+  RUST_REFCOUNTED_WITH_DTOR(T, delete (T*)this)
+#define RUST_REFCOUNTED_WITH_DTOR(T, dtor) \
+  intptr_t ref_count;      \
+  void ref() { ++ref_count; } \
+  void deref() { if (--ref_count == 0) { dtor; } }
 
-    void deref() {
-        if (--ref_count == 0) {
-            delete (T*)this;
-        }
-    }
+template <typename T> struct rc_base {
+    RUST_REFCOUNTED(T)
 
     rc_base();
     ~rc_base();