Add apply procedure
This commit is contained in:
parent
abb39e02a5
commit
cc0472da3d
|
@ -1,6 +1,6 @@
|
|||
# Package
|
||||
|
||||
version = "3.3.2" # versioned in git, this version is just to confuse nimble
|
||||
version = "99999999"
|
||||
author = "Emery Hemingway"
|
||||
description = "data model and serialization format"
|
||||
license = "Unlicense"
|
||||
|
|
|
@ -1043,6 +1043,22 @@ when isMainModule:
|
|||
var pr = t.toPreserveHook(void)
|
||||
assert fromPreserveHook(t, pr)
|
||||
|
||||
proc apply*[E](result: var Preserve[E]; op: proc(_: var Preserve[E])) =
|
||||
proc recurse(result: var Preserve[E]) = apply(result, op)
|
||||
op(result)
|
||||
case result.kind
|
||||
of pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkString, pkByteString, pkSymbol, pkEmbedded: discard
|
||||
of pkRecord:
|
||||
apply(result.record, recurse)
|
||||
of pkSequence:
|
||||
apply(result.sequence, recurse)
|
||||
of pkSet:
|
||||
apply(result.set, recurse)
|
||||
of pkDictionary:
|
||||
apply(result.dict) do (e: var DictEntry[E]):
|
||||
recurse(e.key)
|
||||
recurse(e.val)
|
||||
|
||||
proc mapEmbeds*(pr: sink Preserve[void]; E: typedesc): Preserve[E] =
|
||||
## Convert `Preserve[void]` to `Preserve[E]` using `fromPreserve` for `E`.
|
||||
when E is void: {.error: "E cannot be void".}
|
||||
|
|
Loading…
Reference in New Issue