<div dir="ltr"><div>I believe UnsafeRawPointer is a subtype of any UnsafePointer<T> type, so UnsafePointer<T>’s are always implicitly convertible to UnsafeRawPointers. So even the following compiles:<br><br><br><span style="font-family:monospace,monospace">import Foundation<br><br>func f(_ a:UnsafeRawPointer)<br>{<br><br>}<br><br>let data = Data(bytes: [1, 2, 3, 4])<br>data.withUnsafeBytes{ (ptr:UnsafePointer<Range<Int>>) in f(ptr) } </span><br><br></div>So annotate <span style="font-family:monospace,monospace">ptr</span> with its actual type, and it should just work.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 30, 2017 at 10:57 AM, Daniel Dunbar via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Jun 30, 2017, at 7:40 AM, Martin R via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
><br>
> I have a C function<br>
><br>
> void myfunc(const void *ptr);<br>
><br>
> which is imported to Swift as<br>
><br>
> func myfunc(_ ptr: UnsafeRawPointer!)<br>
><br>
> This compiles and runs without problems:<br>
><br>
> let data = Data(bytes: [1, 2, 3, 4])<br>
> data.withUnsafeBytes { (ptr) in myfunc(ptr) } // (A)<br>
><br>
> and the type of `ptr` is inferred as `UnsafePointer<Void>`. But adding an explicit type<br>
> annotation produces a compiler warning:<br>
<br>
</span>How do you know the inferred type is `UnsafePointer<Void>`? I think it is more likely it is `UnsafePointer<UInt8>`, and the following compiles:<br>
```<br>
<span class="">let data = Data(bytes: [1, 2, 3, 4])<br>
</span>data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) in<br>
myfunc(ptr)<br>
}<br>
```<br>
<br>
- Daniel<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> data.withUnsafeBytes { (ptr: UnsafePointer<Void>) in myfunc(ptr) } // (B)<br>
> // warning: UnsafePointer<Void> has been replaced by UnsafeRawPointer<br>
><br>
> which is understandable in the view of "SE-0107 UnsafeRawPointer API".<br>
><br>
> The "Fix-it" replaces `UnsafePointer<Void>` by `UnsafeRawPointer`, and that does not<br>
> compile anymore:<br>
><br>
> data.withUnsafeBytes { (ptr: UnsafeRawPointer) in myfunc(ptr) } // (C)<br>
> // error: cannot convert value of type 'Void' to closure result type '_'<br>
><br>
> because there is no `withUnsafeBytes()` method taking a `(UnsafeRawPointer)-><wbr>ResultType`<br>
> closure.<br>
><br>
><br>
> My questions are:<br>
><br>
> 1. Why are (A) and (B) treated differently?<br>
><br>
> 2. Is (A) "legal", or should one use some non-void pointer<br>
><br>
> data.withUnsafeBytes { (ptr: UnsafePointer<Int8>) in myfunc(ptr) } // (D)<br>
><br>
> (which feels wrong to me because it is converted back to a void pointer when<br>
> calling the function).<br>
><br>
> 3. Or should there be a `withUnsafeRawPointer()` method which makes (C) compile as<br>
><br>
> data.withUnsafeRawBytes { (ptr: UnsafeRawPointer) in myfunc(ptr) }<br>
><br>
> This would also allow to access the data at byte offsets more easily, e.g.<br>
><br>
> data.withUnsafeRawBytes { ptr in<br>
> let u16 = ptr.load(fromByteOffset: 4, as: UInt16.self)<br>
> }<br>
><br>
> Does that makes sense?<br>
><br>
> Regards, Martin<br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>