[swift-users] Mapping C semantics to Swift

Joe Groff jgroff at apple.com
Fri May 20 12:54:18 CDT 2016


> On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users <swift-users at swift.org> wrote:
> 
> I have been told that the in-memory layout of a Swift struct is 'undefined', so the C style of aliasing an array over a struct and iterating across the struct is a definite no-no.  Not surprising, since aliasing violates all type-safe rules.
> 
> So, if you have this situation, you must address it on a case-by-case basis.  In my case, converting a hash algorithm from C to Swift, the required C struct morphs into a Swift class, with a member function 'as_C_byte_array' which answers an array of the required form.  Internally, this function will shift and mask class attributes in order to build the necessary result.

That's not necessary. You can leave the struct defined in C and import it into Swift. Swift will respect C's layout.

-Joe

> Ugly, but I must preserve the semantics exactly if I hope to generate the exact same hash as the C version.
> 
> -- 
> Ken Burgett
> Principal Software Engineer
> Email: kenb at iotone.io
> Office: 530.693.4449
> Mobile: 831.332.6846
> URL: www.iotone.co
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list