Enable --styleCheck:usages
This commit is contained in:
parent
1b561a9e71
commit
1f62235d4f
9 changed files with 39 additions and 37 deletions
|
|
@ -34,7 +34,7 @@ in a way that allows the read and write operations to be handled without any
|
|||
dynamic dispatch in the majority of cases.
|
||||
|
||||
In particular, reading from a `memoryInput` or writing to a `memoryOutput`
|
||||
will have similar performance to a loop iterating over an `openarray` or
|
||||
will have similar performance to a loop iterating over an `openArray` or
|
||||
another loop populating a pre-allocated `string`. `memFileInput` offers
|
||||
the same performance characteristics when working with files. The idiomatic
|
||||
use of the APIs with the rest of the stream types will result in a highly
|
||||
|
|
|
|||
|
|
@ -20,13 +20,15 @@ proc test(env, path: string) =
|
|||
if existsEnv"TEST_LANG":
|
||||
lang = getEnv"TEST_LANG"
|
||||
|
||||
let common_args = "-r -f --hints:off --skipParentCfg --styleCheck:usages --styleCheck:error"
|
||||
|
||||
exec "nim " & lang & " " & env &
|
||||
" -d:async_backend=none -r --hints:off --skipParentCfg " & path
|
||||
" -d:async_backend=none " & common_args & " " & path
|
||||
exec "nim " & lang & " " & env &
|
||||
" -d:async_backend=chronos -r --hints:off --skipParentCfg " & path
|
||||
" -d:async_backend=chronos " & common_args & " " & path
|
||||
# TODO std backend is broken / untested
|
||||
# exec "nim " & lang & " " & env &
|
||||
# " -d:async_backend=chronos -r --hints:off --skipParentCfg " & path
|
||||
# " -d:async_backend=chronos " & common_args & " " & path
|
||||
|
||||
task test, "Run all tests":
|
||||
test "-d:debug --threads:on", "tests/all_tests"
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ else:
|
|||
template describeBuffers*(context: static string, buffers: PageBuffers) =
|
||||
discard
|
||||
|
||||
func openArrayToPair*(a: var openarray[byte]): (ptr byte, Natural) =
|
||||
func openArrayToPair*(a: var openArray[byte]): (ptr byte, Natural) =
|
||||
(addr a[0], Natural(a.len))
|
||||
|
||||
template allocationStart*(page: PageRef): ptr byte =
|
||||
|
|
|
|||
|
|
@ -424,7 +424,7 @@ proc fileInput*(filename: string,
|
|||
buffers: initPageBuffers(pageSize),
|
||||
file: file)
|
||||
|
||||
proc unsafeMemoryInput*(mem: openarray[byte]): InputStreamHandle =
|
||||
proc unsafeMemoryInput*(mem: openArray[byte]): InputStreamHandle =
|
||||
let head = unsafeAddr mem[0]
|
||||
|
||||
makeHandle InputStream(
|
||||
|
|
@ -457,7 +457,7 @@ func memoryInput*(buffers: PageBuffers): InputStreamHandle =
|
|||
span: span,
|
||||
spanEndPos: spanEndPos)
|
||||
|
||||
func memoryInput*(data: openarray[byte]): InputStreamHandle =
|
||||
func memoryInput*(data: openArray[byte]): InputStreamHandle =
|
||||
let stream = if data.len > 0:
|
||||
let
|
||||
buffers = initPageBuffers(data.len)
|
||||
|
|
@ -474,7 +474,7 @@ func memoryInput*(data: openarray[byte]): InputStreamHandle =
|
|||
|
||||
makeHandle stream
|
||||
|
||||
func memoryInput*(data: openarray[char]): InputStreamHandle =
|
||||
func memoryInput*(data: openArray[char]): InputStreamHandle =
|
||||
memoryInput charsToBytes(data)
|
||||
|
||||
proc resetBuffers*(s: InputStream, buffers: PageBuffers) =
|
||||
|
|
@ -812,7 +812,7 @@ template readIntoExImpl(s: InputStream,
|
|||
|
||||
dstLen - bytesDeficit
|
||||
|
||||
proc readIntoEx*(s: InputStream, dst: var openarray[byte]): int =
|
||||
proc readIntoEx*(s: InputStream, dst: var openArray[byte]): int =
|
||||
## Read data into the destination buffer.
|
||||
##
|
||||
## Returns the number of bytes that were successfully
|
||||
|
|
@ -823,7 +823,7 @@ proc readIntoEx*(s: InputStream, dst: var openarray[byte]): int =
|
|||
let dstLen = dst.len
|
||||
readIntoExImpl(s, dstAddr, dstLen, noAwait, readSync)
|
||||
|
||||
proc readInto*(s: InputStream, target: var openarray[byte]): bool =
|
||||
proc readInto*(s: InputStream, target: var openArray[byte]): bool =
|
||||
## Read data into the destination buffer.
|
||||
##
|
||||
## Returns `false` if EOF was reached before the buffer
|
||||
|
|
@ -832,14 +832,14 @@ proc readInto*(s: InputStream, target: var openarray[byte]): bool =
|
|||
s.readIntoEx(target) == target.len
|
||||
|
||||
when fsAsyncSupport:
|
||||
template readIntoEx*(sp: AsyncInputStream, dst: var openarray[byte]): int =
|
||||
template readIntoEx*(sp: AsyncInputStream, dst: var openArray[byte]): int =
|
||||
let s = InputStream(sp)
|
||||
# BEWARE! `openArrayToPair` here is needed to avoid
|
||||
# double evaluation of the `dst` expression:
|
||||
let (dstAddr, dstLen) = openArrayToPair(dst)
|
||||
readIntoExImpl(s, dstAddr, dstLen, fsAwait, readAsync)
|
||||
|
||||
template readInto*(sp: AsyncInputStream, dst: var openarray[byte]): bool =
|
||||
template readInto*(sp: AsyncInputStream, dst: var openArray[byte]): bool =
|
||||
## Asynchronously read data into the destination buffer.
|
||||
##
|
||||
## Returns `false` if EOF was reached before the buffer
|
||||
|
|
@ -871,7 +871,7 @@ template useStackMem(n: static Natural) =
|
|||
|
||||
template readNImpl(sp: InputStream,
|
||||
np: Natural,
|
||||
createAllocMemOp: untyped): openarray[byte] =
|
||||
createAllocMemOp: untyped): openArray[byte] =
|
||||
let
|
||||
s = sp
|
||||
n = np
|
||||
|
|
@ -881,7 +881,7 @@ template readNImpl(sp: InputStream,
|
|||
# to appear in different branches of an if statement, the code must
|
||||
# be written in this branch-free linear fashion. The `dataCopy` seq
|
||||
# may remain empty in the case where we use stack memory or return
|
||||
# an `openarray` from the existing span.
|
||||
# an `openArray` from the existing span.
|
||||
var startAddr: ptr byte
|
||||
|
||||
block:
|
||||
|
|
@ -902,21 +902,21 @@ template readNImpl(sp: InputStream,
|
|||
|
||||
makeOpenArray(startAddr, n)
|
||||
|
||||
template read*(sp: InputStream, np: static Natural): openarray[byte] =
|
||||
template read*(sp: InputStream, np: static Natural): openArray[byte] =
|
||||
const n = np
|
||||
when n < maxStackUsage:
|
||||
readNImpl(sp, n, useStackMem)
|
||||
else:
|
||||
readNImpl(sp, n, useHeapMem)
|
||||
|
||||
template read*(s: InputStream, n: Natural): openarray[byte] =
|
||||
template read*(s: InputStream, n: Natural): openArray[byte] =
|
||||
readNImpl(s, n, useHeapMem)
|
||||
|
||||
when fsAsyncSupport:
|
||||
template read*(s: AsyncInputStream, n: Natural): openarray[byte] =
|
||||
template read*(s: AsyncInputStream, n: Natural): openArray[byte] =
|
||||
read InputStream(s), n
|
||||
|
||||
proc lookAheadMatch*(s: InputStream, data: openarray[byte]): bool =
|
||||
proc lookAheadMatch*(s: InputStream, data: openArray[byte]): bool =
|
||||
for i in 0 ..< data.len:
|
||||
if s.peekAt(i) != data[i]:
|
||||
return false
|
||||
|
|
@ -924,7 +924,7 @@ proc lookAheadMatch*(s: InputStream, data: openarray[byte]): bool =
|
|||
return true
|
||||
|
||||
when fsAsyncSupport:
|
||||
template lookAheadMatch*(s: AsyncInputStream, data: openarray[byte]): bool =
|
||||
template lookAheadMatch*(s: AsyncInputStream, data: openArray[byte]): bool =
|
||||
lookAheadMatch InputStream(s)
|
||||
|
||||
proc next*(s: InputStream): Option[byte] =
|
||||
|
|
|
|||
|
|
@ -652,13 +652,13 @@ proc writeMemCopy*(s: OutputStream, value: auto) =
|
|||
|
||||
when fsAsyncSupport:
|
||||
proc writeBytesAsyncImpl(sp: OutputStream,
|
||||
bytes: openarray[byte]): Future[void] =
|
||||
bytes: openArray[byte]): Future[void] =
|
||||
let s = sp
|
||||
writeBytesImpl(s, bytes):
|
||||
return s.vtable.writeAsync(s, unsafeAddr bytes[0], bytes.len)
|
||||
|
||||
proc writeBytesAsyncImpl(s: OutputStream,
|
||||
chars: openarray[char]): Future[void] =
|
||||
chars: openArray[char]): Future[void] =
|
||||
writeBytesAsyncImpl s, charsToBytes(chars)
|
||||
|
||||
proc writeBytesAsyncImpl(s: OutputStream,
|
||||
|
|
@ -684,7 +684,7 @@ when fsAsyncSupport:
|
|||
template writeMemCopyAndWait*(sp: AsyncOutputStream, value: auto) =
|
||||
writeAndWait(sp, memCopyToBytes(value))
|
||||
|
||||
proc writeBytesToCursor(c: var WriteCursor, bytes: openarray[byte]) =
|
||||
proc writeBytesToCursor(c: var WriteCursor, bytes: openArray[byte]) =
|
||||
var
|
||||
runway = c.span.len
|
||||
inputPos = unsafeAddr bytes[0]
|
||||
|
|
@ -713,11 +713,11 @@ proc writeBytesToCursor(c: var WriteCursor, bytes: openarray[byte]) =
|
|||
copyMem(c.span.startAddr, inputPos, inputLen)
|
||||
c.span.startAddr = offset(c.span.startAddr, inputLen)
|
||||
|
||||
template write*(c: var WriteCursor, bytes: openarray[byte]) =
|
||||
template write*(c: var WriteCursor, bytes: openArray[byte]) =
|
||||
bind writeBytesToCursor
|
||||
writeBytesToCursor(c, bytes)
|
||||
|
||||
proc write*(c: var WriteCursor, chars: openarray[char]) {.inline.} =
|
||||
proc write*(c: var WriteCursor, chars: openArray[char]) {.inline.} =
|
||||
var charsStart = unsafeAddr chars[0]
|
||||
writeBytesToCursor(c, makeOpenArray(cast[ptr byte](charsStart), chars.len))
|
||||
|
||||
|
|
@ -749,7 +749,7 @@ template consumeOutputs*(s: OutputStream, bytesVar, body: untyped) =
|
|||
##
|
||||
## Before consuming the outputs, all outstanding delayed writes must
|
||||
## be finalized.
|
||||
proc consumer(bytesVar: openarray[byte]) {.gcsafe, raises: [Defect].} =
|
||||
proc consumer(bytesVar: openArray[byte]) {.gcsafe, raises: [Defect].} =
|
||||
body
|
||||
|
||||
consumeOutputsImpl(s, consumer)
|
||||
|
|
@ -795,7 +795,7 @@ template consumeContiguousOutput*(s: OutputStream, bytesVar, body: untyped) =
|
|||
## Before consuming the output, all outstanding delayed writes must
|
||||
## be finalized.
|
||||
##
|
||||
proc consumer(bytesVar: openarray[byte]) {.gcsafe, raises: [Defect].} =
|
||||
proc consumer(bytesVar: openArray[byte]) {.gcsafe, raises: [Defect].} =
|
||||
body
|
||||
|
||||
consumeContiguousOutputImpl(s, consumer)
|
||||
|
|
|
|||
|
|
@ -112,14 +112,14 @@ template writeText*(s: OutputStream, str: string) =
|
|||
template writeText*(s: OutputStream, val: auto) =
|
||||
write s, $val
|
||||
|
||||
proc writeHex*(s: OutputStream, bytes: openarray[byte]) =
|
||||
proc writeHex*(s: OutputStream, bytes: openArray[byte]) =
|
||||
const hexChars = "0123456789abcdef"
|
||||
|
||||
for b in bytes:
|
||||
s.write hexChars[int b shr 4 and 0xF]
|
||||
s.write hexChars[int b and 0xF]
|
||||
|
||||
proc writeHex*(s: OutputStream, chars: openarray[char]) =
|
||||
proc writeHex*(s: OutputStream, chars: openArray[char]) =
|
||||
writeHex s, charsToBytes(chars)
|
||||
|
||||
const
|
||||
|
|
@ -145,7 +145,7 @@ proc readLine*(s: InputStream, keepEol = false): TaintedString {.fsMultiSync.} =
|
|||
result.add s.read.char
|
||||
|
||||
proc readUntil*(s: InputStream,
|
||||
sep: openarray[char]): Option[TaintedString] =
|
||||
sep: openArray[char]): Option[TaintedString] =
|
||||
fsAssert readableNow(s)
|
||||
var res = ""
|
||||
while s.readable(sep.len):
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ proc base64decode*(i: InputStream, o: OutputStream) {.fsMultiSync.} =
|
|||
proc decodeSize(size: int): int =
|
||||
return (size * 3 div 4) + 6
|
||||
|
||||
proc raiseInvalidChar(c: byte, pos: int) {.noReturn.} =
|
||||
proc raiseInvalidChar(c: byte, pos: int) {.noreturn.} =
|
||||
raise newException(ValueError,
|
||||
"Invalid base64 format character `" & char(c) & "` at location " & $pos & ".")
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import
|
|||
|
||||
setCurrentDir getAppDir()
|
||||
|
||||
proc str(bytes: openarray[byte]): string =
|
||||
proc str(bytes: openArray[byte]): string =
|
||||
result = newStringOfCap(bytes.len)
|
||||
for b in items(bytes):
|
||||
result.add b.char
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ when fsAsyncSupport:
|
|||
let size = sp.totalUnconsumedBytes()
|
||||
if size > 0:
|
||||
var data = newSeq[byte](size)
|
||||
discard sp.readinto(data)
|
||||
discard sp.readInto(data)
|
||||
result = cast[string](data)
|
||||
|
||||
suite "pipelines":
|
||||
|
|
@ -73,17 +73,17 @@ when fsAsyncSupport:
|
|||
|
||||
let inputText = loremIpsum.repeat(5000)
|
||||
|
||||
timeIt times.stdFunctionCalls:
|
||||
timeit times.stdFunctionCalls:
|
||||
stdRes = base64.decode(base64.encode(toUpperAscii(inputText)))
|
||||
|
||||
timeIt times.fsPipeline:
|
||||
timeit times.fsPipeline:
|
||||
fsRes = executePipeline(unsafeMemoryInput(inputText),
|
||||
upcaseAllCharacters,
|
||||
base64encode,
|
||||
base64decode,
|
||||
getOutput string)
|
||||
|
||||
timeIt times.fsAsyncPipeline:
|
||||
timeit times.fsAsyncPipeline:
|
||||
fsAsyncRes = waitFor executePipeline(Async unsafeMemoryInput(inputText),
|
||||
upcaseAllCharacters,
|
||||
base64encode,
|
||||
|
|
@ -130,7 +130,7 @@ when fsAsyncSupport:
|
|||
|
||||
let fsAsyncres = await f
|
||||
|
||||
check fsAsyncRes == toUpperAscii(inputText)
|
||||
check fsAsyncres == toUpperAscii(inputText)
|
||||
else:
|
||||
test "pipelines":
|
||||
skip
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue