Attempting to convert UnsafeMutableRawPointer back into a recognizable type

177 views Asked by At

TL;DR I'm attempting to convert UnsafeMutableRawPointer into some UnsafePointer<T>.


I've developed a Minimal Reproducible Example of my program (Here's a snippet)

var foo: Int = 100
var bar: String = "hello"
func unsafe(_ mutable: (UnsafeMutableRawPointer...) -> ()) {
    mutable(&foo, &bar)
}

unsafe { (mutable: UnsafeMutableRawPointer...) in
    for m in mutable {
        print(UnsafePointer<Int>(.init(m)).pointee)
    }
}

The goal is to print this...

100
"hello"

Instead, here is what was printed...

100
478560413032

Finally, if I try and replace...
print(UnsafePointer<Int>(.init(m)).pointee)
with...
print(UnsafePointer<String>(.init(m)).pointee)

I also receive a crash. (If this could be handled with try that would be great. But it is not a throwing method.)


I really wish this was possible, but unfortunately this also crashes:

print(UnsafePointer<Any>(.init(m)).pointee)
0

There are 0 answers