Don't rely on the filestream destructors

This commit is contained in:
Zahary Karadjov 2020-04-22 21:26:02 +03:00
parent 67bb6a77bb
commit db037cbece
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
1 changed files with 11 additions and 5 deletions

View File

@ -78,8 +78,11 @@ template loadFile*(Format: distinct type,
mixin init, ReaderType, readValue
var stream = fileInput(filename)
var reader = unpackArgs(init, [ReaderType(Format), stream, params])
reader.readValue(RecordType)
try:
var reader = unpackArgs(init, [ReaderType(Format), stream, params])
reader.readValue(RecordType)
finally:
close stream
template loadFile*[RecordType](Format: type,
filename: string,
@ -91,9 +94,12 @@ template saveFile*(Format: type, filename: string, value: auto, params: varargs[
mixin init, WriterType, writeValue
var stream = fileOutput(filename)
var writer = unpackArgs(init, [WriterType(Format), stream, params])
writer.writeValue(value)
flush stream
try:
var writer = unpackArgs(init, [WriterType(Format), stream, params])
writer.writeValue(value)
flush stream
finally:
close stream
template borrowSerialization*(Alias: distinct type,
OriginalType: distinct type) {.dirty.} =