1- # MultiProducerSingleConsumerChannel
1+ # MultiProducerSingleConsumerAsyncChannel
22
33* Proposal: [ SAA-0016] ( 0016-multi-producer-single-consumer-channel.md )
44* Authors: [ Franz Busch] ( https://github.com/FranzBusch )
@@ -129,24 +129,24 @@ The above motivation lays out the expected behaviors for any consumer/producer
129129system and compares them to the behaviors of ` Async[Throwing]Stream ` and
130130` Async[Throwing]Channel ` .
131131
132- This section proposes a new type called ` MultiProducerSingleConsumerChannel `
132+ This section proposes a new type called ` MultiProducerSingleConsumerAsyncChannel `
133133that implement all of the above-mentioned behaviors. Importantly, this proposed
134134solution is taking advantage of ` ~Copyable ` types to model the
135135multi-producer-single-consumer behavior. While the current ` AsyncSequence `
136136protocols are not supporting ` ~Copyable ` types we provide a way to convert the
137137proposed channel to an asynchronous sequence. This leaves us room to support any
138138potential future asynchronous streaming protocol that supports ` ~Copyable ` .
139139
140- ### Creating a MultiProducerSingleConsumerChannel
140+ ### Creating a MultiProducerSingleConsumerAsyncChannel
141141
142- You can create an ` MultiProducerSingleConsumerChannel ` instance using the
142+ You can create an ` MultiProducerSingleConsumerAsyncChannel ` instance using the
143143` makeChannel(of: backpressureStrategy:) ` method. This method returns you the
144144channel and the source. The source can be used to send new values to the
145145asynchronous channel. The new API specifically provides a
146146multi-producer/single-consumer pattern.
147147
148148``` swift
149- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
149+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
150150 of : Int .self ,
151151 backpressureStrategy : .watermark (low : 2 , high : 4 )
152152)
192192```
193193
194194The above API offers the most control and highest performance when bridging a
195- synchronous producer to a ` MultiProducerSingleConsumerChannel ` . First, you have
195+ synchronous producer to a ` MultiProducerSingleConsumerAsyncChannel ` . First, you have
196196to send values using the ` send(contentsOf:) ` which returns a ` SendResult ` . The
197197result either indicates that more values should be produced or that a callback
198198should be enqueued by calling the `enqueueCallback(callbackToken:
@@ -221,7 +221,7 @@ try await source.send(contentsOf: sequence)
221221```
222222
223223With the above APIs, we should be able to effectively bridge any system into a
224- ` MultiProducerSingleConsumerChannel ` regardless if the system is callback-based,
224+ ` MultiProducerSingleConsumerAsyncChannel ` regardless if the system is callback-based,
225225blocking, or asynchronous.
226226
227227### Multi producer
@@ -232,7 +232,7 @@ region than the original source allowing to pass it into a different isolation
232232region to concurrently produce elements.
233233
234234``` swift
235- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
235+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
236236 of : Int .self ,
237237 backpressureStrategy : .watermark (low : 2 , high : 5 )
238238)
@@ -262,7 +262,7 @@ this:
262262
263263``` swift
264264// Termination through calling finish
265- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
265+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
266266 of : Int .self ,
267267 backpressureStrategy : .watermark (low : 2 , high : 4 )
268268)
@@ -280,7 +280,7 @@ If the channel has a failure type it can also be finished with an error.
280280
281281``` swift
282282// Termination through calling finish
283- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
283+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
284284 of : Int .self ,
285285 throwing : SomeError.self ,
286286 backpressureStrategy : .watermark (low : 2 , high : 4 )
@@ -301,7 +301,7 @@ will happen automatically when the source is last used or explicitly consumed.
301301
302302``` swift
303303// Termination through deiniting the source
304- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
304+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
305305 of : Int .self ,
306306 backpressureStrategy : .watermark (low : 2 , high : 4 )
307307)
@@ -322,7 +322,7 @@ callback. Termination of the producer happens in the following scenarios:
322322
323323``` swift
324324// Termination through task cancellation
325- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
325+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
326326 of : Int .self ,
327327 backpressureStrategy : .watermark (low : 2 , high : 4 )
328328)
@@ -338,7 +338,7 @@ task.cancel() // Prints Terminated
338338
339339``` swift
340340// Termination through deiniting the channel
341- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
341+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
342342 of : Int .self ,
343343 backpressureStrategy : .watermark (low : 2 , high : 4 )
344344)
@@ -350,7 +350,7 @@ _ = consume channel // Prints Terminated
350350
351351``` swift
352352// Termination through finishing the source and consuming the last element
353- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
353+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
354354 of : Int .self ,
355355 backpressureStrategy : .watermark (low : 2 , high : 4 )
356356)
@@ -367,7 +367,7 @@ await channel.next() // Prints Terminated
367367
368368``` swift
369369// Termination through deiniting the last source and consuming the last element
370- let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel (
370+ let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel (
371371 of : Int .self ,
372372 backpressureStrategy : .watermark (low : 2 , high : 4 )
373373)
@@ -394,29 +394,29 @@ producer has been terminated will result in an error thrown from the send method
394394``` swift
395395#if compiler (>= 6.1 )
396396/// An error that is thrown from the various `send` methods of the
397- /// ``MultiProducerSingleConsumerChannel /Source``.
397+ /// ``MultiProducerSingleConsumerAsyncChannel /Source``.
398398///
399399/// This error is thrown when the channel is already finished when
400400/// trying to send new elements to the source.
401- public struct MultiProducerSingleConsumerChannelAlreadyFinishedError : Error { }
401+ public struct MultiProducerSingleConsumerAsyncChannelAlreadyFinishedError : Error { }
402402
403403/// A multi-producer single-consumer channel.
404404///
405- /// The ``MultiProducerSingleConsumerChannel `` provides a ``MultiProducerSingleConsumerChannel /Source`` to
405+ /// The ``MultiProducerSingleConsumerAsyncChannel `` provides a ``MultiProducerSingleConsumerAsyncChannel /Source`` to
406406/// send values to the channel. The channel supports different back pressure strategies to control the
407407/// buffering and demand. The channel will buffer values until its backpressure strategy decides that the
408408/// producer have to wait.
409409///
410410/// This channel is also suitable for the single-producer single-consumer use-case
411411///
412- /// ## Using a MultiProducerSingleConsumerChannel
412+ /// ## Using a MultiProducerSingleConsumerAsyncChannel
413413///
414- /// To use a ``MultiProducerSingleConsumerChannel `` you have to create a new channel with its source first by calling
415- /// the ``MultiProducerSingleConsumerChannel /makeChannel(of:throwing:BackpressureStrategy:)`` method.
414+ /// To use a ``MultiProducerSingleConsumerAsyncChannel `` you have to create a new channel with its source first by calling
415+ /// the ``MultiProducerSingleConsumerAsyncChannel /makeChannel(of:throwing:BackpressureStrategy:)`` method.
416416/// Afterwards, you can pass the source to the producer and the channel to the consumer.
417417///
418418/// ```
419- /// let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel(
419+ /// let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel(
420420/// of: Int.self,
421421/// backpressureStrategy: .watermark(low: 2, high: 4)
422422/// )
@@ -428,8 +428,8 @@ public struct MultiProducerSingleConsumerChannelAlreadyFinishedError: Error { }
428428///
429429/// ### Asynchronous producing
430430///
431- /// Values can be send to the source from asynchronous contexts using ``MultiProducerSingleConsumerChannel /Source/send(_:)-8eo96``
432- /// and ``MultiProducerSingleConsumerChannel /Source/send(contentsOf:)``. Backpressure results in calls
431+ /// Values can be send to the source from asynchronous contexts using ``MultiProducerSingleConsumerAsyncChannel /Source/send(_:)-8eo96``
432+ /// and ``MultiProducerSingleConsumerAsyncChannel /Source/send(contentsOf:)``. Backpressure results in calls
433433/// to the `send` methods to be suspended. Once more elements should be produced the `send` methods will be resumed.
434434///
435435/// ```
@@ -495,14 +495,14 @@ public struct MultiProducerSingleConsumerChannelAlreadyFinishedError: Error { }
495495/// getting cancelled, the source will get notified about the termination if a termination callback has been set
496496/// before by calling ``Source/setOnTerminationCallback(_:)``.
497497@available (macOS 15.0 , iOS 18.0 , watchOS 11.0 , tvOS 18.0 , visionOS 2.0 , * )
498- public struct MultiProducerSingleConsumerChannel <Element , Failure : Error >: ~ Copyable {
498+ public struct MultiProducerSingleConsumerAsyncChannel <Element , Failure : Error >: ~ Copyable {
499499 /// A struct containing the initialized channel and source.
500500 ///
501501 /// This struct can be deconstructed by consuming the individual
502502 /// components from it.
503503 ///
504504 /// ```swift
505- /// let channelAndSource = MultiProducerSingleConsumerChannel .makeChannel(
505+ /// let channelAndSource = MultiProducerSingleConsumerAsyncChannel .makeChannel(
506506 /// of: Int.self,
507507 /// backpressureStrategy: .watermark(low: 5, high: 10)
508508 /// )
@@ -512,12 +512,12 @@ public struct MultiProducerSingleConsumerChannel<Element, Failure: Error>: ~Copy
512512 @frozen
513513 public struct ChannelAndStream : ~ Copyable {
514514 /// The channel.
515- public var channel: MultiProducerSingleConsumerChannel
515+ public var channel: MultiProducerSingleConsumerAsyncChannel
516516 /// The source.
517517 public var source: Source
518518 }
519519
520- /// Initializes a new ``MultiProducerSingleConsumerChannel `` and an ``MultiProducerSingleConsumerChannel /Source``.
520+ /// Initializes a new ``MultiProducerSingleConsumerAsyncChannel `` and an ``MultiProducerSingleConsumerAsyncChannel /Source``.
521521 ///
522522 /// - Parameters:
523523 /// - elementType: The element type of the channel.
@@ -547,7 +547,7 @@ public struct MultiProducerSingleConsumerChannel<Element, Failure: Error>: ~Copy
547547}
548548
549549@available (macOS 15.0 , iOS 18.0 , watchOS 11.0 , tvOS 18.0 , visionOS 2.0 , * )
550- extension MultiProducerSingleConsumerChannel {
550+ extension MultiProducerSingleConsumerAsyncChannel {
551551 /// A struct to send values to the channel.
552552 ///
553553 /// Use this source to provide elements to the channel by calling one of the `send` methods.
@@ -583,18 +583,18 @@ extension MultiProducerSingleConsumerChannel {
583583 /// A type that indicates the result of sending elements to the source.
584584 public enum SendResult : ~ Copyable , Sendable {
585585 /// An opaque token that is returned when the channel's backpressure strategy indicated that production should
586- /// be suspended. Use this token to enqueue a callback by calling the ``MultiProducerSingleConsumerChannel /Source/enqueueCallback(callbackToken:onProduceMore:)`` method.
586+ /// be suspended. Use this token to enqueue a callback by calling the ``MultiProducerSingleConsumerAsyncChannel /Source/enqueueCallback(callbackToken:onProduceMore:)`` method.
587587 ///
588- /// - Important: This token must only be passed once to ``MultiProducerSingleConsumerChannel /Source/enqueueCallback(callbackToken:onProduceMore:)``
589- /// and ``MultiProducerSingleConsumerChannel /Source/cancelCallback(callbackToken:)``.
588+ /// - Important: This token must only be passed once to ``MultiProducerSingleConsumerAsyncChannel /Source/enqueueCallback(callbackToken:onProduceMore:)``
589+ /// and ``MultiProducerSingleConsumerAsyncChannel /Source/cancelCallback(callbackToken:)``.
590590 public struct CallbackToken : Sendable , Hashable { }
591591
592592 /// Indicates that more elements should be produced and send to the source.
593593 case produceMore
594594
595595 /// Indicates that a callback should be enqueued.
596596 ///
597- /// The associated token should be passed to the ````MultiProducerSingleConsumerChannel /Source/enqueueCallback(callbackToken:onProduceMore:)```` method.
597+ /// The associated token should be passed to the ````MultiProducerSingleConsumerAsyncChannel /Source/enqueueCallback(callbackToken:onProduceMore:)```` method.
598598 case enqueueCallback (CallbackToken)
599599 }
600600
@@ -728,7 +728,7 @@ extension MultiProducerSingleConsumerChannel {
728728
729729 /// Indicates that the production terminated.
730730 ///
731- /// After all buffered elements are consumed the subsequent call to ``MultiProducerSingleConsumerChannel /next(isolation:)`` will return
731+ /// After all buffered elements are consumed the subsequent call to ``MultiProducerSingleConsumerAsyncChannel /next(isolation:)`` will return
732732 /// `nil` or throw an error.
733733 ///
734734 /// Calling this function more than once has no effect. After calling finish, the channel enters a terminal state and doesn't accept
@@ -741,7 +741,7 @@ extension MultiProducerSingleConsumerChannel {
741741}
742742
743743@available (macOS 15.0 , iOS 18.0 , watchOS 11.0 , tvOS 18.0 , visionOS 2.0 , * )
744- extension MultiProducerSingleConsumerChannel {
744+ extension MultiProducerSingleConsumerAsyncChannel {
745745 /// Converts the channel to an asynchronous sequence for consumption.
746746 ///
747747 /// - Important: The returned asynchronous sequence only supports a single iterator to be created and
0 commit comments