<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 12, 2016, at 12:39 PM, Bouke Haarsma via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Sorry for all the pings, but it appears that the code below doesn’t work after all;</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">fatal error: can't unsafeBitCast between types of different sizes<br class=""></blockquote><br class=""></div><div class="">So the question remains on how to perform the casts using Swift?</div><div class=""><br class=""></div><div class="">—</div><div class="">Bouke</div></div></div></blockquote><div><br class=""></div><div>Hi Bouke,</div><div><br class=""></div><div>Please see this migration guide:</div><a href="https://swift.org/migration-guide/se-0107-migrate.html" class="">https://swift.org/migration-guide/se-0107-migrate.html</a></div><div><br class=""></div><div>It explains a few things that are not self-explanatory and includes some helper code for dealing with the socket API.</div><div><br class=""></div><div>Given how your code is structured, I would start with a raw pointer, then replace all the ‘withMemoryRebound’ calls to ‘bindMemory’, and you should be fine:</div><div><br class=""></div><div><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">var</span><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">&nbsp;rawSockAddr = UnsafeRawPointer(</span><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">CFDataGetBytePtr</span><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">(data))</span></div><div><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">switch …</span></div><div><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">case …:</span></div><div><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">&nbsp; let ipv4 = rawSockAddr</span><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">.bindMemory</span><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">(to:&nbsp;</span><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">sockaddr_in</span><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">.</span><span class="" style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">self</span><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">, capacity: 1)</span></div><div><span class="" style="color: rgb(52, 149, 175); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div><span class="" style="font-variant-ligatures: no-common-ligatures;">You might also be able to avoid CFData and use Swift’s Data directly, but any Swifty API you use will encourage you to restructure your code so that pointer access is confined to a closure, like Data.withUnsafeBytes, or UnsafePointer.withMemoryRebound(to:capacity:). You should never return the pointer argument from these closure. The closure taking APIs are designed to keep your data alive while you access it and make sure you’re not mixing pointers of different types to the same memory.</span></div><div><span class="" style="font-variant-ligatures: no-common-ligatures;"><br class=""></span></div><div><span class="" style="font-variant-ligatures: no-common-ligatures;">-Andy</span></div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">

</div>
<div class=""><blockquote type="cite" class=""><div class="">On 12 sep. 2016, at 21:37, Bouke Haarsma &lt;<a href="mailto:bouke@haarsma.eu" class="">bouke@haarsma.eu</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div class="">Sorry, missed the first line when copying:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;generic = unsafeBitCast(CFDataGetBytePtr(data), to: sockaddr.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">switch</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;generic.sa_family {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">case</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;sa_family_t(AF_INET):</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;ipv4 = unsafeBitCast(generic, to: sockaddr_in.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures;">//...</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">case</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;sa_family_t(AF_INET6):</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">let</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;ipv6 = unsafeBitCast(generic, to: sockaddr_in6.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);">self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">)</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures;">//...</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(4, 51, 255);"><span class="" style="font-variant-ligatures: no-common-ligatures;">default</span><span class="" style="font-variant-ligatures: no-common-ligatures;">:</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp; &nbsp;&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures;">//...</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;">}</span></div><div class=""><br class=""></div><div class="">—</div><div class="">Bouke</div></div></div></span></div></div><div class="">

</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On 12 sep. 2016, at 21:35, Bouke Haarsma &lt;<a href="mailto:bouke@haarsma.eu" class="">bouke@haarsma.eu</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Ah the missing part of the puzzle appears to be unsafeBitCast(:to:), so the Swift version becomes this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures" class=""> generic.sa_family {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> sa_family_t(AF_INET):</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ipv4 = unsafeBitCast(generic, to: sockaddr_in.</span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">//...</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> sa_family_t(AF_INET6):</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ipv6 = unsafeBitCast(generic, to: sockaddr_in6.</span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">//...</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(4, 51, 255);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">default</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">//...</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""></div><div class="">—</div><div class="">Bouke</div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">On 12 sep. 2016, at 21:25, Bouke Haarsma &lt;<a href="mailto:bouke@haarsma.eu" class="">bouke@haarsma.eu</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi all,</div><div class=""><br class=""></div><div class="">Inside my&nbsp;CFSocketCallBack a pointer to a sockaddr is provided wrapped in a CFData structure. The sockaddr could be either a sockaddr_in (IPv4) or sockaddr_in6 (IPv6) struct. In order to discover which struct you’re dealing with, the attribute sa_family can be inspected. In C this would look something like this:</div><div class=""><br class=""></div><div class=""><pre class="lang-c prettyprinted prettyprint" style="margin-top: 0px; padding: 5px; border: 0px; font-size: 13px; width: auto; max-height: 600px; overflow: auto; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; background-color: rgb(239, 240, 241); color: rgb(57, 51, 24); word-wrap: normal;"><code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;" class=""><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(16, 16, 148);">struct</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> sockaddr_storage sa</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">

</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(16, 16, 148);">switch</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">(((</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sockaddr</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">*)&amp;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sa</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">)-&gt;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sa_family</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">
    </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(16, 16, 148);">case</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> AF_INET</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">
        inet_ntop</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">AF_INET</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">&amp;(((</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sockaddr_in</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">*)&amp;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sa</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">)-&gt;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sin_addr</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">),</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">...);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">
        </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(16, 16, 148);">break</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">
    </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(16, 16, 148);">case</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> AF_INET6</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">
        inet_ntop</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">AF_INET6</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">&amp;(((</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sockaddr_in6</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">*)&amp;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sa</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">)-&gt;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">sin6_addr</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">),</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">...);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">
        </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: rgb(16, 16, 148);">break</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">}</span></code></pre><div class="">(from:&nbsp;<a href="http://stackoverflow.com/a/13167913" class="">http://stackoverflow.com/a/13167913</a>)</div></div><div class=""><br class=""></div><div class="">Wheras to do this in Swift 3, the only way I found thus far is this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> generic = </span><span style="font-variant-ligatures: no-common-ligatures" class="">CFDataGetBytePtr</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(data).</span><span style="font-variant-ligatures: no-common-ligatures" class="">withMemoryRebound</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(to: </span><span style="font-variant-ligatures: no-common-ligatures" class="">sockaddr</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, capacity: 1) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">pointee</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures" class=""> generic.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">sa_family</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">sa_family_t</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">AF_INET</span><span style="font-variant-ligatures: no-common-ligatures;" class="">):</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ipv4 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">withUnsafePointer</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: &amp;generic) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">withMemoryRebound</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">sockaddr_in</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">, capacity: 1) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">pointee</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; //...</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(4, 51, 255);" class="">case</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="color: rgb(52, 149, 175); font-variant-ligatures: no-common-ligatures;" class="">sa_family_t</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="color: rgb(52, 149, 175); font-variant-ligatures: no-common-ligatures;" class="">AF_INET6</span><span style="font-variant-ligatures: no-common-ligatures;" class="">):</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ipv6 = </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">withUnsafePointer</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: &amp;generic) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">withMemoryRebound</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">sockaddr_in6</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #0433ff" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">, capacity: 1) {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3495af" class="">pointee</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; //...</div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(4, 51, 255); font-variant-ligatures: no-common-ligatures;" class="">default</span><span style="font-variant-ligatures: no-common-ligatures;" class="">:</span></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; //…</div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal; color: rgb(4, 51, 255);" class=""><br class=""></div></div></div><div class="">This looks very ugly with all the closures and ``withMemoryRebound``. Is there a better way to do this? I think there should be something more “Swifty”.</div><div class=""><br class=""></div><div class="">—</div><div class="">Bouke</div></div></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>