Skip to content

Can MultiProducerSingleConsumerAsyncChannel.Source be Sendable? #402

@blindspotbounty

Description

@blindspotbounty

I would like to use MPSCAsyncChannel as following:

private final class ShardedData: Sendable {
    typealias Channel = MultiProducerSingleConsumerAsyncChannel<Int, Never>
    let shards: UniqueArray<Channel.Source>

    func send(idx: Int, value: Int) async throws {
        try await shards[idx].send(value)
    }
}

Unfortunately Source is mutating on send() and non-sendable itself.
I wonder if that is intentional and if it is safe to workaround it wrapping into @uncheked Sendable class:

private final class ShardedData: Sendable {
    typealias Channel = MultiProducerSingleConsumerAsyncChannel<Int, Never>

    final class SourceWrap: @unchecked Sendable {
        var source: Channel.Source
        init(source: consuming Channel.Source) {
            self.source = source
        }
    }

    let shards: UniqueArray<SourceWrap>

    func send(idx: Int, value: Int) async throws {
        try await shards[idx].source.send(value)
    }
}

or/and as:

    private nonisolated(unsafe) var shards: UniqueArray<Channel.Source>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions