diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2019-07-18 18:16:32 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-07-18 18:16:32 +0200 |
commit | fbb0642e278efd1b9ac4175228befc58655c3849 (patch) | |
tree | ae4d5349c81e33bd83cfe8c3ea94bc0c5e684100 /lib/pure | |
parent | 9852cf804b09710837c849bdfda2be499e702a05 (diff) | |
parent | 6d8913ee1422143baebd438f7526208193c0bd5e (diff) | |
download | Nim-fbb0642e278efd1b9ac4175228befc58655c3849.tar.gz |
Merge branch 'devel' into araq-detect-unused-imports
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/asyncdispatch.nim | 16 | ||||
-rw-r--r-- | lib/pure/collections/intsets.nim | 2 | ||||
-rw-r--r-- | lib/pure/concurrency/cpuinfo.nim | 4 | ||||
-rw-r--r-- | lib/pure/hashes.nim | 4 | ||||
-rw-r--r-- | lib/pure/httpclient.nim | 10 | ||||
-rw-r--r-- | lib/pure/json.nim | 4 | ||||
-rw-r--r-- | lib/pure/net.nim | 19 | ||||
-rw-r--r-- | lib/pure/os.nim | 2 | ||||
-rw-r--r-- | lib/pure/terminal.nim | 2 | ||||
-rw-r--r-- | lib/pure/unicode.nim | 6 | ||||
-rw-r--r-- | lib/pure/unittest.nim | 16 |
11 files changed, 42 insertions, 43 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index f0f498b9f..f1f27cc42 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -169,7 +169,7 @@ include "system/inclrtl" import os, tables, strutils, times, heapqueue, lists, options, asyncstreams -import options, math +import options, math, std/monotimes import asyncfutures except callSoon import nativesockets, net, deques @@ -184,7 +184,7 @@ export asyncstreams type PDispatcherBase = ref object of RootRef - timers*: HeapQueue[tuple[finishAt: float, fut: Future[void]]] + timers*: HeapQueue[tuple[finishAt: MonoTime, fut: Future[void]]] callbacks*: Deque[proc () {.gcsafe.}] proc processTimers( @@ -192,7 +192,7 @@ proc processTimers( ): Option[int] {.inline.} = # Pop the timers in the order in which they will expire (smaller `finishAt`). var count = p.timers.len - let t = epochTime() + let t = getMonoTime() while count > 0 and t >= p.timers[0].finishAt: p.timers.pop().fut.complete() dec count @@ -201,8 +201,8 @@ proc processTimers( # Return the number of miliseconds in which the next timer will expire. if p.timers.len == 0: return - let milisecs = (p.timers[0].finishAt - epochTime()) * 1000 - return some(ceil(milisecs).int) + let millisecs = (p.timers[0].finishAt - getMonoTime()).inMilliseconds + return some(millisecs.int + 1) proc processPendingCallbacks(p: PDispatcherBase; didSomeWork: var bool) = while p.callbacks.len > 0: @@ -1778,7 +1778,11 @@ proc sleepAsync*(ms: int | float): owned(Future[void]) = ## ``ms`` milliseconds. var retFuture = newFuture[void]("sleepAsync") let p = getGlobalDispatcher() - p.timers.push((epochTime() + (ms / 1000), retFuture)) + when ms is int: + p.timers.push((getMonoTime() + initDuration(milliseconds = ms), retFuture)) + elif ms is float: + let ns = (ms * 1_000_000).int64 + p.timers.push((getMonoTime() + initDuration(nanoseconds = ns), retFuture)) return retFuture proc withTimeout*[T](fut: Future[T], timeout: int): owned(Future[bool]) = diff --git a/lib/pure/collections/intsets.nim b/lib/pure/collections/intsets.nim index 91b6b55e8..8b7703804 100644 --- a/lib/pure/collections/intsets.nim +++ b/lib/pure/collections/intsets.nim @@ -20,7 +20,7 @@ import - hashes, math + hashes type BitScalar = uint diff --git a/lib/pure/concurrency/cpuinfo.nim b/lib/pure/concurrency/cpuinfo.nim index 4f681f980..6fc5eb95b 100644 --- a/lib/pure/concurrency/cpuinfo.nim +++ b/lib/pure/concurrency/cpuinfo.nim @@ -11,10 +11,8 @@ include "system/inclrtl" -import strutils, os - when not defined(windows): - import posix + import strutils, posix, os when defined(linux): import linux diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 8104470e3..48e192a59 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -44,10 +44,6 @@ ## * `std/sha1 module <sha1.html>`_ for a sha1 encoder and decoder ## * `tables module <tables.html>`_ for hash tables - -import - strutils - type Hash* = int ## A hash value. Hash tables using these values should ## always have a size of a power of two and can use the ``and`` diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index cef0d9607..9ae9819c3 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -176,7 +176,7 @@ ## import net, strutils, uri, parseutils, strtabs, base64, os, mimetypes, - math, random, httpcore, times, tables, streams + math, random, httpcore, times, tables, streams, std/monotimes import asyncnet, asyncdispatch, asyncfile import nativesockets @@ -610,7 +610,7 @@ type contentTotal: BiggestInt contentProgress: BiggestInt oneSecondProgress: BiggestInt - lastProgressReport: float + lastProgressReport: MonoTime when SocketType is AsyncSocket: bodyStream: FutureStream[string] parseBodyFut: Future[void] @@ -706,13 +706,13 @@ proc reportProgress(client: HttpClient | AsyncHttpClient, progress: BiggestInt) {.multisync.} = client.contentProgress += progress client.oneSecondProgress += progress - if epochTime() - client.lastProgressReport >= 1.0: + if (getMonoTime() - client.lastProgressReport).inSeconds > 1: if not client.onProgressChanged.isNil: await client.onProgressChanged(client.contentTotal, client.contentProgress, client.oneSecondProgress) client.oneSecondProgress = 0 - client.lastProgressReport = epochTime() + client.lastProgressReport = getMonoTime() proc recvFull(client: HttpClient | AsyncHttpClient, size: int, timeout: int, keep: bool): Future[int] {.multisync.} = @@ -784,7 +784,7 @@ proc parseBody(client: HttpClient | AsyncHttpClient, client.contentTotal = 0 client.contentProgress = 0 client.oneSecondProgress = 0 - client.lastProgressReport = 0 + client.lastProgressReport = MonoTime() when client is AsyncHttpClient: assert(not client.bodyStream.finished) diff --git a/lib/pure/json.nim b/lib/pure/json.nim index fa2ddb6f2..1ef08f547 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -142,8 +142,8 @@ runnableExamples: doAssert $(%* Foo()) == """{"a1":0,"a2":0,"a0":0,"a3":0,"a4":0}""" import - hashes, tables, strutils, lexbase, streams, unicode, macros, parsejson, - typetraits, options + hashes, tables, strutils, lexbase, streams, macros, parsejson, + options export tables.`$` diff --git a/lib/pure/net.nim b/lib/pure/net.nim index 8ae40a799..6e2f5d049 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -65,7 +65,8 @@ ## echo("Client connected from: ", address) {.deadCodeElim: on.} # dce option deprecated -import nativesockets, os, strutils, parseutils, times, sets, options +import nativesockets, os, strutils, parseutils, times, sets, options, + std/monotimes export Port, `$`, `==` export Domain, SockType, Protocol @@ -1077,7 +1078,7 @@ proc recv*(socket: Socket, data: pointer, size: int): int {.tags: [ReadIOEffect] # Save the error in case it gets reset. socket.lastError = osLastError() -proc waitFor(socket: Socket, waited: var float, timeout, size: int, +proc waitFor(socket: Socket, waited: var Duration, timeout, size: int, funcName: string): int {.tags: [TimeEffect].} = ## determines the amount of characters that can be read. Result will never ## be larger than ``size``. For unbuffered sockets this will be ``1``. @@ -1092,7 +1093,7 @@ proc waitFor(socket: Socket, waited: var float, timeout, size: int, result = socket.bufLen - socket.currPos result = min(result, size) else: - if timeout - int(waited * 1000.0) < 1: + if timeout - waited.inMilliseconds < 1: raise newException(TimeoutError, "Call to '" & funcName & "' timed out.") when defineSsl: @@ -1104,17 +1105,17 @@ proc waitFor(socket: Socket, waited: var float, timeout, size: int, if sslPending != 0: return min(sslPending, size) - var startTime = epochTime() - let selRet = select(socket, timeout - int(waited * 1000.0)) + var startTime = getMonoTime() + let selRet = select(socket, (timeout - waited.inMilliseconds).int) if selRet < 0: raiseOSError(osLastError()) if selRet != 1: raise newException(TimeoutError, "Call to '" & funcName & "' timed out.") - waited += (epochTime() - startTime) + waited += (getMonoTIme() - startTime) proc recv*(socket: Socket, data: pointer, size: int, timeout: int): int {. tags: [ReadIOEffect, TimeEffect].} = ## overload with a ``timeout`` parameter in milliseconds. - var waited = 0.0 # number of seconds already waited + var waited: Duration # duration already waited var read = 0 while read < size: @@ -1223,7 +1224,7 @@ proc readLine*(socket: Socket, line: var TaintedString, timeout = -1, if flags.isDisconnectionError(lastError): setLen(line.string, 0); return socket.socketError(n, lastError = lastError) - var waited = 0.0 + var waited: Duration setLen(line.string, 0) while true: @@ -1307,7 +1308,7 @@ proc skip*(socket: Socket, size: int, timeout = -1) = ## bytes takes longer than specified a TimeoutError exception will be raised. ## ## Returns the number of skipped bytes. - var waited = 0.0 + var waited: Duration var dummy = alloc(size) var bytesSkipped = 0 while bytesSkipped != size: diff --git a/lib/pure/os.nim b/lib/pure/os.nim index ee40a24c7..9ee6a4d4f 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1849,7 +1849,7 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1", break # getFullPathName doesn't do case corrections, so we have to use this convoluted # way of retrieving the true filename - for x in walkFiles(result.string): + for x in walkFiles(result): result = x if not existsFile(result) and not existsDir(result): raise newException(OSError, "file '" & result & "' does not exist") diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim index eb65b6f57..0d386bf18 100644 --- a/lib/pure/terminal.nim +++ b/lib/pure/terminal.nim @@ -19,7 +19,7 @@ import macros import strformat from strutils import toLowerAscii, `%` -import colors, tables +import colors when defined(windows): import winlean diff --git a/lib/pure/unicode.nim b/lib/pure/unicode.nim index bbd378740..35c364221 100644 --- a/lib/pure/unicode.nim +++ b/lib/pure/unicode.nim @@ -1281,7 +1281,7 @@ when isMainModule: compared = (someString == $someRunes) doAssert compared == true - proc test_replacements(word: string): string = + proc testReplacements(word: string): string = case word of "two": return "2" @@ -1294,8 +1294,8 @@ when isMainModule: else: return "12345" - doAssert translate("two not alpha foo βeta", test_replacements) == "2 12345 αlpha BAR beta" - doAssert translate(" two not foo βeta ", test_replacements) == " 2 12345 BAR beta " + doAssert translate("two not alpha foo βeta", testReplacements) == "2 12345 αlpha BAR beta" + doAssert translate(" two not foo βeta ", testReplacements) == " 2 12345 BAR beta " doAssert title("foo bar") == "Foo Bar" doAssert title("αlpha βeta γamma") == "Αlpha Βeta Γamma" diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index a6104428d..4cbd2f734 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -190,7 +190,7 @@ proc defaultConsoleFormatter*(): <//>ConsoleOutputFormatter = var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string var colorOutput = isatty(stdout) if existsEnv("NIMTEST_COLOR"): - let colorEnv = getenv("NIMTEST_COLOR") + let colorEnv = getEnv("NIMTEST_COLOR") if colorEnv == "never": colorOutput = false elif colorEnv == "always": @@ -349,7 +349,7 @@ proc glob(matcher, filter: string): bool = let beforeAndAfter = filter.split('*', maxsplit=1) if beforeAndAfter.len == 1: # "foo*" - return matcher.startswith(beforeAndAfter[0]) + return matcher.startsWith(beforeAndAfter[0]) if matcher.len < filter.len - 1: return false # "12345" should not match "123*345" @@ -366,8 +366,8 @@ proc matchFilter(suiteName, testName, filter: string): bool = if suiteAndTestFilters.len == 1: # no suite specified - let test_f = suiteAndTestFilters[0] - return glob(testName, test_f) + let testFilter = suiteAndTestFilters[0] + return glob(testName, testFilter) return glob(suiteName, suiteAndTestFilters[0]) and glob(testName, suiteAndTestFilters[1]) @@ -627,7 +627,7 @@ macro check*(conditions: untyped): untyped = # Ident !"v" # IntLit 2 result.check[i] = exp[i][1] - if exp[i].typekind notin {ntyTypeDesc}: + if exp[i].typeKind notin {ntyTypeDesc}: let arg = newIdentNode(":p" & $counter) result.assigns.add getAst(asgn(arg, paramAst)) result.printOuts.add getAst(print(argStr, arg)) @@ -640,7 +640,7 @@ macro check*(conditions: untyped): untyped = of nnkCallKinds: let (assigns, check, printOuts) = inspectArgs(checked) - let lineinfo = newStrLitNode(checked.lineinfo) + let lineinfo = newStrLitNode(checked.lineInfo) let callLit = checked.toStrLit result = quote do: block: @@ -657,7 +657,7 @@ macro check*(conditions: untyped): untyped = result.add(newCall(!"check", node)) else: - let lineinfo = newStrLitNode(checked.lineinfo) + let lineinfo = newStrLitNode(checked.lineInfo) let callLit = checked.toStrLit result = quote do: @@ -712,7 +712,7 @@ macro expect*(exceptions: varargs[typed], body: untyped): untyped = for i in countup(1, exp.len - 2): errorTypes.add(exp[i]) - result = getAst(expectBody(errorTypes, exp.lineinfo, body)) + result = getAst(expectBody(errorTypes, exp.lineInfo, body)) proc disableParamFiltering* = ## disables filtering tests with the command line params |