16
16
import struct NIO. ByteBuffer
17
17
18
18
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
19
- public struct ByteBufferToUInt8AsyncSequence < Upstream: AsyncSequence > : AsyncSequence where Upstream. Element == ByteBuffer {
19
+ public struct NIOByteBufferToUInt8AsyncSequence < Upstream: AsyncSequence > : AsyncSequence where Upstream. Element == ByteBuffer {
20
20
public typealias Element = UInt8
21
21
public typealias AsyncIterator = Iterator
22
22
23
+ @usableFromInline
24
+ let upstream : Upstream
25
+
26
+ @inlinable
27
+ init ( _ upstream: Upstream ) {
28
+ self . upstream = upstream
29
+ }
30
+
31
+ @inlinable
32
+ public func makeAsyncIterator( ) -> Iterator {
33
+ Iterator ( self . upstream. makeAsyncIterator ( ) )
34
+ }
35
+
23
36
public struct Iterator: AsyncIteratorProtocol {
24
37
/*private but*/ @usableFromInline var state : State
25
38
@@ -28,10 +41,18 @@ public struct ByteBufferToUInt8AsyncSequence<Upstream: AsyncSequence>: AsyncSequ
28
41
case hasBuffer( ByteBuffer , Upstream . AsyncIterator )
29
42
case askForMore( Upstream . AsyncIterator )
30
43
case finished
31
- case modifying
44
+
45
+ @inlinable
46
+ init ( buffer: ByteBuffer , upstream: Upstream . AsyncIterator ) {
47
+ if buffer. readableBytes > 0 {
48
+ self = . hasBuffer( buffer, upstream)
49
+ } else {
50
+ self = . askForMore( upstream)
51
+ }
52
+ }
32
53
}
33
54
34
- @usableFromInline
55
+ @inlinable
35
56
init ( _ upstream: Upstream . AsyncIterator ) {
36
57
self . state = . askForMore( upstream)
37
58
}
@@ -40,21 +61,17 @@ public struct ByteBufferToUInt8AsyncSequence<Upstream: AsyncSequence>: AsyncSequ
40
61
public mutating func next( ) async throws -> Element ? {
41
62
switch self . state {
42
63
case . askForMore( var upstream) :
43
- self . state = . modifying
44
-
45
64
while true {
46
65
switch try await upstream. next ( ) {
47
66
case . some ( let nextBuffer) where nextBuffer. readableBytes == 0 :
48
- break
67
+ // we received an empty buffer. for this reason, let's continue and get the
68
+ // next buffer fro, the sequence
69
+ continue
49
70
50
71
case . some ( var nextBuffer) :
51
72
assert ( nextBuffer. readableBytes > 0 )
52
73
let result = nextBuffer. readInteger ( as: UInt8 . self)
53
- if nextBuffer. readableBytes > 0 {
54
- self . state = . hasBuffer( nextBuffer, upstream)
55
- } else {
56
- self . state = . askForMore( upstream)
57
- }
74
+ self . state = . init( buffer: nextBuffer, upstream: upstream)
58
75
return result
59
76
60
77
case . none:
@@ -65,50 +82,28 @@ public struct ByteBufferToUInt8AsyncSequence<Upstream: AsyncSequence>: AsyncSequ
65
82
66
83
case . hasBuffer ( var buffer, let upstream) :
67
84
assert( buffer. readableBytes > 0 )
68
- self. state = . modifying
69
-
70
85
let result = buffer. readInteger ( as: UInt8 . self)
71
- if buffer. readableBytes > 0 {
72
- self . state = . hasBuffer( buffer, upstream)
73
- } else {
74
- self . state = . askForMore( upstream)
75
- }
86
+ self. state = . init( buffer: buffer, upstream: upstream)
76
87
return result
77
88
78
89
case . finished:
79
90
return nil
80
-
81
- case . modifying :
82
- preconditionFailure( " Invalid state: \( self . state) " )
83
91
}
84
92
}
85
93
}
86
94
87
- @inlinable
88
- public func makeAsyncIterator( ) - > Iterator {
89
- Iterator ( self . upstream. makeAsyncIterator ( ) )
90
- }
91
-
92
- @usableFromInline
93
- let upstream: Upstream
94
-
95
- /*private but*/ @usableFromInline init( _ upstream: Upstream) {
96
- self . upstream = upstream
97
- }
98
95
}
99
96
100
- @usableFromInline
101
- struct TooManyBytesError: Error {
102
- @usableFromInline
103
- init( ) { }
97
+ public struct NIOTooManyBytesError: Error {
98
+ public init( ) { }
104
99
}
105
100
106
101
@available ( macOS 12 , iOS 15 , tvOS 15 , watchOS 8 , * )
107
102
extension AsyncSequence where Element == ByteBuffer {
108
103
/// Transform an AsyncSequence of ByteBuffers into an AsyncSequence of single bytes.
109
104
@inlinable
110
- public func toBytes( ) -> ByteBufferToUInt8AsyncSequence < Self > {
111
- ByteBufferToUInt8AsyncSequence ( self )
105
+ public func toBytes( ) -> NIOByteBufferToUInt 8 AsyncSequence < Self> {
106
+ NIOByteBufferToUInt 8 AsyncSequence ( self)
112
107
}
113
108
114
109
/// Consume an ``Swift/AsyncSequence`` of ``NIO/ByteBuffer``s into a single `ByteBuffer`.
@@ -124,13 +119,13 @@ extension AsyncSequence where Element == ByteBuffer {
124
119
125
120
var receivedBytes = buffer. readableBytes
126
121
if receivedBytes > maxBytes {
127
- throw TooManyBytesError ( )
122
+ throw NIOTooManyBytesError ( )
128
123
}
129
124
130
125
while var next = try await iterator. next ( ) {
131
126
receivedBytes += next. readableBytes
132
127
if receivedBytes > maxBytes {
133
- throw TooManyBytesError ( )
128
+ throw NIOTooManyBytesError ( )
134
129
}
135
130
136
131
buffer. writeBuffer ( & next)
0 commit comments