Skip to content

Commit db77aa5

Browse files
committed
Added module pointers containing toUncheckedArray
1 parent 319aac4 commit db77aa5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## additions and changes
2+
3+
- Added module `pointers` containing `toUncheckedArray`

src/fusion/pointers.nim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
##[
2+
Convenience procs to deal with pointer-like variables.
3+
]##
4+
5+
proc toUncheckedArray*[T](a: ptr T): ptr UncheckedArray[T] {.inline.} =
6+
## Shortcut for `cast[ptr UncheckedArray[T]](a)`, where T is inferred.
7+
## This allows array indexing operations on `a`.
8+
## This is unsafe as it returns `UncheckedArray`.
9+
runnableExamples:
10+
var a = @[10, 11, 12]
11+
let pa = a[1].addr.toUncheckedArray
12+
doAssert pa[-1] == 10
13+
pa[0] = 100
14+
doAssert a == @[10, 100, 12]
15+
pa[0] += 5
16+
doAssert a[1] == 105
17+
cast[ptr UncheckedArray[T]](a)

0 commit comments

Comments
 (0)