-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fusion/pointers: add pointer arithmetic operators #21
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,37 @@ proc toUncheckedArray*[T](a: ptr T): ptr UncheckedArray[T] {.inline.} = | |
pa[0] += 5 | ||
doAssert a[1] == 105 | ||
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 | ||
doAssert (pa + 1)[] == 11 | ||
doAssert pa[2] == 12 | ||
pa[1] = 2 | ||
doAssert a[1] == 2 | ||
type T = typeof(p[]) # pending https://github.com/nim-lang/Nim/issues/13527 | ||
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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A single pointer is not an array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about this; slightly more verbose than # `[]` =>
template `at`*[T](p: ptr T, off: int): T = ...
# `[]=` =>
template `at=`*[T](p: ptr T, off: int, val: T) = ... it would still allow this: echo p.at(1)
p.at(2) = 2
p.at(2) -= 3 ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
p[1]
vs
p.toUncheckedArray[1] That's why it's been implemented in so many places (with more or less good implementations eg many implementations have issues eg are not safe wrt multiple template argument evaluation bugs) and requested also in many places as shown here #21 (comment) Among other things, it makes it in particular easy to adapt C/C++ code to nim with the simplest possible syntax. |
||
## Unsafe. | ||
(p + off)[] | ||
|
||
template `[]=`*[T](p: ptr T, off: int, val: T) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A single pointer is not an array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #21 (comment) |
||
## 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import fusion/pointers | ||
|
||
block: | ||
var a = @[10, 11, 12] | ||
let pa = a[0].addr | ||
let pb = pa + 1 | ||
doAssert pb[] == 11 | ||
doAssert (pb - 1)[] == 10 | ||
pa[] = 100 | ||
doAssert a[0] == 100 | ||
doAssert pa[1] == 11 | ||
|
||
var pc = pa | ||
pc += 1 | ||
doAssert pc[] == 11 | ||
doAssert pc[0] == 11 | ||
doAssert pc == pb | ||
pc -= 1 | ||
doAssert pc == pa |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my own low level code I never needed the
* sizeof(T)
part. It's not intuitive and probably error-prone. Why try to outsmart the programmer who chose to operate on a very low level for a reason?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #21 (comment)