Search code examples
scala-native

Scala Native - Convert Array[Byte] to Ptr[Byte]


I'm trying to pass a Scala array to native code and need to do the conversion. How to convert Array[Byte] to Ptr[Byte] in Scala Native?


Solution

  • My best solution so far is this:

    def makeNativeArray(input: Array[Byte])(using z: Zone): Ptr[Byte] =
      val rawSize = input.length
      val buffer = alloc[Byte](rawSize)
      for i <- 0 until rawSize
      do
        buffer(i) = input(i)
      buffer