diff --git a/changelog.md b/changelog.md index 3144c7cf54a46..0cb48942b9dc7 100644 --- a/changelog.md +++ b/changelog.md @@ -212,6 +212,7 @@ - Added `progressInterval` argument to `asyncftpclient.newAsyncFtpClient` to control the interval at which progress callbacks are called. +- Added module `pointers` containing `toUncheckedArray` ## Language changes diff --git a/lib/std/pointers.nim b/lib/std/pointers.nim new file mode 100644 index 0000000000000..4c92f74b7d146 --- /dev/null +++ b/lib/std/pointers.nim @@ -0,0 +1,17 @@ +##[ +Convenience procs to deal with pointer-like variables. +]## + +proc toUncheckedArray*[T](a: ptr T): ptr UncheckedArray[T] {.inline.} = + ## Shortcut for `cast[ptr UncheckedArray[T]](a)`, where T is inferred. + ## This allows array indexing operations on `a`. + ## This is unsafe as it returns `UncheckedArray`. + runnableExamples: + var a = @[10, 11, 12] + let pa = a[1].addr.toUncheckedArray + doAssert pa[-1] == 10 + pa[0] = 100 + doAssert a == @[10, 100, 12] + pa[0] += 5 + doAssert a[1] == 105 + cast[ptr UncheckedArray[T]](a)