typetraits: add toSigned, toUnsigned (#18445)
* typetraits: add toSigned, toUnsigned * improve and add tests Co-authored-by: Andreas Rumpf <rumpf_a@web.de> Co-authored-by: flywind <xzsflywind@gmail.com>
This commit is contained in:
parent
1807de38e5
commit
e78ef57c93
7 changed files with 85 additions and 33 deletions
|
|
@ -10,8 +10,13 @@ template forwardImpl*(impl, arg) {.dirty.} =
|
|||
else:
|
||||
impl(x.uint64)
|
||||
|
||||
template toUnsigned*(x: int8): uint8 = cast[uint8](x)
|
||||
template toUnsigned*(x: int16): uint16 = cast[uint16](x)
|
||||
template toUnsigned*(x: int32): uint32 = cast[uint32](x)
|
||||
template toUnsigned*(x: int64): uint64 = cast[uint64](x)
|
||||
template toUnsigned*(x: int): uint = cast[uint](x)
|
||||
# this could also be implemented via:
|
||||
# import std/typetraits
|
||||
# template castToUnsigned*(x: SomeInteger): auto = cast[toUnsigned(typeof(x))](x)
|
||||
|
||||
template castToUnsigned*(x: int8): uint8 = cast[uint8](x)
|
||||
template castToUnsigned*(x: int16): uint16 = cast[uint16](x)
|
||||
template castToUnsigned*(x: int32): uint32 = cast[uint32](x)
|
||||
template castToUnsigned*(x: int64): uint64 = cast[uint64](x)
|
||||
template castToUnsigned*(x: int): uint = cast[uint](x)
|
||||
template castToUnsigned*[T: SomeUnsignedInt](x: T): T = x
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue