From 991bdb8443560e4c722d5ea8dfecbc8600737ae1 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 11 Apr 2018 14:26:46 +0300 Subject: [PATCH] Moved toOpenArray above its usage --- ranges/typedranges.nim | 8 +++++--- tests/ttypedranges.nim | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ranges/typedranges.nim b/ranges/typedranges.nim index 9690615..9794ac2 100644 --- a/ranges/typedranges.nim +++ b/ranges/typedranges.nim @@ -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) diff --git a/tests/ttypedranges.nim b/tests/ttypedranges.nim index 2a22938..b3b07b9 100644 --- a/tests/ttypedranges.nim +++ b/tests/ttypedranges.nim @@ -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]