From 6525f4ce1d1a7eba146e5f1a53f6f105077ae686 Mon Sep 17 00:00:00 2001 From: Eugene Kabanov Date: Wed, 2 Nov 2022 09:09:15 +0200 Subject: [PATCH] Fix some warnings. (#310) * Fix some warnings messages. * More warning fixes. * Address review comments. Fix MacOS issues. * More warning fixes. * More Windows specific fixes. * Fix macos and windows warnings. * Fix warnings in timer.nim Refactor to remove `result`. Improve performance and behavior of timer to string procedure. Add tests. --- chronos/asyncloop.nim | 4 +- chronos/handles.nim | 3 +- chronos/ioselects/ioselectors_epoll.nim | 2 +- chronos/streams/tlsstream.nim | 2 +- chronos/timer.nim | 201 ++++++++++++++---------- chronos/transports/common.nim | 2 +- chronos/transports/ipnet.nim | 6 +- chronos/transports/osnet.nim | 64 ++++---- chronos/transports/stream.nim | 4 +- tests/testmacro.nim | 4 +- tests/testtime.nim | 23 +++ 11 files changed, 182 insertions(+), 133 deletions(-) diff --git a/chronos/asyncloop.nim b/chronos/asyncloop.nim index 55340a338..f5cd5415f 100644 --- a/chronos/asyncloop.nim +++ b/chronos/asyncloop.nim @@ -237,11 +237,11 @@ func getAsyncTimestamp*(a: Duration): auto {.inline.} = var res = nansec div milsec let mid = nansec mod milsec when defined(windows): - res = min(cast[int64](high(int32) - 1), res) + res = min(int64(high(int32) - 1), res) result = cast[DWORD](res) result += DWORD(min(1'i32, cast[int32](mid))) else: - res = min(cast[int64](high(int32) - 1), res) + res = min(int64(high(int32) - 1), res) result = cast[int32](res) result += min(1, cast[int32](mid)) diff --git a/chronos/handles.nim b/chronos/handles.nim index 690ad9247..2f40095fb 100644 --- a/chronos/handles.nim +++ b/chronos/handles.nim @@ -13,11 +13,10 @@ else: {.push raises: [].} import std/[net, nativesockets] -import stew/base10 import ./asyncloop when defined(windows) or defined(nimdoc): - import os, winlean + import os, winlean, stew/base10 const asyncInvalidSocket* = AsyncFD(-1) TCP_NODELAY* = 1 diff --git a/chronos/ioselects/ioselectors_epoll.nim b/chronos/ioselects/ioselectors_epoll.nim index d1e138ad9..8207cd320 100644 --- a/chronos/ioselects/ioselectors_epoll.nim +++ b/chronos/ioselects/ioselectors_epoll.nim @@ -433,7 +433,7 @@ proc selectInto*[T](s: Selector[T], timeout: int, if posix.read(cint(fdi), addr data, sizeof(SignalFdInfo)) != sizeof(SignalFdInfo): raiseIOSelectorsError(osLastError()) - if cast[int](data.ssi_pid) == pkey.param: + if data.ssi_pid == uint32(pkey.param): rkey.events.incl(Event.Process) else: inc(i) diff --git a/chronos/streams/tlsstream.nim b/chronos/streams/tlsstream.nim index 00052af3b..a0d3c1aba 100644 --- a/chronos/streams/tlsstream.nim +++ b/chronos/streams/tlsstream.nim @@ -468,7 +468,7 @@ proc newTLSClientAsyncStream*(rsource: AsyncStreamReader, if TLSFlags.NoVerifyHost in flags: sslClientInitFull(res.ccontext, addr res.x509, nil, 0) - initNoAnchor(res.xwc, addr res.x509.vtable) + x509NoanchorInit(res.xwc, addr res.x509.vtable) sslEngineSetX509(res.ccontext.eng, addr res.xwc.vtable) else: sslClientInitFull(res.ccontext, addr res.x509, diff --git a/chronos/timer.nim b/chronos/timer.nim index 1a44189f7..f8668a9f9 100644 --- a/chronos/timer.nim +++ b/chronos/timer.nim @@ -22,6 +22,8 @@ ## This clock is slower then ``system`` clock. ## ## You can specify which timer you want to use ``-d:asyncTimer=``. +import stew/base10 + const asyncTimer* {.strdefine.} = "mono" when (NimMajor, NimMinor) < (1, 4): @@ -38,15 +40,15 @@ when defined(windows): ## Timer resolution is millisecond. var t: FILETIME getSystemTimeAsFileTime(t) - result = ((cast[uint64](t.dwHighDateTime) shl 32) or - cast[uint64](t.dwLowDateTime)) div 10_000 + ((cast[uint64](t.dwHighDateTime) shl 32) or + cast[uint64](t.dwLowDateTime)) div 10_000 proc fastEpochTimeNano(): uint64 {.inline.} = ## Timer resolution is nanosecond. var t: FILETIME getSystemTimeAsFileTime(t) - result = ((cast[uint64](t.dwHighDateTime) shl 32) or - cast[uint64](t.dwLowDateTime)) * 100 + ((cast[uint64](t.dwHighDateTime) shl 32) or + cast[uint64](t.dwLowDateTime)) * 100 else: proc QueryPerformanceCounter*(res: var uint64) {. @@ -61,14 +63,13 @@ when defined(windows): ## Procedure's resolution is nanosecond. var res: uint64 QueryPerformanceCounter(res) - result = res * queryFrequencyN + res * queryFrequencyN - proc fastEpochTime*(): uint64 {. - inline, deprecated: "Use Moment.now()".} = + proc fastEpochTime*(): uint64 {.inline, deprecated: "Use Moment.now()".} = ## Procedure's resolution is millisecond. var res: uint64 QueryPerformanceCounter(res) - result = res div queryFrequencyM + res div queryFrequencyM proc setupQueryFrequence() = var freq: uint64 @@ -94,15 +95,13 @@ elif defined(macosx): ## Procedure's resolution is millisecond. var t: Timeval posix_gettimeofday(t) - result = (cast[uint64](t.tv_sec) * 1_000 + - cast[uint64](t.tv_usec) div 1_000) + uint64(t.tv_sec) * 1_000 + uint64(t.tv_usec) div 1_000 proc fastEpochTimeNano(): uint64 {.inline.} = ## Procedure's resolution is nanosecond. var t: Timeval posix_gettimeofday(t) - result = (cast[uint64](t.tv_sec) * 1_000_000_000 + - cast[uint64](t.tv_usec) * 1_000) + uint64(t.tv_sec) * 1_000_000_000 + uint64(t.tv_usec) * 1_000 else: type MachTimebaseInfo {.importc: "struct mach_timebase_info", @@ -126,12 +125,12 @@ elif defined(macosx): proc fastEpochTime*(): uint64 {. inline, deprecated: "Use Moment.now()".} = ## Procedure's resolution is millisecond. - result = (mach_absolute_time() * queryFrequencyN) div queryFrequencyD - result = result div 1_000_000 + let res = (mach_absolute_time() * queryFrequencyN) div queryFrequencyD + res div 1_000_000 proc fastEpochTimeNano(): uint64 {.inline.} = ## Procedure's resolution is nanosecond. - result = (mach_absolute_time() * queryFrequencyN) div queryFrequencyD + (mach_absolute_time() * queryFrequencyN) div queryFrequencyD setupQueryFrequence() @@ -144,15 +143,13 @@ elif defined(posix): ## Procedure's resolution is millisecond. var t: Timespec discard clock_gettime(CLOCK_REALTIME, t) - result = (cast[uint64](t.tv_sec) * 1_000 + - (cast[uint64](t.tv_nsec) div 1_000_000)) + uint64(t.tv_sec) * 1_000 + (uint64(t.tv_nsec) div 1_000_000) proc fastEpochTimeNano(): uint64 {.inline.} = ## Procedure's resolution is nanosecond. var t: Timespec discard clock_gettime(CLOCK_REALTIME, t) - result = cast[uint64](t.tv_sec) * 1_000_000_000'u64 + - cast[uint64](t.tv_nsec) + uint64(t.tv_sec) * 1_000_000_000'u64 + uint64(t.tv_nsec) else: proc fastEpochTime*(): uint64 {. @@ -160,15 +157,13 @@ elif defined(posix): ## Procedure's resolution is millisecond. var t: Timespec discard clock_gettime(CLOCK_MONOTONIC, t) - result = (cast[uint64](t.tv_sec) * 1_000 + - (cast[uint64](t.tv_nsec) div 1_000_000)) + uint64(t.tv_sec) * 1_000 + (uint64(t.tv_nsec) div 1_000_000) proc fastEpochTimeNano(): uint64 {.inline.} = ## Procedure's resolution is nanosecond. var t: Timespec discard clock_gettime(CLOCK_MONOTONIC, t) - result = cast[uint64](t.tv_sec) * 1_000_000_000'u64 + - cast[uint64](t.tv_nsec) + uint64(t.tv_sec) * 1_000_000_000'u64 + uint64(t.tv_nsec) elif defined(nimdoc): @@ -196,15 +191,15 @@ else: func `+`*(a: Duration, b: Duration): Duration {.inline.} = ## Duration + Duration = Duration - result.value = a.value + b.value + Duration(value: a.value + b.value) func `+`*(a: Duration, b: Moment): Moment {.inline.} = ## Duration + Moment = Moment - result.value = a.value + b.value + Moment(value: a.value + b.value) func `+`*(a: Moment, b: Duration): Moment {.inline.} = ## Moment + Duration = Moment - result.value = a.value + b.value + Moment(value: a.value + b.value) func `+=`*(a: var Moment, b: Duration) {.inline.} = ## Moment += Duration @@ -218,79 +213,79 @@ func `-`*(a, b: Moment): Duration {.inline.} = ## Moment - Moment = Duration ## ## Note: Duration can't be negative. - result.value = if a.value >= b.value: a.value - b.value else: 0'i64 + Duration(value: if a.value >= b.value: a.value - b.value else: 0'i64) func `-`*(a: Moment, b: Duration): Moment {.inline.} = ## Moment - Duration = Moment ## ## Note: Moment can be negative - result.value = a.value - b.value + Moment(value: a.value - b.value) func `-`*(a: Duration, b: Duration): Duration {.inline.} = ## Duration - Duration = Duration ## ## Note: Duration can't be negative. - result.value = if a.value >= b.value: a.value - b.value else: 0'i64 + Duration(value: if a.value >= b.value: a.value - b.value else: 0'i64) -func `-=`*(a: var Duration, b: Duration): Duration {.inline.} = +func `-=`*(a: var Duration, b: Duration) {.inline.} = ## Duration -= Duration a.value = if a.value >= b.value: a.value - b.value else: 0'i64 -func `-=`*(a: var Moment, b: Duration): Moment {.inline.} = +func `-=`*(a: var Moment, b: Duration) {.inline.} = ## Moment -= Duration a.value -= b.value func `==`*(a, b: Duration): bool {.inline.} = ## Returns ``true`` if ``a`` equal to ``b``. - result = (a.value == b.value) + a.value == b.value func `==`*(a, b: Moment): bool {.inline.} = ## Returns ``true`` if ``a`` equal to ``b``. - result = (a.value == b.value) + a.value == b.value func `<`*(a, b: Duration): bool {.inline.} = ## Returns ``true`` if ``a`` less then ``b``. - result = (a.value < b.value) + a.value < b.value func `<`*(a, b: Moment): bool {.inline.} = ## Returns ``true`` if ``a`` less then ``b``. - result = (a.value < b.value) + a.value < b.value func `<=`*(a, b: Duration): bool {.inline.} = ## Returns ``true`` if ``a`` less or equal ``b``. - result = (a.value <= b.value) + a.value <= b.value func `<=`*(a, b: Moment): bool {.inline.} = ## Returns ``true`` if ``a`` less or equal ``b``. - result = (a.value <= b.value) + a.value <= b.value func `>`*(a, b: Duration): bool {.inline.} = ## Returns ``true`` if ``a`` bigger then ``b``. - result = (a.value > b.value) + a.value > b.value func `>`*(a, b: Moment): bool {.inline.} = ## Returns ``true`` if ``a`` bigger then ``b``. - result = (a.value > b.value) + a.value > b.value func `>=`*(a, b: Duration): bool {.inline.} = ## Returns ``true`` if ``a`` bigger or equal ``b``. - result = (a.value >= b.value) + a.value >= b.value func `>=`*(a, b: Moment): bool {.inline.} = ## Returns ``true`` if ``a`` bigger or equal ``b``. - result = (a.value >= b.value) + a.value >= b.value func `*`*(a: Duration, b: SomeIntegerI64): Duration {.inline.} = ## Returns Duration multiplied by scalar integer. - result.value = a.value * int64(b) + Duration(value: a.value * int64(b)) func `*`*(a: SomeIntegerI64, b: Duration): Duration {.inline.} = ## Returns Duration multiplied by scalar integer. - result.value = int64(a) * b.value + Duration(value: int64(a) * b.value) func `div`*(a: Duration, b: SomeIntegerI64): Duration {.inline.} = ## Returns Duration which is result of dividing a Duration by scalar integer. - result.value = a.value div int64(b) + Duration(value: a.value div int64(b)) const Nanosecond* = Duration(value: 1'i64) @@ -319,139 +314,167 @@ template low*(T: typedesc[Duration]): Duration = func nanoseconds*(v: SomeIntegerI64): Duration {.inline.} = ## Initialize Duration with nanoseconds value ``v``. - result.value = int64(v) + Duration(value: int64(v)) func microseconds*(v: SomeIntegerI64): Duration {.inline.} = ## Initialize Duration with microseconds value ``v``. - result.value = int64(v) * Microsecond.value + Duration(value: int64(v) * Microsecond.value) func milliseconds*(v: SomeIntegerI64): Duration {.inline.} = ## Initialize Duration with milliseconds value ``v``. - result.value = int64(v) * Millisecond.value + Duration(value: int64(v) * Millisecond.value) func seconds*(v: SomeIntegerI64): Duration {.inline.} = ## Initialize Duration with seconds value ``v``. - result.value = int64(v) * Second.value + Duration(value: int64(v) * Second.value) func minutes*(v: SomeIntegerI64): Duration {.inline.} = ## Initialize Duration with minutes value ``v``. - result.value = int64(v) * Minute.value + Duration(value: int64(v) * Minute.value) func hours*(v: SomeIntegerI64): Duration {.inline.} = ## Initialize Duration with hours value ``v``. - result.value = int64(v) * Hour.value + Duration(value: int64(v) * Hour.value) func days*(v: SomeIntegerI64): Duration {.inline.} = ## Initialize Duration with days value ``v``. - result.value = int64(v) * Day.value + Duration(value: int64(v) * Day.value) func weeks*(v: SomeIntegerI64): Duration {.inline.} = ## Initialize Duration with weeks value ``v``. - result.value = int64(v) * Week.value + Duration(value: int64(v) * Week.value) func nanoseconds*(v: Duration): int64 {.inline.} = ## Round Duration ``v`` to nanoseconds. - result = v.value + v.value func microseconds*(v: Duration): int64 {.inline.} = ## Round Duration ``v`` to microseconds. - result = v.value div Microsecond.value + v.value div Microsecond.value func milliseconds*(v: Duration): int64 {.inline.} = ## Round Duration ``v`` to milliseconds. - result = v.value div Millisecond.value + v.value div Millisecond.value func seconds*(v: Duration): int64 {.inline.} = ## Round Duration ``v`` to seconds. - result = v.value div Second.value + v.value div Second.value func minutes*(v: Duration): int64 {.inline.} = ## Round Duration ``v`` to minutes. - result = v.value div Minute.value + v.value div Minute.value func hours*(v: Duration): int64 {.inline.} = ## Round Duration ``v`` to hours. - result = v.value div Hour.value + v.value div Hour.value func days*(v: Duration): int64 {.inline.} = ## Round Duration ``v`` to days. - result = v.value div Day.value + v.value div Day.value func weeks*(v: Duration): int64 {.inline.} = ## Round Duration ``v`` to weeks. - result = v.value div Week.value + v.value div Week.value func nanos*(v: SomeIntegerI64): Duration {.inline.} = - result = nanoseconds(v) + nanoseconds(v) func micros*(v: SomeIntegerI64): Duration {.inline.} = - result = microseconds(v) + microseconds(v) func millis*(v: SomeIntegerI64): Duration {.inline.} = - result = milliseconds(v) + milliseconds(v) func secs*(v: SomeIntegerI64): Duration {.inline.} = - result = seconds(v) + seconds(v) func nanos*(v: Duration): int64 {.inline.} = - result = nanoseconds(v) + nanoseconds(v) func micros*(v: Duration): int64 {.inline.} = - result = microseconds(v) + microseconds(v) func millis*(v: Duration): int64 {.inline.} = - result = milliseconds(v) + milliseconds(v) func secs*(v: Duration): int64 {.inline.} = - result = seconds(v) + seconds(v) + +template add(a: var string, b: Base10Buf[uint64]) = + for index in 0 ..< b.len: + a.add(char(b.data[index])) func `$`*(a: Duration): string {.inline.} = ## Returns string representation of Duration ``a`` as nanoseconds value. + var res = "" var v = a.value + if v >= Week.value: - result = $(v div Week.value) & "w" + res.add(Base10.toBytes(uint64(v div Week.value))) + res.add('w') v = v mod Week.value + if v == 0: return res if v >= Day.value: - result &= $(v div Day.value) & "d" + res.add(Base10.toBytes(uint64(v div Day.value))) + res.add('d') v = v mod Day.value + if v == 0: return res if v >= Hour.value: - result &= $(v div Hour.value) & "h" + res.add(Base10.toBytes(uint64(v div Hour.value))) + res.add('h') v = v mod Hour.value + if v == 0: return res if v >= Minute.value: - result &= $(v div Minute.value) & "m" + res.add(Base10.toBytes(uint64(v div Minute.value))) + res.add('m') v = v mod Minute.value + if v == 0: return res if v >= Second.value: - result &= $(v div Second.value) & "s" + res.add(Base10.toBytes(uint64(v div Second.value))) + res.add('s') v = v mod Second.value + if v == 0: return res if v >= Millisecond.value: - result &= $(v div Millisecond.value) & "ms" + res.add(Base10.toBytes(uint64(v div Millisecond.value))) + res.add('m') + res.add('s') v = v mod Millisecond.value + if v == 0: return res if v >= Microsecond.value: - result &= $(v div Microsecond.value) & "us" + res.add(Base10.toBytes(uint64(v div Microsecond.value))) + res.add('u') + res.add('s') v = v mod Microsecond.value - result &= $(v div Nanosecond.value) & "ns" + if v == 0: return res + res.add(Base10.toBytes(uint64(v div Nanosecond.value))) + res.add('n') + res.add('s') + res func `$`*(a: Moment): string {.inline.} = ## Returns string representation of Moment ``a`` as nanoseconds value. - result = $(a.value) - result.add("ns") + var res = "" + res.add(Base10.toBytes(uint64(a.value))) + res.add('n') + res.add('s') + res func isZero*(a: Duration): bool {.inline.} = ## Returns ``true`` if Duration ``a`` is ``0``. - result = (a.value == 0) + a.value == 0 func isInfinite*(a: Duration): bool {.inline.} = ## Returns ``true`` if Duration ``a`` is infinite. - result = (a.value == InfiniteDuration.value) + a.value == InfiniteDuration.value proc now*(t: typedesc[Moment]): Moment {.inline.} = ## Returns current moment in time as Moment. - result.value = int64(fastEpochTimeNano()) + Moment(value: int64(fastEpochTimeNano())) func init*(t: typedesc[Moment], value: int64, precision: Duration): Moment = ## Initialize Moment with absolute time value ``value`` with precision ## ``precision``. - result.value = value * precision.value + Moment(value: value * precision.value) func epochSeconds*(moment: Moment): int64 = moment.value div Second.value @@ -461,7 +484,7 @@ func epochNanoSeconds*(moment: Moment): int64 = proc fromNow*(t: typedesc[Moment], a: Duration): Moment {.inline.} = ## Returns moment in time which is equal to current moment + Duration. - result = Moment.now() + a + Moment.now() + a when defined(posix): from posix import Time, Suseconds, Timeval, Timespec @@ -469,10 +492,14 @@ when defined(posix): func toTimeval*(a: Duration): Timeval = ## Convert Duration ``a`` to ``Timeval`` object. let m = a.value mod Second.value - result.tv_sec = Time(a.value div Second.value) - result.tv_usec = Suseconds(m div Microsecond.value) + Timeval( + tv_sec: Time(a.value div Second.value), + tv_usec: Suseconds(m div Microsecond.value) + ) func toTimespec*(a: Duration): Timespec = ## Convert Duration ``a`` to ``Timespec`` object. - result.tv_sec = Time(a.value div Second.value) - result.tv_nsec = int(a.value mod Second.value) + Timespec( + tv_sec: Time(a.value div Second.value), + tv_nsec: int(a.value mod Second.value) + ) diff --git a/chronos/transports/common.nim b/chronos/transports/common.nim index bc9cc1c07..364097129 100644 --- a/chronos/transports/common.nim +++ b/chronos/transports/common.nim @@ -296,7 +296,7 @@ proc getAddrInfo(address: string, port: Port, domain: Domain, hints.ai_family = toInt(domain) hints.ai_socktype = toInt(sockType) hints.ai_protocol = toInt(protocol) - var gaiRes = getaddrinfo(address, Base10.toString(uint16(port)), + var gaiRes = getaddrinfo(address, cstring(Base10.toString(uint16(port))), addr(hints), res) if gaiRes != 0'i32: when defined(windows) or defined(nimdoc): diff --git a/chronos/transports/ipnet.nim b/chronos/transports/ipnet.nim index ec7c690fb..af7469e6d 100644 --- a/chronos/transports/ipnet.nim +++ b/chronos/transports/ipnet.nim @@ -316,7 +316,7 @@ proc `$`*(mask: IpMask, include0x = false): string = var m = host.mask4 while n > 0: n -= 4 - var c = cast[int]((m shr n) and 0x0F) + var c = int((m shr n) and 0x0F) if c < 10: result.add(chr(ord('0') + c)) else: @@ -328,7 +328,7 @@ proc `$`*(mask: IpMask, include0x = false): string = var m = host.mask6[i] while n > 0: n -= 4 - var c = cast[int]((m shr n) and 0x0F) + var c = int((m shr n) and 0x0F) if c < 10: result.add(chr(ord('0') + c)) else: @@ -639,7 +639,7 @@ proc isLoopback*(address: TransportAddress): bool = elif address.family == AddressFamily.IPv6: var test = 0 for i in 0..<(len(address.address_v6) - 1): - test = test or cast[int](address.address_v6[i]) + test = test or int(address.address_v6[i]) result = (test == 0) and (address.address_v6[15] == 1'u8) proc isAnyLocal*(address: TransportAddress): bool = diff --git a/chronos/transports/osnet.nim b/chronos/transports/osnet.nim index 62569ac41..d3883696e 100644 --- a/chronos/transports/osnet.nim +++ b/chronos/transports/osnet.nim @@ -247,7 +247,7 @@ type ifType*: InterfaceType name*: string desc*: string - mtu*: int + mtu*: int64 flags*: uint64 state*: InterfaceState mac*: array[MaxAdapterAddressLength, byte] @@ -310,7 +310,7 @@ proc `$`*(iface: NetworkInterface): string = res.add($iface.ifType) res.add(" ") if iface.maclen > 0: - for i in 0..""".} proc toInterfaceType(f: byte): InterfaceType = - var ft = cast[int](f) + var ft = int(f) if (ft >= 1 and ft <= 196) or (ft == 237) or (ft == 243) or (ft == 244): cast[InterfaceType](ft) else: @@ -1070,8 +1070,8 @@ elif defined(macosx) or defined(bsd): var iface: NetworkInterface var ifaddress: InterfaceAddress - iface.name = $ifap.ifa_name - iface.flags = cast[uint64](ifap.ifa_flags) + iface.name = string($cstring(ifap.ifa_name)) + iface.flags = uint64(ifap.ifa_flags) var i = 0 while i < len(res): if res[i].name == iface.name: @@ -1081,19 +1081,19 @@ elif defined(macosx) or defined(bsd): res.add(iface) if not isNil(ifap.ifa_addr): - let family = cast[int](ifap.ifa_addr.sa_family) + let family = int(ifap.ifa_addr.sa_family) if family == AF_LINK: var data = cast[ptr IfData](ifap.ifa_data) var link = cast[ptr Sockaddr_dl](ifap.ifa_addr) - res[i].ifIndex = cast[int](link.sdl_index) - let nlen = cast[int](link.sdl_nlen) + res[i].ifIndex = int(link.sdl_index) + let nlen = int(link.sdl_nlen) if nlen < len(link.sdl_data): let minsize = min(cast[int](link.sdl_alen), len(res[i].mac)) copyMem(addr res[i].mac[0], addr link.sdl_data[nlen], minsize) - res[i].maclen = cast[int](link.sdl_alen) + res[i].maclen = int(link.sdl_alen) res[i].ifType = toInterfaceType(data.ifi_type) res[i].state = toInterfaceState(ifap.ifa_flags) - res[i].mtu = cast[int](data.ifi_mtu) + res[i].mtu = int(data.ifi_mtu) elif family == posix.AF_INET: fromSAddr(cast[ptr Sockaddr_storage](ifap.ifa_addr), SockLen(sizeof(Sockaddr_in)), ifaddress.host) @@ -1102,7 +1102,7 @@ elif defined(macosx) or defined(bsd): SockLen(sizeof(Sockaddr_in6)), ifaddress.host) if not isNil(ifap.ifa_netmask): var na: TransportAddress - var family = cast[cint](ifap.ifa_netmask.sa_family) + var family = cint(ifap.ifa_netmask.sa_family) if family == posix.AF_INET: fromSAddr(cast[ptr Sockaddr_storage](ifap.ifa_netmask), SockLen(sizeof(Sockaddr_in)), na) @@ -1443,13 +1443,13 @@ elif defined(windows): fromSAddr(cast[ptr Sockaddr_storage](prefix.address.lpSockaddr), SockLen(prefix.address.iSockaddrLength), pa) if netfamily == prefamily: - if ipMatchPrefix(res.host, pa, cast[int](prefix.prefixLength)): - prefixLength = max(prefixLength, cast[int](prefix.prefixLength)) + if ipMatchPrefix(res.host, pa, int(prefix.prefixLength)): + prefixLength = max(prefixLength, int(prefix.prefixLength)) prefix = prefix.next if prefixLength >= 0: res.net = IpNet.init(res.host, prefixLength) else: - let prefixLength = cast[int](ifunic.onLinkPrefixLength) + let prefixLength = int(ifunic.onLinkPrefixLength) if prefixLength >= 0: res.net = IpNet.init(res.host, prefixLength) res @@ -1483,14 +1483,14 @@ elif defined(windows): var slider = cast[ptr IpAdapterAddressesXp](addr buffer[0]) while not isNil(slider): var iface = NetworkInterface( - ifIndex: cast[int](slider.ifIndex), + ifIndex: int(slider.ifIndex), ifType: toInterfaceType(slider.ifType), state: toInterfaceState(slider.operStatus), name: $slider.adapterName, desc: $slider.description, - mtu: cast[int](slider.mtu), - maclen: cast[int](slider.physicalAddressLength), - flags: cast[uint64](slider.flags) + mtu: int(slider.mtu), + maclen: int(slider.physicalAddressLength), + flags: uint64(slider.flags) ) copyMem(addr iface.mac[0], addr slider.physicalAddress[0], len(iface.mac)) diff --git a/chronos/transports/stream.nim b/chronos/transports/stream.nim index 3ec133139..832ea1b0e 100644 --- a/chronos/transports/stream.nim +++ b/chronos/transports/stream.nim @@ -199,8 +199,8 @@ template shiftVectorBuffer(v: var StreamVector, o: untyped) = (v).buflen -= int(o) template shiftVectorFile(v: var StreamVector, o: untyped) = - (v).buf = cast[pointer](cast[uint]((v).buf) - cast[uint](o)) - (v).offset += cast[uint]((o)) + (v).buf = cast[pointer](cast[uint]((v).buf) - uint(o)) + (v).offset += uint(o) proc setupStreamTransportTracker(): StreamTransportTracker {. gcsafe, raises: [Defect].} diff --git a/tests/testmacro.nim b/tests/testmacro.nim index f834ce329..22c479af0 100644 --- a/tests/testmacro.nim +++ b/tests/testmacro.nim @@ -105,7 +105,7 @@ suite "Macro transformations test suite": macroAsync2(testMacro2, seq, Opt, Result, OpenObject, cstring) check waitFor(testMacro2()).len == 0 -suite "async transformation issues": +suite "Closure iterator's exception transformation issues": test "Nested defer/finally not called on return": # issue #288 # fixed by https://github.com/nim-lang/Nim/pull/19933 @@ -113,7 +113,7 @@ suite "async transformation issues": proc a {.async.} = try: try: - await sleepAsync(0) + await sleepAsync(0.milliseconds) return finally: answer = 32 diff --git a/tests/testtime.nim b/tests/testtime.nim index a66d1291d..430db8f8b 100644 --- a/tests/testtime.nim +++ b/tests/testtime.nim @@ -67,6 +67,29 @@ suite "Asynchronous timers & steps test suite": test $TimersCount & " timers with 1000ms timeout": var res = waitFor(test(1000.milliseconds)) check (res >= 1000.milliseconds) and (res <= 5000.milliseconds) + test "Timer stringification test": + check: + $weeks(1) == "1w" + $days(1) == "1d" + $hours(1) == "1h" + $minutes(1) == "1m" + $seconds(1) == "1s" + $milliseconds(1) == "1ms" + $microseconds(1) == "1us" + $nanoseconds(1) == "1ns" + $(weeks(1) + days(1)) == "1w1d" + $(days(1) + hours(1)) == "1d1h" + $(hours(1) + minutes(1)) == "1h1m" + $(minutes(1) + seconds(1)) == "1m1s" + $(seconds(1) + milliseconds(1)) == "1s1ms" + $(milliseconds(1) + microseconds(1)) == "1ms1us" + $nanoseconds(1_000_000_000) == "1s" + $nanoseconds(1_900_000_000) == "1s900ms" + $nanoseconds(1_000_900_000) == "1s900us" + $nanoseconds(1_000_000_900) == "1s900ns" + $nanoseconds(1_800_700_000) == "1s800ms700us" + $nanoseconds(1_800_000_600) == "1s800ms600ns" + test "Asynchronous steps test": var futn1 = stepsAsync(-1) var fut0 = stepsAsync(0)