DeviceKit icon indicating copy to clipboard operation
DeviceKit copied to clipboard

Incorrect ApplePencilSupport OptionSet rawValue definition

Open jamztang opened this issue 7 months ago • 0 comments

https://github.com/devicekit/DeviceKit/blob/513b9d7e7a1bd46504a1009bbab943b75ce2f195/Source/Device.generated.swift#L2108-L2111

If we consider the following code, it would not work well because of 0x03 can be subtracted by 0x01 and 0x02 in binary operation

extension ApplePencilSupport
    func supportsHover() -> Bool {
        // future proofing new apple pencils by using a black list here
        self.subtracting([.firstGeneration, .secondGeneration]).isEmpty == false
    }
}


let deviceA: ApplePencilSupport = [.pro] // supports hover
let deviceB: ApplePencilSupport = [.firstGenerationUsbC] // supports hover
let deviceC: ApplePencilSupport = [.firstGeneration, .secondGeneration] // no hover capability
let deviceD: ApplePencilSupport = [.secondGeneration, .firstGenerationUsbC] // supports hover

deviceA.supportsHover() // return true
deviceB.supportsHover() // return false but it should return true
deviceC.supportsHover() // return false 
deviceD.supportsHover() // return false but it should return true

I believe the fix would look something like this

    public static let firstGeneration = ApplePencilSupport(rawValue: 1 << 0)   // 1
    public static let secondGeneration = ApplePencilSupport(rawValue: 1 << 1) // 2
    public static let firstGenerationUsbC = ApplePencilSupport(rawValue: 1 << 2) // 4
    public static let pro = ApplePencilSupport(rawValue: 1 << 3) // 8

jamztang avatar May 27 '25 04:05 jamztang