On 29 Oct 2016, at 11:34, Thierry Passeron via swift-users 
<swift-users@swift.org> wrote:

> For me this tuple should have been at least imported with named fields or 
> even better as [UInt32] array...

The latter is not possible because Swift does not currently have support for 
fixed-size arrays.  This is a well-known limitation.

To get the former you’d have to either tweak the Mach headers or give the Swift 
compiler knowledge about Mach.  Both are technically possible but I haven’t 
seen any concrete efforts in that direction.

Note: The situation with Mach APIs on our platforms is rather nuance.  They are 
officially supported but we strongly encourage folks not to use them because 
they are very tightly bound to the kernel.  In your case (getting CPU load 
information) using Mach is fine, but for most Mach APIs there are better 
high-level alternatives.

Feel free to file bug reports for where you’d like to see the system improved 
here.

<https://developer.apple.com/bug-reporting/>

In the meantime, if I were in your shoes I’d write an extension for 
`processor_cpu_load_info` that adds the accessors I need:

extension processor_cpu_load_info {
    var user: UInt32 {
        get {
            assert(CPU_STATE_USER == 0)
            return self.cpu_ticks.0
        }
        set {
            assert(CPU_STATE_USER == 0)
            self.cpu_ticks.0 = newValue

        }
    }
}

There’s very little risk hard coding the `.0` because:

* the order of these fields can’t change without breaking binary compatibility

* if you port the code to a new platform, you’ll very likely trip the asserts 
if the fields get re-ordered

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to