Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Oct 9, 2020
1 parent 2196b55 commit 946eb57
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/fusion/pointers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ proc toUncheckedArray*[T](a: ptr T): ptr UncheckedArray[T] {.inline.} =
cast[ptr UncheckedArray[T]](a)

template `+`*[T](p: ptr T, off: int): ptr T =
## Unsafe.
runnableExamples:
var a = @[10, 11, 12]
let pa = a[0].addr
Expand All @@ -28,18 +29,23 @@ template `+`*[T](p: ptr T, off: int): ptr T =
cast[ptr T](cast[ByteAddress](p) +% off * sizeof(T))

template `-`*[T](p: ptr T, off: int): ptr T =
## Unsafe.
type T = typeof(p[])
cast[ptr T](cast[ByteAddress](p) -% off * sizeof(T))

template `[]`*[T](p: ptr T, off: int): T =
## Unsafe.
(p + off)[]

template `[]=`*[T](p: ptr T, off: int, val: T) =
## Unsafe.
(p + off)[] = val

proc `+=`*[T](p: var ptr T, off: int) {.inline.} =
## Unsafe.
# not a template to avoid double evaluation issues
p = p + off

proc `-=`*[T](p: var ptr T, off: int) {.inline.} =
## Unsafe.
p = p - off

0 comments on commit 946eb57

Please sign in to comment.