Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Define an extension method for conversion .toCSize as an alias to .toUSize #3808

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package scala.scalanative.unsigned

import scala.scalanative.runtime.Intrinsics._
import scala.scalanative.unsafe.CSize

/** Scala Native unsigned extensions to the standard Byte. */
extension (inline value: Byte) {
inline def toUByte: UByte = unsignedOf(value)
inline def toUShort: UShort = unsignedOf(value.toShort)
inline def toUInt: UInt = unsignedOf(byteToUInt(value))
inline def toULong: ULong = unsignedOf(byteToULong(value))
inline def toUSize: CSize = unsignedOf(
castIntToRawSizeUnsigned(byteToUInt(value))
)
inline def toCSize: CSize = toUSize
}

/** Scala Native unsigned extensions to the standard Short. */
Expand All @@ -16,6 +21,10 @@ extension (inline value: Short) {
inline def toUShort: UShort = unsignedOf(value)
inline def toUInt: UInt = unsignedOf(shortToUInt(value))
inline def toULong: ULong = unsignedOf(shortToULong(value))
inline def toUSize: USize = unsignedOf(
castIntToRawSizeUnsigned(shortToUInt((value)))
)
inline def toCSize: CSize = toUSize
}

/** Scala Native unsigned extensions to the standard Int. */
Expand All @@ -25,6 +34,7 @@ extension (inline value: Int) {
inline def toUInt: UInt = unsignedOf(value)
inline def toULong: ULong = unsignedOf(intToULong(value))
inline def toUSize: USize = unsignedOf(castIntToRawSizeUnsigned(value))
inline def toCSize: CSize = toUSize
}

