Moved toOpenArray above its usage

This commit is contained in:
Yuriy Glukhov 2018-04-11 14:26:46 +03:00
parent 86037c1d93
commit 991bdb8443
2 changed files with 11 additions and 3 deletions

View File

@ -132,10 +132,12 @@ proc `[]=`*[T, U, V](r: MutRange[T], s: HSlice[U, V], v: openarray[T]) =
else:
raise newException(RangeError, "different lengths for slice assignment")
template toOpenArray*[T](r: Range[T]): auto =
# TODO: Casting through an {.unchecked.} array would be more appropriate
# here, but currently this results in internal compiler error.
toOpenArray(cast[ptr array[10000000, T]](r.start)[], 0, r.high)
proc `[]=`*[T, U, V](r: MutRange[T], s: HSlice[U, V], v: Range[T]) {.inline.} =
r[s] = toOpenArray(v)
proc baseAddr*[T](r: Range[T]): ptr T {.inline.} = r.start
template toOpenArray*[T](r: Range[T]): auto =
toOpenArray(cast[ptr array[10000000, T]](r.start)[], 0, r.high)

View File

@ -13,3 +13,9 @@ suite "Typed ranges":
var s = newSeq[int]()
for a in r: s.add(a)
check s == @[1, 2, 3, 4, 5]
test "subrange":
var a = newRange[int](5)
let b = toRange(@[1, 2, 3])
a[1 .. 3] = b
check a.toSeq == @[0, 1, 2, 3, 0]