Skip to content

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

Open
@Ceylo

Description

@Ceylo

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions