How to convert 'void *' return from a C function to 'UnsafeMutableRawPointer' in Swift 3?
<p>
I'm trying to convert a lua bridge from Swift 2 to Swift 3. I am not the original author so there are aspects of the library I don't know very well and the original author seems not interested to continue working on the project. I have most of the conversion done but there remain one place I'm stuck and could not figure out. I've tried searching on SO and on the Internet but could not find anything that could help me solve the problem.</p>

<p>If anyone is interested in looking at the full source code, here is my fork of the project on github: <a href="https://github.com/weyhan/lua4swift">https://github.com/weyhan/lua4swift</a> (My changes is in the Swift3 branch)</p>

<p>Allow me setup the context to the error I'm stuck on. There is a Userdata class, specifically in the method <code>userdataPointer&lt;T&gt;() -&gt; UnsafeMutablePointer&lt;T&gt;</code> the c function <code>lua_touserdata</code> returns the block address of userdata as a <code>void *</code> pointer type.</p>

<p>Original code written in Swift 2:</p>

<pre><code>public class Userdata: StoredValue {

    public func userdataPointer&lt;T&gt;() -&gt; UnsafeMutablePointer&lt;T&gt; {
        push(vm)
        let ptr = lua_touserdata(vm.vm, -1)
        vm.pop()
        return UnsafeMutablePointer&lt;T&gt;(ptr)
    }

    public func toCustomType&lt;T: CustomTypeInstance&gt;() -&gt; T {
        return userdataPointer().memory
    }

    public func toAny() -&gt; Any {
        return userdataPointer().memory
    }

    override public func kind() -&gt; Kind { return .Userdata }

}
</code></pre>

<p>After the conversion with Xcode 8 migration tool, Xcode is complaining about the return line with error <code>Cannot invoke initializer for type 'UnsafeMutablePointer&lt;T&gt;' with an argument list of type '(UnsafeMutableRawPointer?)'</code>:</p>

<pre><code>return UnsafeMutablePointer&lt;T&gt;(ptr)
</code></pre>

<p>I've fixed it with:</p>

<pre><code>return (ptr?.assumingMemoryBound(to: T.self))!
</code></pre>

<p>Following that above change, now Xcode 8 is now complaining about the calling statement in <code>createCustomType</code>:</p>

<pre><code>public func createCustomType&lt;T: CustomTypeInstance&gt;(setup: (CustomType&lt;T&gt;) -&gt; Void) -&gt; CustomType&lt;T&gt; {
    lua_createtable(vm, 0, 0)
    let lib = CustomType&lt;T&gt;(self)
    pop()

    setup(lib)

    registry[T.luaTypeName()] = lib
    lib.becomeMetatableFor(lib)
    lib["__index"] = lib
    lib["__name"] = T.luaTypeName()

    let gc = lib.gc
    lib["__gc"] = createFunction([CustomType&lt;T&gt;.arg]) { args in
        let ud = args.userdata

        // ******* Here's the line that is causing problem in Swift 3
        (ud.userdataPointer() as UnsafeMutablePointer&lt;Void&gt;).destroy()
        // *******

        let o: T = ud.toCustomType()
        gc?(o)
        return .Nothing
    }

    if let eq = lib.eq {
        lib["__eq"] = createFunction([CustomType&lt;T&gt;.arg, CustomType&lt;T&gt;.arg]) { args in
            let a: T = args.customType()
            let b: T = args.customType()
            return .Value(eq(a, b))
        }
    }
    return lib
}
</code></pre>

<p>Where I'm getting stuck is the line :</p>

<pre><code>(ud.userdataPointer() as UnsafeMutablePointer&lt;Void&gt;).destroy()
</code></pre>

<p>I believe the original author is attempting to clear the memory block where the pointer returned by <code>userdataPointer()</code> call is pointing to.</p>

<p>With the Xcode 8 auto migration tool the above line is converted as below:</p>

<pre><code>(ud.userdataPointer() as UnsafeMutableRawPointer).deinitialize()
</code></pre>

<p>However Xcode now is then complains that <code>Cannot convert call result type 'UnsafeMutablePointer&lt;_&gt;' to expected type 'UnsafeMutableRawPointer'</code>.</p>

<p>From my research, the change to the return line in <code>userdataPointer</code> seems correct, so I think the issue is with the cast to UnsafeMutableRawPointer. I've tried dropping the cast to <code>UnsafeMutableRawPointer</code> and invoke <code>ud.userdataPointer().deinitialize()</code> directly but I get this error <code>Generic parameter 'T' could not be inferred</code>.</p>

<p>Other things I've tried is to convert the UnsafeMutablePointer to UnsafeMutableRawPointer but It always result in Xcode 8 complaining one thing or another. Any suggestion on how to get this to work?</p>