[swift-users] Pointer conversions between different sockaddr types

Quinn "The Eskimo!" eskimo1 at apple.com
Thu Aug 18 02:28:58 CDT 2016


On 17 Aug 2016, at 18:55, Martin R via swift-users <swift-users at swift.org> wrote:

> - Are both solutions correct, should one be preferred, or are both wrong?

Your `withMemoryRebound` solution is correct.

> - Can the same be achieved simpler?

Not without introducing a layer of abstraction.

In my case I introduced an abstract `Address` type (basically a wrapper around `sockaddr_storage`) and then added a method to that object which calls a closure with the right parameters (actually, multiple such methods, depending on whether I’m calling something like `connect` which takes an address, or `getpeername`, which returns one).  This approach concentrates all the ugly in one place, making the rest of my BSD Sockets code much cleaner.

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware




More information about the swift-users mailing list