/** Scala Native unsigned extensions to the standard Long. */
Expand All @@ -34,4 +44,5 @@ extension (inline value: Long) {
inline def toUInt: UInt = unsignedOf(value.toInt)
inline def toULong: ULong = unsignedOf(value)
inline def toUSize: USize = unsignedOf(castLongToRawSize(value))
inline def toCSize: CSize = toUSize
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ object Array {
val fromPtr = from.atRawUnsafe(fromPos)
val toPtr = to.atRawUnsafe(toPos)
val size = to.stride * len
libc.memmove(toPtr, fromPtr, castIntToRawSizeUnsigned(size))
ffi.memmove(toPtr, fromPtr, castIntToRawSizeUnsigned(size))
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ object Array {
} else {
val leftPtr = left.atRaw(leftPos)
val rightPtr = right.atRaw(rightPos)
libc.memcmp(leftPtr, rightPtr, castIntToRawSizeUnsigned(len * left.stride))
ffi.memcmp(leftPtr, rightPtr, castIntToRawSizeUnsigned(len * left.stride))
}
}
}
Expand Down Expand Up @@ -229,7 +229,7 @@ final class ${T}Array private () extends Array[${Repr}] {
val arrcls = classOf[${T}Array]
val arr = GC.alloc_array(arrcls, length, ${sizeT})
val src = castObjectToRawPtr(this)
libc.memcpy(
ffi.memcpy(
elemRawPtr(arr, MemoryLayout.Array.ValuesOffset),
elemRawPtr(src, MemoryLayout.Array.ValuesOffset),
castIntToRawSizeUnsigned(${sizeT} * length)
Expand Down Expand Up @@ -280,7 +280,7 @@ object ${T}Array {
val dst = arr.atRawUnsafe(0)
val src = data
val size = castIntToRawSizeUnsigned(${sizeT} * length)
libc.memcpy(dst, src, size)
ffi.memcpy(dst, src, size)
}
arr
}
Expand Down
2 changes: 2 additions & 0 deletions nativelib/src/main/scala/scala/scalanative/unsafe/Size.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ final class Size(private[scalanative] val rawSize: RawSize)
@inline def toUInt: UInt = toUSize.toUInt
@inline def toULong: ULong = toUSize.toULong
@inline def toUSize: USize = USize.valueOf(rawSize)
@inline def toCSize: CSize = toUSize
@inline def toCSSize: CSSize = this


@inline override def doubleValue(): Double = toLong.toDouble
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ final class Size(private[scalanative] val rawSize: RawSize)
@inline def toUInt: UInt = toUSize.toUInt
@inline def toULong: ULong = toUSize.toULong
@inline def toUSize: USize = USize.valueOf(rawSize)
@inline def toCSize: CSize = toUSize
@inline def toCSSize: CSSize = this


@inline override def doubleValue(): Double = toLong.toDouble
Expand Down
2 changes: 2 additions & 0 deletions nativelib/src/main/scala/scala/scalanative/unsafe/Tag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ object Tag {
private[scalanative] sealed trait NatTag {
def toInt: Int
def toUInt: UInt = toInt.toUInt
def toCSize: CSize = toInt.toCSize
def toCSSize: CSSize = toInt.toCSSize
}

object Nat0 extends Tag[unsafe.Nat._0] with NatTag {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ object Tag {
private[scalanative] sealed trait NatTag {
def toInt: Int
def toUInt: UInt = toInt.toUInt
def toCSize: CSize = toInt.toCSize
def toCSSize: CSSize = toInt.toCSSize
}

% for N in range(0, 10):
Expand Down Expand Up @@ -157,7 +159,7 @@ object Tag {
val dst = rawptr
if (value != null) {
val src = value.rawptr
libc.memcpy(dst, src, castIntToRawSizeUnsigned(size))
ffi.memcpy(dst, src, castIntToRawSizeUnsigned(size))
} else storeRawPtr(dst, null)
}
}
Expand Down Expand Up @@ -214,7 +216,7 @@ object Tag {
val dst = rawptr
if (value != null) {
val src = value.rawptr
libc.memcpy(dst, src, castIntToRawSizeUnsigned(size))
ffi.memcpy(dst, src, castIntToRawSizeUnsigned(size))
} else storeRawPtr(dst, null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,27 @@ package object unsafe extends unsafe.UnsafePackageCompat {
/** Scala Native unsafe extensions to the standard Byte. */
implicit class UnsafeRichByte(val value: Byte) extends AnyVal {
@inline def toSize: Size = Size.valueOf(castIntToRawSize(value.toInt))
@inline def toCSSize: CSSize = toSize
}

/** Scala Native unsafe extensions to the standard Short. */
implicit class UnsafeRichShort(val value: Short) extends AnyVal {
@inline def toSize: Size = Size.valueOf(castIntToRawSize(value.toInt))
@inline def toCSSize: CSSize = toSize
}

/** Scala Native unsafe extensions to the standard Int. */
implicit class UnsafeRichInt(val value: Int) extends AnyVal {
@inline def toPtr[T]: Ptr[T] = fromRawPtr[T](castIntToRawPtr(value))
@inline def toSize: Size = Size.valueOf(castIntToRawSize(value))
@inline def toCSSize: CSSize = toSize
}

/** Scala Native unsafe extensions to the standard Long. */
implicit class UnsafeRichLong(val value: Long) extends AnyVal {
@inline def toPtr[T]: Ptr[T] = fromRawPtr[T](castLongToRawPtr(value))
@inline def toSize: Size = Size.valueOf(castLongToRawSize(value))
@inline def toCSSize: CSSize = toSize
}

/** Scala Native unsafe extensions to Arrays */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ final class USize(private[scalanative] val rawSize: RawSize) extends scala.math.
@inline def toLong: Long = castRawSizeToLongUnsigned(rawSize)
@inline def toSize: unsafe.Size = new unsafe.Size(rawSize)

@inline def toCSize: unsafe.CSize = this
@inline def toCSSize: unsafe.CSSize = toSize

@inline def toUByte: UByte = toByte.toUByte
@inline def toUShort: UShort = toShort.toUShort
@inline def toUInt: UInt = unsignedOf(castRawSizeToInt(rawSize))
@inline def toULong: ULong = unsignedOf(castRawSizeToLongUnsigned(rawSize))
@inline def toUSize: USize = this

@inline override def doubleValue(): Double = toLong.toDouble
@inline override def floatValue(): Float = toInt.toFloat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ final class USize(private[scalanative] val rawSize: RawSize) extends scala.math.
@inline def toLong: Long = castRawSizeToLongUnsigned(rawSize)
@inline def toSize: unsafe.Size = new unsafe.Size(rawSize)

@inline def toCSize: unsafe.CSize = this
@inline def toCSSize: unsafe.CSSize = toSize

@inline def toUByte: UByte = toByte.toUByte
@inline def toUShort: UShort = toShort.toUShort
@inline def toUInt: UInt = unsignedOf(castRawSizeToInt(rawSize))
@inline def toULong: ULong = unsignedOf(castRawSizeToLongUnsigned(rawSize))
@inline def toUSize: USize = this

@inline override def doubleValue(): Double = toLong.toDouble
@inline override def floatValue(): Float = toInt.toFloat
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scala.scalanative

import scala.scalanative.runtime.Intrinsics._
import scala.scalanative.unsafe.CSize

package object unsigned {

Expand All @@ -12,6 +13,9 @@ package object unsigned {
@inline def toUShort: UShort = unsignedOf(value.toShort)
@inline def toUInt: UInt = unsignedOf(byteToUInt(value))
@inline def toULong: ULong = unsignedOf(byteToULong(value))
@inline def toCSize: CSize = unsignedOf(
castIntToRawSizeUnsigned(byteToUInt(value))
)
}

/** Scala Native unsigned extensions to the standard Short. */
Expand All @@ -20,6 +24,9 @@ package object unsigned {
@inline def toUShort: UShort = unsignedOf(value)
@inline def toUInt: UInt = unsignedOf(shortToUInt(value))
@inline def toULong: ULong = unsignedOf(shortToULong(value))
@inline def toCSize: CSize = unsignedOf(
castIntToRawSizeUnsigned(shortToUInt(value))
)
}

/** Scala Native unsigned extensions to the standard Int. */
Expand All @@ -29,6 +36,7 @@ package object unsigned {
@inline def toUInt: UInt = unsignedOf(value)
@inline def toULong: ULong = unsignedOf(intToULong(value))
@inline def toUSize: USize = unsignedOf(castIntToRawSizeUnsigned(value))
@inline def toCSize: CSize = toUSize
}

/** Scala Native unsigned extensions to the standard Long. */
Expand All @@ -38,5 +46,6 @@ package object unsigned {
@inline def toUInt: UInt = unsignedOf(value.toInt)
@inline def toULong: ULong = unsignedOf(value)
@inline def toUSize: USize = unsignedOf(castLongToRawSize(value))
@inline def toCSize: CSize = toUSize
}
}