Re: [swift-users] Mapping C semantics to Swift
On 2016-05-20 08:55, Jens Alfke wrote: On May 20, 2016, at 7:33 AM, Ken Burgett via swift-userswrote: 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. It would be nice to see Swift acquire structured-data I/O APIs as found in languages like Ruby and Python — these take a printf-like format string and parameters, but read and write values in binary. The format syntax offers modifiers to specify things like byte order, string encoding, etc. This is so much more reliable and readable than doing the encoding by hand. —Jens Hi Jens, I understand the value of strong typing from a runtime point of view, but I really miss the quickness and simplicity of writing a class in Ruby, where duck-typing is king. -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] Mapping C semantics to Swift
> 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 the > necessary result. It would be nice to see Swift acquire structured-data I/O APIs as found in languages like Ruby and Python — these take a printf-like format string and parameters, but read and write values in binary. The format syntax offers modifiers to specify things like byte order, string encoding, etc. This is so much more reliable and readable than doing the encoding by hand. —Jens___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] Mapping C semantics to Swift
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(, length: sizeof(mach_header)) location += sizeof(mach_header) data.getBytes(, range: NSRange(location: location, length: sizeof( load_command)) ) location += sizeof(load_command) } -Rk On Thu, May 19, 2016 at 10:52 PM, Joe Groff via swift-users < swift-users@swift.org> wrote: > > > On May 19, 2016, at 10:00 AM, Ken Burgett via swift-users < > swift-users@swift.org> 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 things like overlaying an array over the struct using a cast, and then > performs and cryptographic hashing of the entire struct by treating it as > an array. > > > > 1. I a Swift struct contiguous? > > > > 2. If 1 above is true, can I somehow cast an array over the struct? > > Swift structs have unspecified layout. If you depend on a specific layout, > you should define the struct in C and import it into Swift for now. > > -Joe > ___ > swift-users mailing list > swift-users@swift.org > https://lists.swift.org/mailman/listinfo/swift-users > ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] Mapping C semantics to Swift
> 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 things like > overlaying an array over the struct using a cast, and then performs and > cryptographic hashing of the entire struct by treating it as an array. > > 1. I a Swift struct contiguous? > > 2. If 1 above is true, can I somehow cast an array over the struct? Swift structs have unspecified layout. If you depend on a specific layout, you should define the struct in C and import it into Swift for now. -Joe ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users