Skip to content

Commit 00e23d3

Browse files
authored
Merge pull request SwiftGit2#149 from mattrubin/equatable-and-hashable
Simplify Equatable and Hashable implementations
2 parents dcf3666 + df7bd3c commit 00e23d3

File tree

7 files changed

+42
-114
lines changed

7 files changed

+42
-114
lines changed

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ opt_in_rules:
5252
- redundant_nil_coalescing
5353
- redundant_type_annotation
5454
- sorted_first_last
55+
- static_operator
5556
- strict_fileprivate
5657
- switch_case_on_newline
5758
- toggle_bool

SwiftGit2/Libgit2.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
import libgit2
1010

11-
func == (lhs: git_otype, rhs: git_otype) -> Bool {
12-
return lhs.rawValue == rhs.rawValue
13-
}
14-
1511
extension git_strarray {
1612
func filter(_ isIncluded: (String) -> Bool) -> [String] {
1713
return map { $0 }.filter(isIncluded)

SwiftGit2/OID.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ extension OID: Hashable {
6262
hasher.combine(bytes: $0)
6363
}
6464
}
65-
}
6665

67-
public func == (lhs: OID, rhs: OID) -> Bool {
68-
var left = lhs.oid
69-
var right = rhs.oid
70-
return git_oid_cmp(&left, &right) == 0
66+
public static func == (lhs: OID, rhs: OID) -> Bool {
67+
var left = lhs.oid
68+
var right = rhs.oid
69+
return git_oid_cmp(&left, &right) == 0
70+
}
7171
}

SwiftGit2/Objects.swift

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ public protocol ObjectType {
2121
init(_ pointer: OpaquePointer)
2222
}
2323

