Skip to content

Commit 5176720

Browse files
committed
Support tuples in shared vars
1 parent 090beff commit 5176720

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/necsus/util/nimNode.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ proc symbols*(node: NimNode): seq[string] =
77
of nnkCharLit..nnkUInt64Lit: return @[$node.intVal]
88
of nnkFloatLit..nnkFloat64Lit: return @[$node.floatVal]
99
of nnkNilLit: return @["nil"]
10-
of nnkBracketExpr, nnkTupleTy: return node.toSeq.mapIt(it.symbols).foldl(concat(a, b))
10+
of nnkBracketExpr, nnkTupleTy, nnkTupleConstr: return node.toSeq.mapIt(it.symbols).foldl(concat(a, b))
1111
of nnkIdentDefs: return concat(node[0].symbols, node[1].symbols)
1212
of nnkRefTy: return concat(@["ref"], node[0].symbols)
1313
else: error(&"Unable to generate a component symbol from node ({node.kind}): {node.repr}")
@@ -19,7 +19,8 @@ proc hash*(node: NimNode): Hash =
1919
of nnkCharLit..nnkUInt64Lit: return hash(node.intVal)
2020
of nnkFloatLit..nnkFloat64Lit: return hash(node.floatVal)
2121
of nnkNilLit, nnkEmpty: return hash(0)
22-
of nnkBracketExpr, nnkTupleTy, nnkIdentDefs: return node.toSeq.mapIt(hash(it)).foldl(a !& b, hash(node.kind))
22+
of nnkBracketExpr, nnkTupleTy, nnkIdentDefs, nnkTupleConstr:
23+
return node.toSeq.mapIt(hash(it)).foldl(a !& b, hash(node.kind))
2324
of nnkRefTy: return hash(node[0])
2425
else: error(&"Unable to generate a hash from node ({node.kind}): {node.repr}")
2526

tests/t_sharedVarVariousTypes.nim

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest, necsus
2+
3+
proc create(
4+
sharedTuple: Shared[(float, bool)],
5+
sharedNamedTuple: Shared[tuple[num: float, truth: bool]],
6+
sharedSeq: Shared[seq[string]],
7+
sharedArray: Shared[array[5, char]],
8+
) =
9+
sharedTuple.set((3.14, true))
10+
sharedNamedTuple.set((2.78, false))
11+
sharedSeq.set(@[ "a", "b", "c" ])
12+
sharedArray.set([ 'a', 'b', 'c', 'd', 'e' ])
13+
14+
proc assertions(
15+
sharedTuple: Shared[(float, bool)],
16+
sharedNamedTuple: Shared[tuple[num: float, truth: bool]],
17+
sharedSeq: Shared[seq[string]],
18+
sharedArray: Shared[array[5, char]],
19+
) =
20+
check(sharedTuple.get == (3.14, true))
21+
check(sharedNamedTuple.get == (2.78, false))
22+
check(sharedSeq.get == @[ "a", "b", "c" ])
23+
check(sharedArray.get == [ 'a', 'b', 'c', 'd', 'e' ])
24+
25+
proc run(tick: proc(): void) =
26+
tick()
27+
28+
proc testSharedVar() {.necsus(run, [~create, ~assertions], newNecsusConf()).}
29+
30+
test "Creating shared vars with various types":
31+
testSharedVar()

0 commit comments

Comments
 (0)