From 1f62235d4fc1e174db2ed85e0ce2e54b1b06a7f5 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Sun, 5 Dec 2021 16:11:54 +0100 Subject: [PATCH] Enable --styleCheck:usages --- README.md | 2 +- faststreams.nimble | 8 +++++--- faststreams/buffers.nim | 2 +- faststreams/inputs.nim | 28 ++++++++++++++-------------- faststreams/outputs.nim | 14 +++++++------- faststreams/textio.nim | 8 ++++---- tests/base64.nim | 2 +- tests/test_inputs.nim | 2 +- tests/test_pipelines.nim | 10 +++++----- 9 files changed, 39 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 5b37839..d3b2401 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/faststreams.nimble b/faststreams.nimble index 3770107..dfa7242 100644 --- a/faststreams.nimble +++ b/faststreams.nimble @@ -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" diff --git a/faststreams/buffers.nim b/faststreams/buffers.nim index 99b49f2..267616d 100644 --- a/faststreams/buffers.nim +++ b/faststreams/buffers.nim @@ -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 = diff --git a/faststreams/inputs.nim b/faststreams/inputs.nim index 87242b5..ed9fb7e 100644 --- a/faststreams/inputs.nim +++ b/faststreams/inputs.nim @@ -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] = diff --git a/faststreams/outputs.nim b/faststreams/outputs.nim index 00e48df..50795d4 100644 --- a/faststreams/outputs.nim +++ b/faststreams/outputs.nim @@ -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) diff --git a/faststreams/textio.nim b/faststreams/textio.nim index 4041d08..9bdd449 100644 --- a/faststreams/textio.nim +++ b/faststreams/textio.nim @@ -93,7 +93,7 @@ proc writeText*(s: OutputStream, x: CompiledIntTypes) = writeText(s, MatchingUInt(x)) when defined(c): - + proc writeText*(s: OutputStream, x: float64|float32|float) = ## Write the floating point number to the output stream. It has less overhead ## than `$` because it is directly written to the stream without @@ -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): diff --git a/tests/base64.nim b/tests/base64.nim index db86a7d..3bd9c88 100644 --- a/tests/base64.nim +++ b/tests/base64.nim @@ -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 & ".") diff --git a/tests/test_inputs.nim b/tests/test_inputs.nim index ee1a103..74c8cdf 100644 --- a/tests/test_inputs.nim +++ b/tests/test_inputs.nim @@ -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 diff --git a/tests/test_pipelines.nim b/tests/test_pipelines.nim index de4861f..6952a9e 100644 --- a/tests/test_pipelines.nim +++ b/tests/test_pipelines.nim @@ -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