Add support for not nil ref types
This commit is contained in:
parent
96a337d334
commit
bed0e403a4
|
@ -1,3 +1,5 @@
|
||||||
|
{.experimental: "notnil".}
|
||||||
|
|
||||||
import
|
import
|
||||||
strutils, typetraits, macros, strformat,
|
strutils, typetraits, macros, strformat,
|
||||||
faststreams/input_stream, serialization/[object_serialization, errors],
|
faststreams/input_stream, serialization/[object_serialization, errors],
|
||||||
|
@ -164,6 +166,9 @@ func maxAbsValue(T: type[SomeInteger]): uint64 {.compileTime.} =
|
||||||
elif T is int64: 9223372036854775808'u64
|
elif T is int64: 9223372036854775808'u64
|
||||||
else: uint64(high(T))
|
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) =
|
proc readValue*(r: var JsonReader, value: var auto) =
|
||||||
mixin readValue
|
mixin readValue
|
||||||
|
|
||||||
|
@ -182,6 +187,10 @@ proc readValue*(r: var JsonReader, value: var auto) =
|
||||||
r.lexer.next()
|
r.lexer.next()
|
||||||
|
|
||||||
elif value is ref|ptr:
|
elif value is ref|ptr:
|
||||||
|
when compiles(isNotNilCheck(value)):
|
||||||
|
allocPtr value
|
||||||
|
value[] = readValue(r, type(value[]))
|
||||||
|
else:
|
||||||
if tok == tkNull:
|
if tok == tkNull:
|
||||||
value = nil
|
value = nil
|
||||||
r.lexer.next()
|
r.lexer.next()
|
||||||
|
|
Loading…
Reference in New Issue