@@ -45,11 +45,16 @@ public struct HTTPClientResponse: Sendable {
45
45
self . body = Body ( TransactionBody ( bag) )
46
46
}
47
47
48
- @inlinable public init ( ) {
49
- self . version = . http1_1
50
- self . status = . ok
51
- self . headers = [ : ]
52
- self . body = Body ( )
48
+ @inlinable public init (
49
+ version: HTTPVersion = . http1_1,
50
+ status: HTTPResponseStatus = . ok,
51
+ headers: HTTPHeaders = [ : ] ,
52
+ body: Body = Body ( )
53
+ ) {
54
+ self . version = version
55
+ self . status = status
56
+ self . headers = headers
57
+ self . body = body
53
58
}
54
59
}
55
60
@@ -62,7 +67,6 @@ extension HTTPClientResponse {
62
67
/// are entirely synthetic and have no semantic meaning.
63
68
public struct Body : AsyncSequence , Sendable {
64
69
public typealias Element = ByteBuffer
65
- @usableFromInline typealias Storage = Either < TransactionBody , AnyAsyncSequence < ByteBuffer > >
66
70
public struct AsyncIterator : AsyncIteratorProtocol {
67
71
@usableFromInline var storage : Storage . AsyncIterator
68
72
@@ -85,22 +89,70 @@ extension HTTPClientResponse {
85
89
86
90
@available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
87
91
extension HTTPClientResponse . Body {
88
- @inlinable init ( _ body: TransactionBody ) {
89
- self . storage = . a( body)
92
+ @usableFromInline enum Storage : Sendable {
93
+ case transaction( SingleIteratorPrecondition < TransactionBody > )
94
+ case anyAsyncSequence( AnyAsyncSequence < ByteBuffer > )
90
95
}
96
+ }
97
+
98
+ @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
99
+ extension HTTPClientResponse . Body . Storage : AsyncSequence {
100
+ @usableFromInline typealias Element = ByteBuffer
91
101
92
- @inlinable public init < SequenceOfBytes> (
93
- _ sequenceOfBytes: SequenceOfBytes
94
- ) where SequenceOfBytes: AsyncSequence & Sendable , SequenceOfBytes. Element == ByteBuffer {
95
- self . storage = . b( AnyAsyncSequence ( sequenceOfBytes) )
102
+ @inlinable func makeAsyncIterator( ) -> AsyncIterator {
103
+ switch self {
104
+ case . transaction( let transaction) :
105
+ return . transaction( transaction. makeAsyncIterator ( ) )
106
+ case . anyAsyncSequence( let anyAsyncSequence) :
107
+ return . anyAsyncSequence( anyAsyncSequence. makeAsyncIterator ( ) )
108
+ }
109
+ }
110
+ }
111
+
112
+ @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
113
+ extension HTTPClientResponse . Body . Storage {
114
+ @usableFromInline enum AsyncIterator {
115
+ case transaction( SingleIteratorPrecondition < TransactionBody > . AsyncIterator )
116
+ case anyAsyncSequence( AnyAsyncSequence < ByteBuffer > . AsyncIterator )
117
+ }
118
+ }
119
+
120
+ @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
121
+ extension HTTPClientResponse . Body . Storage . AsyncIterator : AsyncIteratorProtocol {
122
+ @inlinable mutating func next( ) async throws -> ByteBuffer ? {
123
+ switch self {
124
+ case . transaction( let iterator) :
125
+ return try await iterator. next ( )
126
+ case . anyAsyncSequence( var iterator) :
127
+ defer { self = . anyAsyncSequence( iterator) }
128
+ return try await iterator. next ( )
129
+ }
130
+ }
131
+ }
132
+
133
+
134
+ @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
135
+ extension HTTPClientResponse . Body {
136
+ init ( _ body: TransactionBody ) {
137
+ self . init ( . transaction( body. singleIteratorPrecondition) )
138
+ }
139
+
140
+ @usableFromInline init ( _ storage: Storage ) {
141
+ self . storage = storage
96
142
}
97
143
98
144
public init ( ) {
99
- self . init ( EmptyCollection ( ) . async )
145
+ self = . stream( EmptyCollection < ByteBuffer > ( ) . async )
146
+ }
147
+
148
+ @inlinable public static func stream< SequenceOfBytes> (
149
+ _ sequenceOfBytes: SequenceOfBytes
150
+ ) -> Self where SequenceOfBytes: AsyncSequence & Sendable , SequenceOfBytes. Element == ByteBuffer {
151
+ self . init ( . anyAsyncSequence( AnyAsyncSequence ( sequenceOfBytes. singleIteratorPrecondition) ) )
100
152
}
101
153
102
- public init ( _ byteBuffer: ByteBuffer ) {
103
- self . init ( CollectionOfOne ( byteBuffer) . async )
154
+ public static func bytes ( _ byteBuffer: ByteBuffer ) -> Self {
155
+ . stream ( CollectionOfOne ( byteBuffer) . async )
104
156
}
105
157
}
106
158
0 commit comments