Skip to content

Incorrect ApplePencilSupport OptionSet rawValue definition #445

@jamztang

Description

@jamztang

public static let firstGeneration = ApplePencilSupport(rawValue: 0x01)
public static let secondGeneration = ApplePencilSupport(rawValue: 0x02)
public static let firstGenerationUsbC = ApplePencilSupport(rawValue: 0x03)
public static let pro = ApplePencilSupport(rawValue: 0x04)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions