将Int转换为Collection.IndexDistance

时间:2016-10-23 17:25:57

标签: swift generics collections

我有一个逐步处理Collection的结构。

struct Foo<T: BidirectionalCollection>
    where T.Iterator.Element == UInt8,
          T.SubSequence: BidirectionalCollection,
          T.SubSequence.Iterator.Element == T.Iterator.Element,
          T.SubSequence.Index == T.Index,
          T.SubSequence.IndexDistance == T.IndexDistance,
          T.SubSequence.SubSequence == T.SubSequence
{
    private var state: T.IndexDistance = 0

    mutating func process(_ foo: T) {
        // ...
    }
}

我希望在内部跟踪T.IndexDistance(例如,计算已处理T.IndexDistance的总数)。

但是,我也希望将此距离与普通UInt进行比较,并将其设置为正常UInt s。

问题是,我似乎找不到将UInt转换为T.IndexDistance的方法。

1 个答案:

答案 0 :(得分:0)

let someUInt: UInt = 42
let indexDistance = T.IndexDistance(IntMax(someUInt))