Hi guys, I have a question regarding `Object pinn...
# kotlin-native
v
Hi guys, I have a question regarding
Object pinning
. According to the documentation this can be done as follows:
Copy code
fun readData(fd: Int): String {
  val buffer = ByteArray(1024)
  buffer.usePinned { pinned ->
    while (true) {
      val length = recv(fd, pinned.addressOf(0), buffer.size.convert(), 0).toInt()
      if (length <= 0) {
        break
     }
     // Now `buffer` has raw data obtained from the `recv()` call.
   }
 }
}
What about if I want to use it on something else than a buffer. For example on
cValue<cairo_text_extents_t>
where
cairo_text_extents_t
is:
Copy code
@kotlinx.cinterop.internal.CStruct public final class cairo_text_extents_t public constructor(rawPtr: kotlinx.cinterop.NativePtr /* = kotlin.native.internal.NativePtr */) : kotlinx.cinterop.CStructVar {
    public companion object : kotlinx.cinterop.CStructVar.Type {
    }

    public final var height: kotlin.Double /* compiled code */

    public final var width: kotlin.Double /* compiled code */

    public final var x_advance: kotlin.Double /* compiled code */

    public final var x_bearing: kotlin.Double /* compiled code */

    public final var y_advance: kotlin.Double /* compiled code */

    public final var y_bearing: kotlin.Double /* compiled code */
}
and the function to which I want to provide it is:
Copy code
@kotlinx.cinterop.internal.CCall public external fun cairo_glyph_extents(cr: kotlinx.cinterop.CValuesRef<libcairo.cairo_t /* = cnames.structs._cairo */>?, glyphs: kotlinx.cinterop.CValuesRef<libcairo.cairo_glyph_t>?, num_glyphs: <http://kotlin.Int|kotlin.Int>, extents: kotlinx.cinterop.CValuesRef<libcairo.cairo_text_extents_t>?): kotlin.Unit { /* compiled code */ }
My problem is that
cValue<cairo_text_extents_t>
does not have the function
addressOf
. I provide a small code snippet to show what I mean:
Copy code
val glyph = cValue<cairo_glyph_t>()
val extents = cValue<cairo_text_extents_t>()
extents.usePinned {pinned ->
	memScoped {
		cairo_glyph_extents(context.cairo, glyph.ptr, 1, pinned.<what>) // how to get the address 
	}
}
Does anybody know how I can do this? Thanks in advance!
d
In the case of structs, you don't/can't pin them. Pinning is fairly limited. The values returned by
cValue
are considered immutable, so you won't be able to mutate it like that.
You can achieve what you want by either using the
copy
function or using
cValue<cairo_text_extents_t> { // mutate here }
to mutate it at initialisation time.
v
Thanks!