<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jul 6, 2016 at 11:30 AM, Harlan Haskins <span dir="ltr"><<a href="mailto:harlan@harlanhaskins.com" target="_blank">harlan@harlanhaskins.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I’ve also seen unsafeAddressOf(_:) used when interfacing with C function pointers when the lifetime of an object is guaranteed. Many C APIs vend an API like:<div><br></div><div><font face="Menlo">void perform_action(void (*callback)(void *data), void *initial_data);</font></div><div><font face="Menlo"><br></font></div><div>For which it is expected to use unsafeAddressOf on a class instance, like:</div><div><br></div><div><font face="Menlo">perform_action({ data in</font></div><div><font face="Menlo"> let _self = unsafeBitCast(data, to: MyClass.self)</font></div><div><font face="Menlo"> _self.foo()</font></div><div><font face="Menlo">}, data: unsafeAddressOf(self))<br></font><div><br></div><div>It’s unsafe and error-prone, sure, but that’s why we have `unsafe` in the name 😅 — I’ve had to use this to interface with libclang.</div></div></div></blockquote><div><br></div><div>Hi Harlan,</div><div><br></div><div>For this case, Unmanaged is the recommended API.</div><div><br></div><div>Dmitri</div></div><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>>*/</div>
</div></div>