Skip to content

Commit 2f61f74

Browse files
committed
Improve Tuple+Versionstamp code quality and readability
- Use count(where:) instead of reduce for counting incomplete versionstamps This is more idiomatic Swift and clearly expresses intent - Replace guard with if for incompleteCount validation Guard is for early returns; if is more appropriate here - Optimize withUnsafeBytes to eliminate unnecessary allocation Append directly instead of creating intermediate Array Addresses: glbrntt review comments FoundationDB#6, FoundationDB#7, FoundationDB#10
1 parent 7f8a07a commit 2f61f74

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Sources/FoundationDB/Tuple+Versionstamp.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ extension Tuple {
8989
}
9090

9191
let offset = UInt32(position)
92-
packed.append(contentsOf: withUnsafeBytes(of: offset.littleEndian) { Array($0) })
92+
withUnsafeBytes(of: offset.littleEndian) { packed.append(contentsOf: $0) }
9393

9494
return packed
9595
}
@@ -108,11 +108,11 @@ extension Tuple {
108108
/// Count incomplete versionstamps in tuple
109109
/// - Returns: Number of incomplete versionstamps
110110
public func countIncompleteVersionstamps() -> Int {
111-
return elements.reduce(0) { count, element in
112-
if let vs = element as? Versionstamp, !vs.isComplete {
113-
return count + 1
111+
return elements.count { element in
112+
if let vs = element as? Versionstamp {
113+
return !vs.isComplete
114114
}
115-
return count
115+
return false
116116
}
117117
}
118118

@@ -121,7 +121,7 @@ extension Tuple {
121121
public func validateForVersionstamp() throws {
122122
let incompleteCount = countIncompleteVersionstamps()
123123

124-
guard incompleteCount == 1 else {
124+
if incompleteCount != 1 {
125125
throw TupleError.invalidEncoding
126126
}
127127
}

0 commit comments

Comments
 (0)