Move stuff around; Add some missing overloads

This commit is contained in:
Zahary Karadjov 2020-05-05 23:57:27 +03:00
commit 65b43d9e38
No known key found for this signature in database
GPG key ID: C8936F8A3073D609
4 changed files with 50 additions and 36 deletions

View file

@ -316,31 +316,6 @@ template charsToBytes*(chars: openArray[char]): untyped =
var charsStart = unsafeAddr chars[0] var charsStart = unsafeAddr chars[0]
makeOpenArray(cast[ptr byte](charsStart), chars.len) makeOpenArray(cast[ptr byte](charsStart), chars.len)
template implementWrites*(buffersParam: PageBuffers,
srcParam: pointer,
srcLenParam: Natural,
dstDesc: static string,
writeStartVar, writeLenVar,
writeBlock: untyped) =
let
buffers = buffersParam
writeStartVar = srcParam
writeLenVar = srcLenParam
template raiseError =
raise newException(IOError, "Failed to write all bytes to " & dstDesc)
if buffers != nil:
for writeStartVar, writeLenVar in consumePageBuffers(s.buffers):
let bytesWritten = writeBlock
# TODO: Can we repair the buffers here?
if bytesWritten != writeLenVar: raiseError()
if srcLen > 0:
fsAssert src != nil
let bytesWritten = writeBlock
if bytesWritten != writeLenVar: raiseError()
type type
ReadFlag* = enum ReadFlag* = enum
partialReadIsEof partialReadIsEof

View file

@ -53,11 +53,11 @@ type
FileInputStream = ref object of InputStream FileInputStream = ref object of InputStream
file: File file: File
template Async*(s: InputStream): AsyncInputStream = template Sync*(s: InputStream): InputStream = s
AsyncInputStream(s) template Async*(s: InputStream): AsyncInputStream = AsyncInputStream(s)
template Sync*(s: AsyncInputStream): InputStream = template Sync*(s: AsyncInputStream): InputStream = InputStream(s)
InputStream(s) template Async*(s: AsyncInputStream): AsyncInputStream = s
proc disconnectInputDevice(s: InputStream) = proc disconnectInputDevice(s: InputStream) =
# TODO # TODO
@ -578,9 +578,17 @@ proc advance*(s: InputStream) =
else: else:
flipPage s flipPage s
proc advance*(s: InputStream, n: Natural) =
# TODO This is silly, implement it properly
for i in 0 ..< n:
advance s
template advance*(s: AsyncInputStream) = template advance*(s: AsyncInputStream) =
advance InputStream(s) advance InputStream(s)
template advance*(s: AsyncInputStream, n: Natural) =
advance InputStream(s), n
proc drainBuffersInto*(s: InputStream, dstAddr: ptr byte, dstLen: Natural): Natural = proc drainBuffersInto*(s: InputStream, dstAddr: ptr byte, dstLen: Natural): Natural =
var var
dst = dstAddr dst = dstAddr

View file

@ -2,6 +2,9 @@ import
stew/shims/macros, stew/shims/macros,
async_backend, inputs, outputs async_backend, inputs, outputs
export
async_backend
macro fsMultiSync*(body: untyped) = macro fsMultiSync*(body: untyped) =
# We will produce an identical copy of the annotated proc, # We will produce an identical copy of the annotated proc,
# but taking async parameters and having the async pragma. # but taking async parameters and having the async pragma.
@ -13,9 +16,9 @@ macro fsMultiSync*(body: untyped) =
# The return types becomes Future[T] # The return types becomes Future[T]
if asyncProcParams[0].kind == nnkEmpty: if asyncProcParams[0].kind == nnkEmpty:
asyncProcParams[0] = newTree(nnkBracketExpr, ident"Future", ident"void") asyncProcParams[0] = newTree(nnkBracketExpr, bindSym"Future", ident"void")
else: else:
asyncProcParams[0] = newTree(nnkBracketExpr, ident"Future", asyncProcParams[0]) asyncProcParams[0] = newTree(nnkBracketExpr, bindSym"Future", asyncProcParams[0])
# We replace all stream inputs with their async counterparts # We replace all stream inputs with their async counterparts
for i in 1 ..< asyncProcParams.len: for i in 1 ..< asyncProcParams.len:

View file

@ -12,7 +12,7 @@ import
buffers, async_backend buffers, async_backend
export export
CloseBehavior initPageBuffers, CloseBehavior
type type
OutputStream* = ref object of RootObj OutputStream* = ref object of RootObj
@ -69,11 +69,11 @@ type
FileOutputStream = ref object of OutputStream FileOutputStream = ref object of OutputStream
file: File file: File
template Async*(s: OutputStream): AsyncOutputStream = template Sync*(s: OutputStream): OutputStream = s
AsyncOutputStream(s) template Async*(s: OutputStream): AsyncOutputStream = AsyncOutputStream(s)
template Sync*(s: AsyncOutputStream): OutputStream = template Sync*(s: AsyncOutputStream): OutputStream = OutputStream(s)
OuputStream(s) template Async*(s: AsyncOutputStream): AsyncOutputStream = s
proc disconnectOutputDevice(s: OutputStream) = proc disconnectOutputDevice(s: OutputStream) =
if s.vtable != nil: if s.vtable != nil:
@ -199,6 +199,31 @@ proc ensureRunway*(s: OutputStream, neededRunway: Natural) =
template ensureRunway*(s: AsyncOutputStream, neededRunway: Natural) = template ensureRunway*(s: AsyncOutputStream, neededRunway: Natural) =
ensureRunway OutputStream(s), neededRunway ensureRunway OutputStream(s), neededRunway
template implementWrites*(buffersParam: PageBuffers,
srcParam: pointer,
srcLenParam: Natural,
dstDesc: static string,
writeStartVar, writeLenVar,
writeBlock: untyped) =
let
buffers = buffersParam
writeStartVar = srcParam
writeLenVar = srcLenParam
template raiseError =
raise newException(IOError, "Failed to write all bytes to " & dstDesc)
if buffers != nil:
for writeStartVar, writeLenVar in consumePageBuffers(s.buffers):
let bytesWritten = writeBlock
# TODO: Can we repair the buffers here?
if bytesWritten != writeLenVar: raiseError()
if writeLenVar > 0:
fsAssert writeStartVar != nil
let bytesWritten = writeBlock
if bytesWritten != writeLenVar: raiseError()
let fileOutputVTable = OutputStreamVTable( let fileOutputVTable = OutputStreamVTable(
writeSync: proc (s: OutputStream, src: pointer, srcLen: Natural) writeSync: proc (s: OutputStream, src: pointer, srcLen: Natural)
{.nimcall, gcsafe, raises: [IOError, Defect].} = {.nimcall, gcsafe, raises: [IOError, Defect].} =
@ -583,6 +608,9 @@ proc writeBytesAsyncImpl(s: OutputStream,
str: string): Future[void] = str: string): Future[void] =
writeBytesAsyncImpl s, toOpenArray(str, 0, str.len - 1) writeBytesAsyncImpl s, toOpenArray(str, 0, str.len - 1)
template writeAndWait*(s: OutputStream, value: untyped) =
write s, value
template writeAndWait*(sp: AsyncOutputStream, value: untyped) = template writeAndWait*(sp: AsyncOutputStream, value: untyped) =
bind writeBytesAsyncImpl bind writeBytesAsyncImpl