24-
public func == <O: ObjectType>(lhs: O, rhs: O) -> Bool {
25-
return lhs.oid == rhs.oid
24+
public extension ObjectType {
25+
static func == (lhs: Self, rhs: Self) -> Bool {
26+
return lhs.oid == rhs.oid
27+
}
28+
29+
func hash(into hasher: inout Hasher) {
30+
hasher.combine(oid)
31+
}
2632
}
2733

2834
public struct Signature {
@@ -77,15 +83,8 @@ extension Signature: Hashable {
7783
}
7884
}
7985

80-
public func == (lhs: Signature, rhs: Signature) -> Bool {
81-
return lhs.name == rhs.name
82-
&& lhs.email == rhs.email
83-
&& lhs.time == rhs.time
84-
&& lhs.timeZone == rhs.timeZone
85-
}
86-
8786
/// A git commit.
88-
public struct Commit: ObjectType {
87+
public struct Commit: ObjectType, Hashable {
8988
public static let type = GIT_OBJ_COMMIT
9089

9190
/// The OID of the commit.
@@ -120,18 +119,12 @@ public struct Commit: ObjectType {
120119
}
121120
}
122121

123-
extension Commit: Hashable {
124-
public func hash(into hasher: inout Hasher) {
125-
hasher.combine(oid)
126-
}
127-
}
128-
129122
/// A git tree.
130-
public struct Tree: ObjectType {
123+
public struct Tree: ObjectType, Hashable {
131124
public static let type = GIT_OBJ_TREE
132125

133126
/// An entry in a `Tree`.
134-
public struct Entry {
127+
public struct Entry: Hashable {
135128
/// The entry's UNIX file attributes.
136129
public let attributes: Int32
137130

@@ -176,34 +169,14 @@ public struct Tree: ObjectType {
176169
}
177170
}
178171

179-
extension Tree.Entry: Hashable {
180-
public func hash(into hasher: inout Hasher) {
181-
hasher.combine(attributes)
182-
hasher.combine(object)
183-
hasher.combine(name)
184-
}
185-
}
186-
187172
extension Tree.Entry: CustomStringConvertible {
188173
public var description: String {
189174
return "\(attributes) \(object) \(name)"
190175
}
191176
}
192177

193-
public func == (lhs: Tree.Entry, rhs: Tree.Entry) -> Bool {
194-
return lhs.attributes == rhs.attributes
195-
&& lhs.object == rhs.object
196-
&& lhs.name == rhs.name
197-
}
198-
199-
extension Tree: Hashable {
200-
public func hash(into hasher: inout Hasher) {
201-
hasher.combine(oid)
202-
}
203-
}
204-
205178
/// A git blob.
206-
public struct Blob: ObjectType {
179+
public struct Blob: ObjectType, Hashable {
207180
public static let type = GIT_OBJ_BLOB
208181

209182
/// The OID of the blob.
@@ -221,14 +194,8 @@ public struct Blob: ObjectType {
221194
}
222195
}
223196

224-
extension Blob: Hashable {
225-
public func hash(into hasher: inout Hasher) {
226-
hasher.combine(oid)
227-
}
228-
}
229-
230197
/// An annotated git tag.
231-
public struct Tag: ObjectType {
198+
public struct Tag: ObjectType, Hashable {
232199
public static let type = GIT_OBJ_TAG
233200

234201
/// The OID of the tag.
@@ -256,9 +223,3 @@ public struct Tag: ObjectType {
256223
message = String(validatingUTF8: git_tag_message(pointer))!
257224
}
258225
}
259-
260-
extension Tag: Hashable {
261-
public func hash(into hasher: inout Hasher) {
262-
hasher.combine(oid)
263-
}
264-
}

SwiftGit2/Pointers.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ public protocol PointerType: Hashable {
1717
var type: git_otype { get }
1818
}
1919

20-
public func == <P: PointerType>(lhs: P, rhs: P) -> Bool {
21-
return lhs.oid == rhs.oid && lhs.type.rawValue == rhs.type.rawValue
20+
public extension PointerType {
21+
static func == (lhs: Self, rhs: Self) -> Bool {
22+
return lhs.oid == rhs.oid
23+
&& lhs.type == rhs.type
24+
}
25+
26+
func hash(into hasher: inout Hasher) {
27+
hasher.combine(oid)
28+
}
2229
}
2330

2431
/// A pointer to a git object.
@@ -71,12 +78,6 @@ public enum Pointer: PointerType {
7178
}
7279
}
7380

74-
extension Pointer: Hashable {
75-
public func hash(into hasher: inout Hasher) {
76-
hasher.combine(oid)
77-
}
78-
}
79-
8081
extension Pointer: CustomStringConvertible {
8182
public var description: String {
8283
switch self {
@@ -103,9 +104,3 @@ public struct PointerTo<T: ObjectType>: PointerType {
103104
self.oid = oid
104105
}
105106
}
106-
107-
extension PointerTo: Hashable {
108-
public func hash(into hasher: inout Hasher) {
109-
hasher.combine(oid)
110-
}
111-
}

SwiftGit2/References.swift

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ public protocol ReferenceType {
2020
var oid: OID { get }
2121
}
2222

23-
public func ==<T: ReferenceType>(lhs: T, rhs: T) -> Bool {
24-
return lhs.longName == rhs.longName
25-
&& lhs.oid == rhs.oid
23+
public extension ReferenceType {
24+
static func == (lhs: Self, rhs: Self) -> Bool {
25+
return lhs.longName == rhs.longName
26+
&& lhs.oid == rhs.oid
27+
}
28+
29+
func hash(into hasher: inout Hasher) {
30+
hasher.combine(longName)
31+
hasher.combine(oid)
32+
}
2633
}
2734

2835
/// Create a Reference, Branch, or TagReference from a libgit2 `git_reference`.
@@ -37,7 +44,7 @@ internal func referenceWithLibGit2Reference(_ pointer: OpaquePointer) -> Referen
3744
}
3845

3946
/// A generic reference to a git object.
40-
public struct Reference: ReferenceType {
47+
public struct Reference: ReferenceType, Hashable {
4148
/// The full name of the reference (e.g., `refs/heads/master`).
4249
public let longName: String
4350

@@ -56,15 +63,8 @@ public struct Reference: ReferenceType {
5663
}
5764
}
5865

59-
extension Reference: Hashable {
60-
public func hash(into hasher: inout Hasher) {
61-
hasher.combine(longName)
62-
hasher.combine(oid)
63-
}
64-
}
65-
6666
/// A git branch.
67-
public struct Branch: ReferenceType {
67+
public struct Branch: ReferenceType, Hashable {
6868
/// The full name of the reference (e.g., `refs/heads/master`).
6969
public let longName: String
7070

@@ -122,15 +122,8 @@ public struct Branch: ReferenceType {
122122
}
123123
}
124124

125-
extension Branch: Hashable {
126-
public func hash(into hasher: inout Hasher) {
127-
hasher.combine(longName)
128-
hasher.combine(oid)
129-
}
130-
}
131-
132125
/// A git tag reference, which can be either a lightweight tag or a Tag object.
133-
public enum TagReference: ReferenceType {
126+
public enum TagReference: ReferenceType, Hashable {
134127
/// A lightweight tag, which is just a name and an OID.
135128
case lightweight(String, OID)
136129

@@ -194,10 +187,3 @@ public enum TagReference: ReferenceType {
194187
git_object_free(pointer)
195188
}
196189
}
197-
198-
extension TagReference: Hashable {
199-
public func hash(into hasher: inout Hasher) {
200-
hasher.combine(longName)
201-
hasher.combine(oid)
202-
}
203-
}

SwiftGit2/Remotes.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import libgit2
1010

1111
/// A remote in a git repository.
12-
public struct Remote {
12+
public struct Remote: Hashable {
1313
/// The name of the remote.
1414
public let name: String
1515

@@ -24,14 +24,3 @@ public struct Remote {
2424
URL = String(validatingUTF8: git_remote_url(pointer))!
2525
}
2626
}
27-
28-
extension Remote: Hashable {
29-
public func hash(into hasher: inout Hasher) {
30-
hasher.combine(name)
31-
hasher.combine(URL)
32-
}
33-
}
34-
35-
public func == (lhs: Remote, rhs: Remote) -> Bool {
36-
return lhs.name == rhs.name && lhs.URL == rhs.URL
37-
}

0 commit comments

Comments
 (0)