Skip to content

Commit

Permalink
Add support for not nil ref types
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed Apr 22, 2020
1 parent 96a337d commit bed0e40
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions json_serialization/reader.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{.experimental: "notnil".}

import
strutils, typetraits, macros, strformat,
faststreams/input_stream, serialization/[object_serialization, errors],
Expand Down Expand Up @@ -164,6 +166,9 @@ func maxAbsValue(T: type[SomeInteger]): uint64 {.compileTime.} =
elif T is int64: 9223372036854775808'u64
else: uint64(high(T))

proc isNotNilCheck[T](x: ref T not nil) {.compileTime.} = discard
proc isNotNilCheck[T](x: ptr T not nil) {.compileTime.} = discard

proc readValue*(r: var JsonReader, value: var auto) =
mixin readValue

Expand All @@ -182,12 +187,16 @@ proc readValue*(r: var JsonReader, value: var auto) =
r.lexer.next()

elif value is ref|ptr:
if tok == tkNull:
value = nil
r.lexer.next()
else:
when compiles(isNotNilCheck(value)):
allocPtr value
value[] = readValue(r, type(value[]))
else:
if tok == tkNull:
value = nil
r.lexer.next()
else:
allocPtr value
value[] = readValue(r, type(value[]))

elif value is enum:
case tok
Expand Down

0 comments on commit bed0e40

Please sign in to comment.