Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Ceylo opened this issue Jun 30, 2024 · 0 comments
Open

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

Ceylo opened this issue Jun 30, 2024 · 0 comments

Comments

@Ceylo
Copy link

Ceylo commented Jun 30, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant