Skip to content

Commit

Permalink
Support tuples in shared vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Nycto committed May 23, 2024
1 parent 090beff commit 5176720
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/necsus/util/nimNode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ proc symbols*(node: NimNode): seq[string] =
of nnkCharLit..nnkUInt64Lit: return @[$node.intVal]
of nnkFloatLit..nnkFloat64Lit: return @[$node.floatVal]
of nnkNilLit: return @["nil"]
of nnkBracketExpr, nnkTupleTy: return node.toSeq.mapIt(it.symbols).foldl(concat(a, b))
of nnkBracketExpr, nnkTupleTy, nnkTupleConstr: return node.toSeq.mapIt(it.symbols).foldl(concat(a, b))
of nnkIdentDefs: return concat(node[0].symbols, node[1].symbols)
of nnkRefTy: return concat(@["ref"], node[0].symbols)
else: error(&"Unable to generate a component symbol from node ({node.kind}): {node.repr}")
Expand All @@ -19,7 +19,8 @@ proc hash*(node: NimNode): Hash =
of nnkCharLit..nnkUInt64Lit: return hash(node.intVal)
of nnkFloatLit..nnkFloat64Lit: return hash(node.floatVal)
of nnkNilLit, nnkEmpty: return hash(0)
of nnkBracketExpr, nnkTupleTy, nnkIdentDefs: return node.toSeq.mapIt(hash(it)).foldl(a !& b, hash(node.kind))
of nnkBracketExpr, nnkTupleTy, nnkIdentDefs, nnkTupleConstr:
return node.toSeq.mapIt(hash(it)).foldl(a !& b, hash(node.kind))
of nnkRefTy: return hash(node[0])
else: error(&"Unable to generate a hash from node ({node.kind}): {node.repr}")

Expand Down
31 changes: 31 additions & 0 deletions tests/t_sharedVarVariousTypes.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest, necsus

proc create(
sharedTuple: Shared[(float, bool)],
sharedNamedTuple: Shared[tuple[num: float, truth: bool]],
sharedSeq: Shared[seq[string]],
sharedArray: Shared[array[5, char]],
) =
sharedTuple.set((3.14, true))
sharedNamedTuple.set((2.78, false))
sharedSeq.set(@[ "a", "b", "c" ])
sharedArray.set([ 'a', 'b', 'c', 'd', 'e' ])

proc assertions(
sharedTuple: Shared[(float, bool)],
sharedNamedTuple: Shared[tuple[num: float, truth: bool]],
sharedSeq: Shared[seq[string]],
sharedArray: Shared[array[5, char]],
) =
check(sharedTuple.get == (3.14, true))
check(sharedNamedTuple.get == (2.78, false))
check(sharedSeq.get == @[ "a", "b", "c" ])
check(sharedArray.get == [ 'a', 'b', 'c', 'd', 'e' ])

proc run(tick: proc(): void) =
tick()

proc testSharedVar() {.necsus(run, [~create, ~assertions], newNecsusConf()).}

test "Creating shared vars with various types":
testSharedVar()

0 comments on commit 5176720

Please sign in to comment.