Skip to content

Commit

Permalink
Wrap buffer to make it @sendable (#5815)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Sep 30, 2024
1 parent 6d1cb60 commit c4a7325
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Source/SwiftLintCore/Extensions/Array+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,36 @@ public extension Array {
func parallelMap<T>(transform: (Element) -> T) -> [T] {
var result = ContiguousArray<T?>(repeating: nil, count: count)
return result.withUnsafeMutableBufferPointer { buffer in
let buffer = Wrapper(buffer: buffer)
DispatchQueue.concurrentPerform(iterations: buffer.count) { idx in
buffer[idx] = transform(self[idx])
}
return buffer.map { $0! }
return buffer.data
}
}

private class Wrapper<T>: @unchecked Sendable {
let buffer: UnsafeMutableBufferPointer<T?>

init(buffer: UnsafeMutableBufferPointer<T?>) {
self.buffer = buffer
}

var data: [T] {
buffer.map { $0! }
}

var count: Int {
buffer.count
}

subscript(index: Int) -> T {
get {
queuedFatalError("Do not call this getter.")
}
set(newValue) {
buffer[index] = newValue
}
}
}
}
Expand Down

0 comments on commit c4a7325

Please sign in to comment.