Re: [swift-users] Mapping C semantics to Swift

2016-05-20 Thread Matthew Johnson via swift-users
> On May 20, 2016, at 12:54 PM, Joe Groff via swift-users > wrote: > > >> On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users >> 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

Re: [swift-users] Mapping C semantics to Swift

2016-05-20 Thread Joe Groff via swift-users
> On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users > 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 viola

Re: [swift-users] Mapping C semantics to Swift

2016-05-20 Thread Ken Burgett via swift-users
On 2016-05-20 08:55, Jens Alfke wrote: On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users wrote: 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 attri

Re: [swift-users] Mapping C semantics to Swift

2016-05-20 Thread Jens Alfke via swift-users
> On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users > wrote: > > 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

Re: [swift-users] Mapping C semantics to Swift

2016-05-20 Thread Ken Burgett via swift-users
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

Re: [swift-users] Mapping C semantics to Swift

2016-05-19 Thread Ramakrishna Mallireddy via swift-users
An working example, let fm = NSFileManager.defaultManager() var mh = mach_header() // Defined as C struct, var lc = load_command() // Defined as C struct var location = 0 if let data = fm.contentsAtPath("home/user/projects/some.o") { data.getBytes(&mh, length: sizeof(mach_hea

Re: [swift-users] Mapping C semantics to Swift

2016-05-19 Thread Joe Groff via swift-users
> On May 19, 2016, at 10:00 AM, Ken Burgett via swift-users > wrote: > > I would like to know if a struct in Swift has any guarantee of contiguity, > like a struct in C. I am attempting to port a C program that makes many > assumptions about the underlying semantics of C structs, and does th