Skip to content

Commit 3459bd9

Browse files
committed
PR feedback
1 parent 86bed79 commit 3459bd9

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

Sources/SWBCore/SpecImplementations/Tools/SwiftCompiler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3237,7 +3237,7 @@ public final class SwiftCompilerSpec : CompilerSpec, SpecIdentifierType, SwiftDi
32373237
// See the related `previewXOJITThunkInfoResolvesSymlinkedSourcePath` test.
32383238
func resolvedPath(_ path: Path) -> Path { (try? fs.realpath(path)) ?? path }
32393239
let realSourceFile = resolvedPath(sourceFile)
3240-
selectedInputPath = originalInputs.first { resolvedPath($0) == realSourceFile } ?? sourceFile
3240+
selectedInputPath = originalInputs.only { resolvedPath($0) == realSourceFile } ?? sourceFile
32413241

32423242
if let driverPayload = payload.driverPayload {
32433243
do {

Sources/SWBUtil/Collection.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,28 @@ public extension Collection {
1717
}
1818

1919
/// Return the only element in the collection, if it has exactly one element.
20-
/// - complexity: O(1).
20+
///
21+
/// **Complexity**. O(1).
2122
var only: Iterator.Element? {
2223
return !isEmpty && index(after: startIndex) == endIndex ? self.first : nil
2324
}
2425

26+
/// Returns the single element of the sequence satisfying `predicate`,
27+
/// or `nil`if no element — or more than one element — satisfies it.
28+
///
29+
/// Unlike `first(where:)`, multiple matches yield `nil`;
30+
/// iteration stops as soon as a second match is found.
31+
///
32+
/// **Complexity**. O(n), where n is the length of the sequence.
33+
func only(where predicate: (Element) throws -> Bool) rethrows -> Element? {
34+
var onlyMatch: Element?
35+
for candidate in self where try predicate(candidate) {
36+
guard onlyMatch == nil else { return nil }
37+
onlyMatch = candidate
38+
}
39+
return onlyMatch
40+
}
41+
2542
/// Returns the elements of the sequence, sorted using the given key path as the comparison between elements.
2643
@inlinable func sorted<Value: Comparable>(by predicate: (Element) -> Value) -> [Element] {
2744
return sorted(<, by: predicate)

0 commit comments

Comments
 (0)