|
1 | | -type CppVar[T] = distinct ptr T |
| 1 | +# stew |
| 2 | +# Copyright 2026 Status Research & Development GmbH |
| 3 | +# Licensed under either of |
| 4 | +# |
| 5 | +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 6 | +# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 7 | +# |
| 8 | +# at your option. This file may not be copied, modified, or distributed except according to those terms. |
2 | 9 |
|
3 | | -iterator evalTemplateParamOnceImpl[T](x: T): lent T = |
4 | | - yield x |
| 10 | +import ./lentutils |
5 | 11 |
|
6 | | -when defined(cpp): |
7 | | - # TODO `nim cpp` miscompiles iterators returning `var`, |
8 | | - # so we need to emulate them in terms of pointers: |
9 | | - iterator evalTemplateParamOnceImpl[T](x: var T): CppVar[T] = |
10 | | - yield CppVar[T](addr(x)) |
| 12 | +iterator evalTemplateParamOnceImpl[T](x: T): maybeLent T = |
| 13 | + yield x |
11 | 14 |
|
12 | | - template stripCppVar[T](p: CppVar[T]): var T = |
13 | | - ((ptr T)(p))[] |
14 | | -else: |
15 | | - iterator evalTemplateParamOnceImpl[T](x: var T): var T = |
16 | | - yield x |
| 15 | +iterator evalTemplateParamOnceImpl[T](x: var T): var T = |
| 16 | + yield x |
17 | 17 |
|
18 | 18 | template evalTemplateParamOnce*(templateParam, newName, blk: untyped) = |
19 | 19 | ## This can be used in templates to avoid the problem of multiple |
@@ -43,11 +43,5 @@ template evalTemplateParamOnce*(templateParam, newName, blk: untyped) = |
43 | 43 | ## Both limitations will be lifted in a future implementation based on |
44 | 44 | ## view types. |
45 | 45 | block: |
46 | | - for paramAddr in evalTemplateParamOnceImpl(templateParam): |
47 | | - template newName: auto = |
48 | | - when paramAddr is CppVar: |
49 | | - stripCppVar(paramAddr) |
50 | | - else: |
51 | | - paramAddr |
52 | | - |
| 46 | + for newName in evalTemplateParamOnceImpl(templateParam): |
53 | 47 | blk |
0 commit comments