summary refs log tree commit diff stats
path: root/lib/system.nim
diff options
context:
space:
mode:
authorcooldome <cdome@bk.ru>2020-02-18 18:43:26 +0000
committerGitHub <noreply@github.com>2020-02-18 19:43:26 +0100
commit046bb0b881b1e4917fd0ff230f9d716d31f1f20c (patch)
tree48404590b8e3eedaf1dbfa4275809b7807bce7b4 /lib/system.nim
parent8f73753a2ddd65c4016510d8472cf63d614ee0c1 (diff)
downloadNim-046bb0b881b1e4917fd0ff230f9d716d31f1f20c.tar.gz
stdlib/system: add sink and move (#13283)
Diffstat (limited to 'lib/system.nim')
-rw-r--r--lib/system.nim136
1 files changed, 92 insertions, 44 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 92470fbce..433829c3c 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -406,7 +406,7 @@ when defined(nimArrIdx):
   proc `[]`*[I: Ordinal;T](a: T; i: I): T {.
     noSideEffect, magic: "ArrGet".}
   proc `[]=`*[I: Ordinal;T,S](a: T; i: I;
-    x: S) {.noSideEffect, magic: "ArrPut".}
+    x: sink S) {.noSideEffect, magic: "ArrPut".}
   proc `=`*[T](dest: var T; src: T) {.noSideEffect, magic: "Asgn".}
 
   proc arrGet[I: Ordinal;T](a: T; i: I): T {.
@@ -427,7 +427,7 @@ type
     b*: U                  ## The upper bound (inclusive).
   Slice*[T] = HSlice[T, T] ## An alias for ``HSlice[T, T]``.
 
-proc `..`*[T, U](a: T, b: U): HSlice[T, U] {.noSideEffect, inline, magic: "DotDot".} =
+proc `..`*[T, U](a: sink T, b: sink U): HSlice[T, U] {.noSideEffect, inline, magic: "DotDot".} =
   ## Binary `slice`:idx: operator that constructs an interval ``[a, b]``, both `a`
   ## and `b` are inclusive.
   ##
@@ -439,7 +439,7 @@ proc `..`*[T, U](a: T, b: U): HSlice[T, U] {.noSideEffect, inline, magic: "DotDo
   ##   echo a[2 .. 3] # @[30, 40]
   result = HSlice[T, U](a: a, b: b)
 
-proc `..`*[T](b: T): HSlice[int, T] {.noSideEffect, inline, magic: "DotDot".} =
+proc `..`*[T](b: sink T): HSlice[int, T] {.noSideEffect, inline, magic: "DotDot".} =
   ## Unary `slice`:idx: operator that constructs an interval ``[default(int), b]``.
   ##
   ## .. code-block:: Nim
@@ -1242,7 +1242,7 @@ proc delete*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
     else:
       defaultImpl()
 
-proc insert*[T](x: var seq[T], item: T, i = 0.Natural) {.noSideEffect.} =
+proc insert*[T](x: var seq[T], item: sink T, i = 0.Natural) {.noSideEffect.} =
   ## Inserts `item` into `x` at position `i`.
   ##
   ## .. code-block:: Nim
@@ -1525,48 +1525,96 @@ proc `@`*[T](a: openArray[T]): seq[T] =
   newSeq(result, a.len)
   for i in 0..a.len-1: result[i] = a[i]
 
-proc `&`*[T](x, y: seq[T]): seq[T] {.noSideEffect.} =
-  ## Concatenates two sequences.
-  ##
-  ## Requires copying of the sequences.
-  ##
-  ## See also:
-  ## * `add(var seq[T], openArray[T]) <#add,seq[T][T],openArray[T]>`_
-  ##
-  ## .. code-block:: Nim
-  ##   assert(@[1, 2, 3, 4] & @[5, 6] == @[1, 2, 3, 4, 5, 6])
-  newSeq(result, x.len + y.len)
-  for i in 0..x.len-1:
-    result[i] = x[i]
-  for i in 0..y.len-1:
-    result[i+x.len] = y[i]
 
-proc `&`*[T](x: seq[T], y: T): seq[T] {.noSideEffect.} =
-  ## Appends element y to the end of the sequence.
-  ##
-  ## Requires copying of the sequence.
-  ##
-  ## See also:
-  ## * `add(var seq[T], T) <#add,seq[T][T],T>`_
-  ##
-  ## .. code-block:: Nim
-  ##   assert(@[1, 2, 3] & 4 == @[1, 2, 3, 4])
-  newSeq(result, x.len + 1)
-  for i in 0..x.len-1:
-    result[i] = x[i]
-  result[x.len] = y
+when defined(nimSeqsV2):
 
-proc `&`*[T](x: T, y: seq[T]): seq[T] {.noSideEffect.} =
-  ## Prepends the element x to the beginning of the sequence.
-  ##
-  ## Requires copying of the sequence.
-  ##
-  ## .. code-block:: Nim
-  ##   assert(1 & @[2, 3, 4] == @[1, 2, 3, 4])
-  newSeq(result, y.len + 1)
-  result[0] = x
-  for i in 0..y.len-1:
-    result[i+1] = y[i]
+  proc `&`*[T](x, y: sink seq[T]): seq[T] {.noSideEffect.} =
+    ## Concatenates two sequences.
+    ##
+    ## Requires copying of the sequences.
+    ##
+    ## See also:
+    ## * `add(var seq[T], openArray[T]) <#add,seq[T][T],openArray[T]>`_
+    ##
+    ## .. code-block:: Nim
+    ##   assert(@[1, 2, 3, 4] & @[5, 6] == @[1, 2, 3, 4, 5, 6])
+    newSeq(result, x.len + y.len)
+    for i in 0..x.len-1:
+      result[i] = move(x[i])
+    for i in 0..y.len-1:
+      result[i+x.len] = move(y[i])
+
+  proc `&`*[T](x: sink seq[T], y: sink T): seq[T] {.noSideEffect.} =
+    ## Appends element y to the end of the sequence.
+    ##
+    ## Requires copying of the sequence.
+    ##
+    ## See also:
+    ## * `add(var seq[T], T) <#add,seq[T][T],T>`_
+    ##
+    ## .. code-block:: Nim
+    ##   assert(@[1, 2, 3] & 4 == @[1, 2, 3, 4])
+    newSeq(result, x.len + 1)
+    for i in 0..x.len-1:
+      result[i] = move(x[i])
+    result[x.len] = move(y)
+
+  proc `&`*[T](x: sink T, y: sink seq[T]): seq[T] {.noSideEffect.} =
+    ## Prepends the element x to the beginning of the sequence.
+    ##
+    ## Requires copying of the sequence.
+    ##
+    ## .. code-block:: Nim
+    ##   assert(1 & @[2, 3, 4] == @[1, 2, 3, 4])
+    newSeq(result, y.len + 1)
+    result[0] = move(x)
+    for i in 0..y.len-1:
+      result[i+1] = move(y[i])
+
+else:
+
+  proc `&`*[T](x, y: seq[T]): seq[T] {.noSideEffect.} =
+    ## Concatenates two sequences.
+    ##
+    ## Requires copying of the sequences.
+    ##
+    ## See also:
+    ## * `add(var seq[T], openArray[T]) <#add,seq[T][T],openArray[T]>`_
+    ##
+    ## .. code-block:: Nim
+    ##   assert(@[1, 2, 3, 4] & @[5, 6] == @[1, 2, 3, 4, 5, 6])
+    newSeq(result, x.len + y.len)
+    for i in 0..x.len-1:
+      result[i] = x[i]
+    for i in 0..y.len-1:
+      result[i+x.len] = y[i]
+
+  proc `&`*[T](x: seq[T], y: T): seq[T] {.noSideEffect.} =
+    ## Appends element y to the end of the sequence.
+    ##
+    ## Requires copying of the sequence.
+    ##
+    ## See also:
+    ## * `add(var seq[T], T) <#add,seq[T][T],T>`_
+    ##
+    ## .. code-block:: Nim
+    ##   assert(@[1, 2, 3] & 4 == @[1, 2, 3, 4])
+    newSeq(result, x.len + 1)
+    for i in 0..x.len-1:
+      result[i] = x[i]
+    result[x.len] = y
+
+  proc `&`*[T](x: T, y: seq[T]): seq[T] {.noSideEffect.} =
+    ## Prepends the element x to the beginning of the sequence.
+    ##
+    ## Requires copying of the sequence.
+    ##
+    ## .. code-block:: Nim
+    ##   assert(1 & @[2, 3, 4] == @[1, 2, 3, 4])
+    newSeq(result, y.len + 1)
+    result[0] = x
+    for i in 0..y.len-1:
+      result[i+1] = y[i]
 
 
 proc astToStr*[T](x: T): string {.magic: "AstToStr", noSideEffect.}