summary refs log tree commit diff stats
path: root/lib/core
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-07-27 18:20:13 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-07-27 18:20:13 +0200
commitef4b755183f6564cc0f35cdf01794626c4e5fe2f (patch)
tree1ea46f6bf9dc4d38067abae043907dcfbadd2ebc /lib/core
parent4ec91a30c4ab4bdb45f9805168f124205235d19c (diff)
downloadNim-ef4b755183f6564cc0f35cdf01794626c4e5fe2f.tar.gz
allows a destructor to be attached to a tyString/tySequence
Diffstat (limited to 'lib/core')
-rw-r--r--lib/core/seqs.nim12
-rw-r--r--lib/core/strs.nim17
2 files changed, 20 insertions, 9 deletions
diff --git a/lib/core/seqs.nim b/lib/core/seqs.nim
index 4327d2352..4dcf6cbbb 100644
--- a/lib/core/seqs.nim
+++ b/lib/core/seqs.nim
@@ -34,7 +34,8 @@ when false:
   proc `=trace`[T](s: NimSeqV2[T]) =
     for i in 0 ..< s.len: `=trace`(s.data[i])
 
-proc `=destroy`[T](x: var NimSeqV2[T]) =
+proc `=destroy`[T](s: var seq[T]) =
+  var x = cast[ptr NimSeqV2[T]](addr s)
   var p = x.p
   if p != nil:
     when not supportsCopyMem(T):
@@ -43,7 +44,10 @@ proc `=destroy`[T](x: var NimSeqV2[T]) =
     x.p = nil
     x.len = 0
 
-proc `=`[T](a: var NimSeqV2[T]; b: NimSeqV2[T]) =
+proc `=`[T](x: var seq[T]; y: seq[T]) =
+  var a = cast[ptr NimSeqV2[T]](addr x)
+  var b = cast[ptr NimSeqV2[T]](unsafeAddr y)
+
   if a.p == b.p: return
   `=destroy`(a)
   a.len = b.len
@@ -56,7 +60,9 @@ proc `=`[T](a: var NimSeqV2[T]; b: NimSeqV2[T]) =
       for i in 0..<a.len:
         a.p.data[i] = b.p.data[i]
 
-proc `=sink`[T](a: var NimSeqV2[T]; b: NimSeqV2[T]) =
+proc `=sink`[T](x: var seq[T]; y: seq[T]) =
+  var a = cast[ptr NimSeqV2[T]](addr x)
+  var b = cast[ptr NimSeqV2[T]](unsafeAddr y)
   if a.p != nil and a.p != b.p:
     `=destroy`(a)
   a.len = b.len
diff --git a/lib/core/strs.nim b/lib/core/strs.nim
index fdf3d6e42..562beaa91 100644
--- a/lib/core/strs.nim
+++ b/lib/core/strs.nim
@@ -45,22 +45,27 @@ template frees(s) =
   if not isLiteral(s):
     s.p.region.dealloc(s.p.region, s.p, contentSize(s.p.cap))
 
-proc `=destroy`(s: var NimStringV2) =
-  frees(s)
-  s.len = 0
-  s.p = nil
+proc `=destroy`(s: var string) =
+  var a = cast[ptr NimStringV2[T]](addr s)
+  frees(a)
+  a.len = 0
+  a.p = nil
 
 template lose(a) =
   frees(a)
 
-proc `=sink`(a: var NimStringV2, b: NimStringV2) =
+proc `=sink`(x: var string, y: string) =
+  var a = cast[ptr NimStringV2](addr x)
+  var b = cast[ptr NimStringV2](unsafeAddr y)
   # we hope this is optimized away for not yet alive objects:
   if unlikely(a.p == b.p): return
   lose(a)
   a.len = b.len
   a.p = b.p
 
-proc `=`(a: var NimStringV2; b: NimStringV2) =
+proc `=`(x: var string, y: string) =
+  var a = cast[ptr NimStringV2](addr x)
+  var b = cast[ptr NimStringV2](unsafeAddr y)
   if unlikely(a.p == b.p): return
   lose(a)
   a.len = b.len