Scala Native - How to convert Int to Ptr[CInt]?

52 views Asked by At

What's the right way to convert VM's Int to unmanaged CInt pointer from scala.scalanative.unsafe?

val num: Int = 5
val nativeInt: Ptr[CInt] = // ?
1

There are 1 answers

0
R A On BEST ANSWER

This seems to be working for me:

def toCIntPtr(num: Int)(using z: Zone): Ptr[CInt] = {
    val nativeInt: Ptr[CInt] = alloc[CInt](sizeof[CInt])
    !nativeInt = num
    nativeInt
}

// implicit zone present
val num: Int = 5
val nativeInt: Ptr[CInt] = toCIntPtr(num)