|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Async Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#if UnstableAsyncStreaming && compiler(>=6.4) |
| 13 | +public import ContainersPreview |
| 14 | +public import BasicContainers |
| 15 | + |
| 16 | +/// An ``AsyncWriter`` that is implemented in terms of a ``CallerAsyncWriter``. |
| 17 | +/// |
| 18 | +/// The adapter allocates a fresh buffer for each ``write(_:)`` call, runs |
| 19 | +/// the body to fill it, and immediately drains it into the underlying |
| 20 | +/// ``CallerAsyncWriter``. Writes are flushed *eagerly*: the adapter does |
| 21 | +/// not defer the most recent buffer to fuse it with ``finish(finalElement:)``. |
| 22 | +/// |
| 23 | +/// Eager flushing keeps request/response patterns deadlock-free — a write |
| 24 | +/// is observable to the peer as soon as the underlying writer accepts it. |
| 25 | +/// The trade-off is that fused close (HTTP/2 DATA+END_STREAM coalescing, |
| 26 | +/// and similar) is not available through this adapter; the underlying |
| 27 | +/// ``CallerAsyncWriter/finish(buffer:finalElement:)`` always receives an |
| 28 | +/// empty buffer. Conformers that need fused close should implement |
| 29 | +/// ``AsyncWriter`` directly rather than going through this adapter. |
| 30 | +@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) |
| 31 | +public struct CallerAsyncWriterAsyncWriterAdapter< |
| 32 | + Underlying: CallerAsyncWriter & ~Copyable, |
| 33 | + Buffer: DynamicContainer<Underlying.WriteElement> & ~Copyable |
| 34 | +>: ~Copyable, AsyncWriter { |
| 35 | + public typealias WriteElement = Underlying.WriteElement |
| 36 | + public typealias WriteFailure = Underlying.WriteFailure |
| 37 | + public typealias FinalElement = Underlying.FinalElement |
| 38 | + |
| 39 | + @usableFromInline |
| 40 | + var underlying: Underlying |
| 41 | + |
| 42 | + @usableFromInline |
| 43 | + let initialCapacity: Int |
| 44 | + |
| 45 | + @inlinable |
| 46 | + init(underlying: consuming Underlying, initialCapacity: Int) { |
| 47 | + self.underlying = underlying |
| 48 | + self.initialCapacity = initialCapacity |
| 49 | + } |
| 50 | + |
| 51 | + @inlinable |
| 52 | + public mutating func write<Return: ~Copyable, Failure: Error>( |
| 53 | + _ body: (inout Buffer) async throws(Failure) -> Return |
| 54 | + ) async throws(EitherError<WriteFailure, Failure>) -> Return { |
| 55 | + var buffer = Buffer(minimumCapacity: self.initialCapacity) |
| 56 | + let result: Return |
| 57 | + do throws(Failure) { |
| 58 | + result = try await body(&buffer) |
| 59 | + } catch { |
| 60 | + throw .second(error) |
| 61 | + } |
| 62 | + do throws(WriteFailure) { |
| 63 | + try await self.underlying.write(buffer: &buffer) |
| 64 | + } catch { |
| 65 | + throw .first(error) |
| 66 | + } |
| 67 | + return result |
| 68 | + } |
| 69 | + |
| 70 | + @inlinable |
| 71 | + public consuming func finish( |
| 72 | + finalElement: consuming FinalElement |
| 73 | + ) async throws(WriteFailure) { |
| 74 | + var empty = Buffer() |
| 75 | + try await self.underlying.finish(buffer: &empty, finalElement: finalElement) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) |
| 80 | +extension CallerAsyncWriter where Self: ~Copyable { |
| 81 | + /// Adapts this ``CallerAsyncWriter`` to an ``AsyncWriter``, using |
| 82 | + /// ``UniqueArray`` as the buffer container. |
| 83 | + /// |
| 84 | + /// Each ``AsyncWriter/write(_:)`` call on the returned adapter |
| 85 | + /// allocates a fresh buffer, runs the closure to fill it, and |
| 86 | + /// immediately drains it into this writer. Writes are flushed |
| 87 | + /// eagerly — see ``CallerAsyncWriterAsyncWriterAdapter`` for the |
| 88 | + /// trade-off this implies for fused close. |
| 89 | + /// |
| 90 | + /// - Parameter initialCapacity: The capacity reserved on each |
| 91 | + /// freshly allocated buffer. |
| 92 | + /// - Returns: An adapter that conforms to ``AsyncWriter``. |
| 93 | + @inlinable |
| 94 | + public consuming func asAsyncWriter( |
| 95 | + initialCapacity: Int = 4096 |
| 96 | + ) -> CallerAsyncWriterAsyncWriterAdapter<Self, UniqueArray<WriteElement>> { |
| 97 | + .init(underlying: self, initialCapacity: initialCapacity) |
| 98 | + } |
| 99 | + |
| 100 | + /// Adapts this ``CallerAsyncWriter`` to an ``AsyncWriter`` with a |
| 101 | + /// caller-chosen buffer container type. |
| 102 | + /// |
| 103 | + /// - Parameters: |
| 104 | + /// - bufferType: The container type for buffers handed to the |
| 105 | + /// ``AsyncWriter/write(_:)`` body. |
| 106 | + /// - initialCapacity: The capacity reserved on each freshly |
| 107 | + /// allocated buffer. |
| 108 | + /// - Returns: An adapter that conforms to ``AsyncWriter``. |
| 109 | + @inlinable |
| 110 | + public consuming func asAsyncWriter<Buffer>( |
| 111 | + bufferOf bufferType: Buffer.Type, |
| 112 | + initialCapacity: Int = 4096 |
| 113 | + ) -> CallerAsyncWriterAsyncWriterAdapter<Self, Buffer> |
| 114 | + where Buffer: DynamicContainer<WriteElement> & ~Copyable { |
| 115 | + .init(underlying: self, initialCapacity: initialCapacity) |
| 116 | + } |
| 117 | +} |
| 118 | +#endif |
0 commit comments