scinfu / SwiftSoup

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
https://scinfu.github.io/SwiftSoup/
MIT License
4.54k stars 347 forks source link

Swift 6's concurrency support/non mutable Element #271

Open Ceylo opened 4 months ago

Ceylo commented 4 months ago

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 Elements?

Thank you!

aehlke commented 2 months ago

I'll merge a PR for this if you have a solution in mind