Skip to content

Commit a9b64d4

Browse files
committed
[BasicContainers] RigidArray,UniqueArray: Add Equatable conformance on Swift 6.4
This is currently conditional on the UnstableContainersPreview trait. (Swift 6.4 has not shipped yet, so we cannot unconditionally enable this without risking breakage.)
1 parent 917eead commit a9b64d4

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

Sources/BasicContainers/RigidArray/RigidArray+Equatable.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,18 @@ extension RigidArray where Element: ~Copyable {
2525
}
2626
}
2727

28-
28+
#if compiler(>=6.4) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
29+
@available(SwiftStdlib 5.0, *)
30+
extension RigidArray: Equatable where Element: Equatable & ~Copyable {
31+
@inlinable
32+
public static func ==(
33+
left: borrowing Self,
34+
right: borrowing Self
35+
) -> Bool {
36+
left.span._elementsEqual(to: right.span)
37+
}
38+
}
39+
#else
2940
@available(SwiftStdlib 5.0, *)
3041
extension RigidArray /*: Equatable */ where Element: Equatable /* & ~Copyable */ {
3142
@inlinable
@@ -36,5 +47,6 @@ extension RigidArray /*: Equatable */ where Element: Equatable /* & ~Copyable */
3647
left.span._elementsEqual(to: right.span)
3748
}
3849
}
50+
#endif
3951

4052
#endif

Sources/BasicContainers/UniqueArray/UniqueArray+Equatable.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ extension UniqueArray where Element: ~Copyable {
2424
}
2525
}
2626

27+
#if compiler(>=6.4) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
2728
@available(SwiftStdlib 5.0, *)
28-
extension UniqueArray /*: Equatable */ where Element: Equatable /* & ~Copyable */ {
29+
extension UniqueArray: Equatable where Element: Equatable & ~Copyable {
2930
@inlinable
3031
public static func ==(
3132
left: borrowing Self,
@@ -34,5 +35,17 @@ extension UniqueArray /*: Equatable */ where Element: Equatable /* & ~Copyable *
3435
left.span._elementsEqual(to: right.span)
3536
}
3637
}
38+
#else
39+
@available(SwiftStdlib 5.0, *)
40+
extension UniqueArray where Element: Equatable {
41+
@inlinable
42+
public static func ==(
43+
left: borrowing Self,
44+
right: borrowing Self
45+
) -> Bool {
46+
left.span._elementsEqual(to: right.span)
47+
}
48+
}
49+
#endif
3750

3851
#endif

Sources/InternalCollectionsUtilities/Span+Extras.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ extension Span where Element: ~Copyable {
3838
}
3939
}
4040

41+
#if compiler(>=6.4) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
42+
@available(SwiftStdlib 5.0, *)
43+
extension Span where Element: Equatable & ~Copyable {
44+
@_alwaysEmitIntoClient
45+
package func _elementsEqual(to other: borrowing Self) -> Bool {
46+
return self.withUnsafeBufferPointer { a in
47+
other.withUnsafeBufferPointer { b in
48+
guard a.count == b.count else { return false }
49+
guard a.baseAddress != b.baseAddress else { return true }
50+
var i = 0
51+
while i < self.count {
52+
guard a[i] == b[i] else { return false }
53+
i &+= 1
54+
}
55+
return true
56+
}
57+
}
58+
}
59+
}
60+
#else
4161
@available(SwiftStdlib 5.0, *)
4262
extension Span where Element: Equatable /* & ~Copyable */ {
4363
@_alwaysEmitIntoClient
@@ -56,6 +76,7 @@ extension Span where Element: Equatable /* & ~Copyable */ {
5676
}
5777
}
5878
}
79+
#endif
5980

6081
@available(SwiftStdlib 5.0, *)
6182
extension Span where Element: Hashable /* & ~Copyable */ {

0 commit comments

Comments
 (0)