apple / swift-mmio

Define and operate on type safe MMIO

Home Page:https://swiftpackageindex.com/apple/swift-mmio/documentation/mmio

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

It would be useful to have common bit sizes of unsigned integers for BitFieldProjectables

phausler opened this issue · comments

UInt8, UInt16, UInt32, and UInt64 are common field member types (when not enumerations or option sets).

extension UInt8: BitFieldProjectable {
  public static var bitWidth: Int { 8 }

  public init<Storage>(storage: Storage)
  where Storage: FixedWidthInteger & UnsignedInteger {
    self.init(storage)
  }

  public func storage<Storage>(_: Storage.Type) -> Storage
  where Storage: FixedWidthInteger & UnsignedInteger {
    Storage(self)
  }
}

yep this makes sense, ideally we could represent this as:

extension<T> T: BitFieldProjectable where T: FixedWidthInteger {
  // public static var bitWidth: Int -- Already a requirement of FixedWidthInteger  
  public init<Storage>(storage: Storage) ...
  public func storage<Storage>(_: Storage.Type) -> Storage ...
}

iirc the extension<T> isn't valid syntax yet.

Yeah this isn't something we can write yet, instead we will need to manually write out conformances for [U]Int{8,16,32,64}

that seems like something that could be added earlier for the utility and then later generalized when the language grows that feature.

Absolutely, I'm not suggesting this wait for extension<T> to exist; just remarking that it would be a more convenient and general spelling.