Swift 6's concurrency support/non mutable Element
Opened this issue · 1 comments
Ceylo commented
Hello,
I'm giving a try to Swift 6 and have to import SwiftSoup
with @preconcurrency
for now as it's not yet ready.
One case I have is this method:
extension Collection where Element: Sendable {
func parallelMap<T: Sendable>(_ transform: @Sendable @escaping (Self.Element) throws -> T) async rethrows -> [T] {
try await withThrowingTaskGroup(of: (Int, T).self) { group in
for (offset, element) in enumerated() {
group.addTask {
(offset, try transform(element))
}
}
return try await group
.reduce(into: [T?](repeating: nil, count: count),
{ $0[$1.0] = $1.1})
as! [T]
}
}
}
Which I then use to transform Elements
provided by SwiftSoup:
async let nodes = elements.parallelMap { element in ... }
From what I understand, since Element
is mutable (and thus not Sendable
), the Swift compiler has no way to know that parallelMap
won't mutate it from another concurrent context, while the calling context may also mutate it.
Could we have in the future some API providing sendable Element
s?
Thank you!
aehlke commented
I'll merge a PR for this if you have a solution in mind