Various fixes to FutureStreams based on PR feedback.

This commit is contained in:
Dominik Picheta 2017-02-26 12:52:19 +01:00
commit f9cce32097
7 changed files with 72 additions and 48 deletions

View file

@ -476,13 +476,16 @@ proc close*(f: AsyncFile) =
if close(f.fd.cint) == -1:
raiseOSError(osLastError())
proc writeFromStream(f: AsyncFile, fut: FutureStream[string]) {.async.} =
proc writeFromStream*(f: AsyncFile, fs: FutureStream[string]) {.async.} =
## Reads data from the specified future stream until it is completed.
## The data which is read is written to the file immediately and
## freed from memory.
##
## This procedure is perfect for saving streamed data to a file without
## wasting memory.
while true:
let (hasValue, value) = await fut.take()
let (hasValue, value) = await fs.read()
if hasValue:
await f.write(value)
else:
break
proc setWriteStream*(f: AsyncFile; fut: FutureStream[string]) {.async.} =
await writeFromStream(f, fut)