[swift-dev] Guarantees of Tuples as Fixed Sized (stack allocated) Arrays

Johannes Weiss johannesweiss at apple.com
Fri Apr 28 06:03:11 CDT 2017


Hi swift-users,

(sorry for the cross post to swift-dev, but wasn't sure where it belongs)

I tried to find guarantees about the memory layout Swift tuples but couldn't find anything. The reason I ask is because I'd like to use them as fixed sized (stack allocated) arrays. I'm pretty sure they're actually not guaranteed to be stack allocated but highly likely I assume :).

Am I correct in assuming that

    let swift_events: (kevent, kevent) = ...

has the same memory layout as

    struct kevent c_events[2] = ...

? In other words, is this legal:

    var events = (kevent(), kevent())
    withUnsafeMutableBytes(of: &events) { event_ptr in
        precondition(MemoryLayout<kevent>.size * 2 == event_ptr.count)
        if let ptr = event_ptr.baseAddress?.bindMemory(to: kevent.self, capacity: 2) {
            return kevent(someFileDescriptor, ptr, 2, ptr, 2, nil)
        }
    }

I'm assuming yes but I'd like to make sure.

Many thanks,
  Johannes



More information about the swift-dev mailing list