Take advantage of some improvements in Nim 1.2
This commit is contained in:
parent
23bbf4b6a5
commit
8f6e86350e
|
@ -1,5 +1,6 @@
|
||||||
import
|
import
|
||||||
faststreams, serialization/[object_serialization, errors]
|
stew/shims/macros, faststreams,
|
||||||
|
serialization/[object_serialization, errors]
|
||||||
|
|
||||||
export
|
export
|
||||||
faststreams, object_serialization, errors
|
faststreams, object_serialization, errors
|
||||||
|
@ -22,48 +23,22 @@ template serializationFormat*(Name: untyped,
|
||||||
mimeType: static string = "") =
|
mimeType: static string = "") =
|
||||||
serializationFormatImpl(Name, Reader, Writer, PreferedOutput, mimeType)
|
serializationFormatImpl(Name, Reader, Writer, PreferedOutput, mimeType)
|
||||||
|
|
||||||
proc encodeImpl(writer: var auto, value: auto) =
|
|
||||||
mixin writeValue, getOutput
|
|
||||||
writer.writeValue value
|
|
||||||
|
|
||||||
template encode*(Format: type, value: auto, params: varargs[untyped]): auto =
|
template encode*(Format: type, value: auto, params: varargs[untyped]): auto =
|
||||||
mixin init, WriterType, PreferedOutputType
|
mixin init, WriterType, writeValue, PreferedOutputType
|
||||||
var s = init OutputStream
|
{.noSideEffect.}:
|
||||||
|
# We assume that there is no side-effects here, because we are
|
||||||
# TODO:
|
# using a `memoryOutput`. The computed side-effects are coming
|
||||||
# Remove this when statement once the following bug is fixed:
|
# from the fact that the dynamic dispatch mechanisms used in
|
||||||
# https://github.com/nim-lang/Nim/issues/9996
|
# faststreams may be writing to a file or a network device.
|
||||||
when astToStr(params) != "":
|
var s = memoryOutput()
|
||||||
var writer = init(WriterType(Format), s, params)
|
var writer = unpackArgs(init, [WriterType(Format), s, params])
|
||||||
else:
|
writeValue writer, value
|
||||||
var writer = init(WriterType(Format), s)
|
s.getOutput PreferedOutputType(Format)
|
||||||
|
|
||||||
encodeImpl(writer, value)
|
|
||||||
s.getOutput PreferedOutputType(Format)
|
|
||||||
|
|
||||||
proc readValue*(reader: var auto, T: type): T =
|
proc readValue*(reader: var auto, T: type): T =
|
||||||
mixin readValue
|
mixin readValue
|
||||||
reader.readValue(result)
|
reader.readValue(result)
|
||||||
|
|
||||||
template decode*(Format: distinct type,
|
|
||||||
input: string,
|
|
||||||
RecordType: distinct type,
|
|
||||||
params: varargs[untyped]): auto =
|
|
||||||
# TODO, this is dusplicated only due to a Nim bug:
|
|
||||||
# If `input` was `string|openarray[byte]`, it won't match `seq[byte]`
|
|
||||||
mixin init, ReaderType
|
|
||||||
var stream = memoryStream(input)
|
|
||||||
|
|
||||||
# TODO:
|
|
||||||
# Remove this when statement once the following bug is fixed:
|
|
||||||
# https://github.com/nim-lang/Nim/issues/9996
|
|
||||||
when astToStr(params) != "":
|
|
||||||
var reader = init(ReaderType(Format), stream, params)
|
|
||||||
else:
|
|
||||||
var reader = init(ReaderType(Format), stream)
|
|
||||||
|
|
||||||
reader.readValue(RecordType)
|
|
||||||
|
|
||||||
template decode*(Format: distinct type,
|
template decode*(Format: distinct type,
|
||||||
input: openarray[byte],
|
input: openarray[byte],
|
||||||
RecordType: distinct type,
|
RecordType: distinct type,
|
||||||
|
@ -71,17 +46,30 @@ template decode*(Format: distinct type,
|
||||||
# TODO, this is dusplicated only due to a Nim bug:
|
# TODO, this is dusplicated only due to a Nim bug:
|
||||||
# If `input` was `string|openarray[byte]`, it won't match `seq[byte]`
|
# If `input` was `string|openarray[byte]`, it won't match `seq[byte]`
|
||||||
mixin init, ReaderType
|
mixin init, ReaderType
|
||||||
var stream = memoryStream(input)
|
{.noSideEffect.}:
|
||||||
|
# We assume that there are no side-effects here, because we are
|
||||||
|
# using a `memoryInput`. The computed side-effects are coming
|
||||||
|
# from the fact that the dynamic dispatch mechanisms used in
|
||||||
|
# faststreams may be reading from a file or a network device.
|
||||||
|
var stream = memoryInput(input)
|
||||||
|
var reader = unpackArgs(init, [ReaderType(Format), stream, params])
|
||||||
|
reader.readValue(RecordType)
|
||||||
|
|
||||||
# TODO:
|
template decode*(Format: distinct type,
|
||||||
# Remove this when statement once the following bug is fixed:
|
input: openarray[byte],
|
||||||
# https://github.com/nim-lang/Nim/issues/9996
|
RecordType: distinct type,
|
||||||
when astToStr(params) != "":
|
params: varargs[untyped]): auto =
|
||||||
var reader = init(ReaderType(Format), stream, params)
|
# TODO, this is dusplicated only due to a Nim bug:
|
||||||
else:
|
# If `input` was `string|openarray[byte]`, it won't match `seq[byte]`
|
||||||
var reader = init(ReaderType(Format), stream)
|
mixin init, ReaderType
|
||||||
|
{.noSideEffect.}:
|
||||||
reader.readValue(RecordType)
|
# We assume that there are no side-effects here, because we are
|
||||||
|
# using a `memoryInput`. The computed side-effects are coming
|
||||||
|
# from the fact that the dynamic dispatch mechanisms used in
|
||||||
|
# faststreams may be reading from a file or a network device.
|
||||||
|
var stream = memoryInput(input)
|
||||||
|
var reader = unpackArgs(init, [ReaderType(Format), stream, params])
|
||||||
|
reader.readValue(RecordType)
|
||||||
|
|
||||||
template loadFile*(Format: distinct type,
|
template loadFile*(Format: distinct type,
|
||||||
filename: string,
|
filename: string,
|
||||||
|
|
|
@ -316,7 +316,7 @@ proc executeReaderWriterTests*(Format: type) =
|
||||||
check fieldReader != nil and idx == 1
|
check fieldReader != nil and idx == 1
|
||||||
|
|
||||||
var bytes = Format.encode("test")
|
var bytes = Format.encode("test")
|
||||||
var stream = memoryStream(bytes)
|
var stream = memoryInput(bytes)
|
||||||
var reader = Reader.init(stream)
|
var reader = Reader.init(stream)
|
||||||
|
|
||||||
var bar: Bar
|
var bar: Bar
|
||||||
|
|
Loading…
Reference in New Issue