Skip to content

Commit 39ccb3f

Browse files
authored
Merge pull request #20 from timotheecour/pr_toUncheckedArray
Added module `pointers` containing `toUncheckedArray`
2 parents 319aac4 + afc19b1 commit 39ccb3f

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Avoids changelog conflicts by assuming additions-only, which is by far the common case.
2+
# In the rare case where branch b1 rebases against branch b2 and both branches
3+
# modified the same changelog entry, you'll end up with that changelog entry
4+
# duplicated, which is easily identifiable and fixable.
5+
/changelog.md merge=union
6+

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)