What does the `refTo` function do? Because it seem...
# kotlin-native
d
What does the
refTo
function do? Because it seems the target array is unpinned before the function returns.
j
It uses
usingPinned
not
usePinned
. And the implementation creates an
CValuesRef<>
object that asks for the pointer from the scope. https://github.com/JetBrains/kotlin-native/blob/master/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/Pinning.kt#L78
o
Think of it as temporary pinning for duration of the call
d
During the call of
refTo
?
Oh, hold on, does
refTo
simply return a value to be pinned in future? Like lazy/on-demand pinning?
o
nope,
Copy code
private inline fun <T : Any, P : CPointed> T.usingPinned(
        crossinline block: Pinned<T>.() -> CPointer<P>
) = object : CValuesRef<P>() {

    override fun getPointer(scope: AutofreeScope): CPointer<P> {
        val pinned = this@usingPinned.pin()
        scope.defer { pinned.unpin() }
        return pinned.block()
    }
}
, i.e. object is pinned until the current scope is active
d
Oh, sorry if I'm not getting this right. So if I call
refTo
but don't "consume" the result, i.e pass to cinterop. It will remain pinned. Until I "consume" it?
Nvm, I think I get it now.