|
10 | 10 | import std/isolation, atomics |
11 | 11 | from typetraits import supportsCopyMem |
12 | 12 |
|
13 | | -template checkNotNil(p, msg: typed) = |
| 13 | +proc raiseNilAccess() {.noinline.} = |
| 14 | + raise newException(NilAccessDefect, "dereferencing nil smart pointer") |
| 15 | + |
| 16 | +template checkNotNil(p: typed) = |
14 | 17 | when compileOption("boundChecks"): |
15 | 18 | {.line.}: |
16 | 19 | if p.isNil: |
17 | | - raise newException(NilAccessDefect, msg) |
| 20 | + raiseNilAccess() |
18 | 21 |
|
19 | 22 | type |
20 | 23 | UniquePtr*[T] = object |
@@ -55,11 +58,11 @@ proc isNil*[T](p: UniquePtr[T]): bool {.inline.} = |
55 | 58 |
|
56 | 59 | proc `[]`*[T](p: UniquePtr[T]): var T {.inline.} = |
57 | 60 | ## Returns a mutable view of the internal value of `p`. |
58 | | - checkNotNil(p, "dereferencing nil unique pointer") |
| 61 | + checkNotNil(p) |
59 | 62 | p.val[] |
60 | 63 |
|
61 | 64 | proc `[]=`*[T](p: UniquePtr[T], val: sink Isolated[T]) {.inline.} = |
62 | | - checkNotNil(p, "dereferencing nil unique pointer") |
| 65 | + checkNotNil(p) |
63 | 66 | p.val[] = extract val |
64 | 67 |
|
65 | 68 | template `[]=`*[T](p: UniquePtr[T]; val: T) = |
@@ -114,11 +117,11 @@ proc isNil*[T](p: SharedPtr[T]): bool {.inline.} = |
114 | 117 | p.val == nil |
115 | 118 |
|
116 | 119 | proc `[]`*[T](p: SharedPtr[T]): var T {.inline.} = |
117 | | - checkNotNil(p, "dereferencing nil shared pointer") |
| 120 | + checkNotNil(p) |
118 | 121 | p.val.value |
119 | 122 |
|
120 | 123 | proc `[]=`*[T](p: SharedPtr[T], val: sink Isolated[T]) {.inline.} = |
121 | | - checkNotNil(p, "dereferencing nil shared pointer") |
| 124 | + checkNotNil(p) |
122 | 125 | p.val.value = extract val |
123 | 126 |
|
124 | 127 | template `[]=`*[T](p: SharedPtr[T]; val: T) = |
@@ -146,7 +149,7 @@ proc isNil*[T](p: ConstPtr[T]): bool {.inline.} = |
146 | 149 |
|
147 | 150 | proc `[]`*[T](p: ConstPtr[T]): lent T {.inline.} = |
148 | 151 | ## Returns an immutable view of the internal value of `p`. |
149 | | - checkNotNil(p, "dereferencing nil const pointer") |
| 152 | + checkNotNil(p) |
150 | 153 | SharedPtr[T](p).val.value |
151 | 154 |
|
152 | 155 | proc `[]=`*[T](p: ConstPtr[T], v: T) = {.error: "`ConstPtr` cannot be assigned.".} |
|
0 commit comments