Skip to content

Commit 03648e6

Browse files
committed
Update [NS]IndexPath to match API exposed by Foundation
1 parent 1dccff5 commit 03648e6

File tree

2 files changed

+65
-49
lines changed

2 files changed

+65
-49
lines changed

Diff for: Foundation/IndexPath.swift

+58-46
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
3232
}
3333

3434
/// Initialize with a sequence of integers.
35-
public init<ElementSequence : Sequence>(indexes: ElementSequence) where ElementSequence.Iterator.Element == Element {
36-
_indexes = indexes.map { $0 }
35+
public init<ElementSequence : Sequence>(indexes: ElementSequence)
36+
where ElementSequence.Iterator.Element == Element {
37+
_indexes = indexes.map { $0 }
3738
}
3839

3940
/// Initialize with an array literal.
@@ -42,7 +43,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
4243
}
4344

4445
/// Initialize with an array of elements.
45-
public init(indexes: Array<Element>) {
46+
public init(indexes: [Element]) {
4647
_indexes = indexes
4748
}
4849

@@ -67,7 +68,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
6768
}
6869

6970
/// Append an array of elements to `self`.
70-
public mutating func append(_ other: Array<Element>) {
71+
public mutating func append(_ other: [Element]) {
7172
_indexes.append(contentsOf: other)
7273
}
7374

@@ -84,7 +85,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
8485
}
8586

8687
/// Return a new `IndexPath` containing the elements in self and the elements in `other`.
87-
public func appending(_ other: Array<Element>) -> IndexPath {
88+
public func appending(_ other: [Element]) -> IndexPath {
8889
return IndexPath(indexes: _indexes + other)
8990
}
9091

@@ -131,10 +132,11 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
131132
}
132133

133134
/// Sorting an array of `IndexPath` using this comparison results in an array representing nodes in depth-first traversal order.
134-
public func compare(_ other: IndexPath) -> ComparisonResult {
135+
public func compare(_ other: IndexPath) -> ComparisonResult {
135136
// This is temporary
136137
let me = self.makeReference()
137-
return me.compare(other)
138+
let other = other.makeReference()
139+
return me.compare(other as IndexPath)
138140
}
139141

140142
public var hashValue: Int {
@@ -143,74 +145,76 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
143145
return me.hash
144146
}
145147

146-
public var description: String {
147-
return _indexes.description
148-
}
149-
150-
public var debugDescription: String {
151-
return _indexes.debugDescription
152-
}
153-
154148
// MARK: - Bridging Helpers
155149

156150
fileprivate init(nsIndexPath: ReferenceType) {
157151
let count = nsIndexPath.length
158152
if count == 0 {
159153
_indexes = []
160154
} else {
161-
var ptr = malloc(count * MemoryLayout<Element>.size)?.bindMemory(to: Element.self, capacity: count * MemoryLayout<Element>.size)
155+
var ptr = malloc(count * MemoryLayout<Element>.size)
162156
defer { free(ptr) }
163157

164-
nsIndexPath.getIndexes(ptr!, range: NSMakeRange(0, count))
158+
let elementPtr = ptr!.bindMemory(to: Element.self, capacity: count)
159+
nsIndexPath.getIndexes(elementPtr, range: NSMakeRange(0, count))
165160

166-
let buffer = UnsafeBufferPointer(start: ptr, count: count)
161+
let buffer = UnsafeBufferPointer(start: elementPtr, count: count)
167162
_indexes = buffer.map { $0 }
168163
}
169164
}
170165

171166
fileprivate func makeReference() -> ReferenceType {
172167
return _indexes.withUnsafeBufferPointer {
173-
return ReferenceType(indexes: $0.baseAddress!, length: $0.count)
168+
return ReferenceType(indexes: $0.baseAddress, length: $0.count)
174169
}
175170
}
176171

177-
}
172+
public static func ==(lhs: IndexPath, rhs: IndexPath) -> Bool {
173+
return lhs._indexes == rhs._indexes
174+
}
178175

179-
public func ==(lhs: IndexPath, rhs: IndexPath) -> Bool {
180-
return lhs._indexes == rhs._indexes
181-
}
176+
public static func +(lhs: IndexPath, rhs: IndexPath) -> IndexPath {
177+
return lhs.appending(rhs)
178+
}
182179

183-
public func +(lhs: IndexPath, rhs: IndexPath) -> IndexPath {
184-
return lhs.appending(rhs)
185-
}
180+
public static func +=(lhs: inout IndexPath, rhs: IndexPath) {
181+
lhs.append(rhs)
182+
}
186183

187-
public func +=(lhs: inout IndexPath, rhs: IndexPath) {
188-
lhs.append(rhs)
189-
}
184+
public static func <(lhs: IndexPath, rhs: IndexPath) -> Bool {
185+
return lhs.compare(rhs) == ComparisonResult.orderedAscending
186+
}
190187

191-
public func <(lhs: IndexPath, rhs: IndexPath) -> Bool {
192-
return lhs.compare(rhs) == ComparisonResult.orderedAscending
193-
}
188+
public static func <=(lhs: IndexPath, rhs: IndexPath) -> Bool {
189+
let order = lhs.compare(rhs)
190+
return order == ComparisonResult.orderedAscending || order == ComparisonResult.orderedSame
191+
}
194192

195-
public func <=(lhs: IndexPath, rhs: IndexPath) -> Bool {
196-
let order = lhs.compare(rhs)
197-
return order == ComparisonResult.orderedAscending || order == ComparisonResult.orderedSame
198-
}
193+
public static func >(lhs: IndexPath, rhs: IndexPath) -> Bool {
194+
return lhs.compare(rhs) == ComparisonResult.orderedDescending
195+
}
199196

200-
public func >(lhs: IndexPath, rhs: IndexPath) -> Bool {
201-
return lhs.compare(rhs) == ComparisonResult.orderedDescending
197+
public static func >=(lhs: IndexPath, rhs: IndexPath) -> Bool {
198+
let order = lhs.compare(rhs)
199+
return order == ComparisonResult.orderedDescending || order == ComparisonResult.orderedSame
200+
}
202201
}
203202

204-
public func >=(lhs: IndexPath, rhs: IndexPath) -> Bool {
205-
let order = lhs.compare(rhs)
206-
return order == ComparisonResult.orderedDescending || order == ComparisonResult.orderedSame
207-
}
203+
extension IndexPath : CustomStringConvertible, CustomDebugStringConvertible, CustomReflectable {
204+
public var description: String {
205+
return _indexes.description
206+
}
208207

209-
extension IndexPath {
210-
public static func _isBridgedToObjectiveC() -> Bool {
211-
return true
208+
public var debugDescription: String {
209+
return _indexes.debugDescription
212210
}
213211

212+
public var customMirror: Mirror {
213+
return _indexes.customMirror
214+
}
215+
}
216+
217+
extension IndexPath : _ObjectiveCBridgeable {
214218
public static func _getObjectiveCType() -> Any.Type {
215219
return NSIndexPath.self
216220
}
@@ -231,5 +235,13 @@ extension IndexPath {
231235

232236
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSIndexPath?) -> IndexPath {
233237
return IndexPath(nsIndexPath: source!)
234-
}
238+
}
239+
}
240+
241+
extension NSIndexPath : _HasCustomAnyHashableRepresentation {
242+
// Must be @nonobjc to avoid infinite recursion during bridging.
243+
@nonobjc
244+
public func _toCustomAnyHashable() -> AnyHashable? {
245+
return AnyHashable(self as IndexPath)
246+
}
235247
}

Diff for: Foundation/NSIndexPath.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
//
99

1010

11-
open class NSIndexPath: NSObject, NSCopying, NSSecureCoding {
11+
open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
1212

1313
internal var _indexes : [Int]
1414
override public init() {
1515
_indexes = []
1616
}
17-
public init(indexes: UnsafePointer<Int>, length: Int) {
17+
public init(indexes: UnsafePointer<Int>!, length: Int) {
1818
_indexes = Array(UnsafeBufferPointer(start: indexes, count: length))
1919
}
2020

@@ -55,7 +55,7 @@ open class NSIndexPath: NSObject, NSCopying, NSSecureCoding {
5555
open func index(atPosition position: Int) -> Int {
5656
return _indexes[position]
5757
}
58-
open var length: Int {
58+
open var length: Int {
5959
return _indexes.count
6060
}
6161

@@ -95,3 +95,7 @@ open class NSIndexPath: NSObject, NSCopying, NSSecureCoding {
9595
return .orderedSame
9696
}
9797
}
98+
99+
extension NSIndexPath {
100+
open func getIndexes(_ indexes: UnsafeMutablePointer<Int>) { NSUnimplemented() }
101+
}

0 commit comments

Comments
 (0)