[swift-users] Mapping C semantics to Swift
Matthew Johnson
matthew at anandabits.com
Fri May 20 12:58:19 CDT 2016
> On May 20, 2016, at 12:54 PM, Joe Groff via swift-users <swift-users at swift.org> wrote:
>
>
>> 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.
Any thought on introducing an attribute or something into the “generated header” for these C imports to make this more clear? The same syntax could be used in the future if / when Swift supports more control over 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
>
> _______________________________________________
> 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