@@ -35,25 +35,25 @@ public struct AsyncBufferSequence: AsyncSequence, Sendable {
35
35
@_nonSendable
36
36
public struct Iterator : AsyncIteratorProtocol {
37
37
public typealias Element = Buffer
38
- internal typealias Stream = AsyncThrowingStream < StreamStatus , Swift . Error >
38
+ internal typealias Stream = AsyncThrowingStream < Buffer , Swift . Error >
39
39
40
40
private let diskIO : DiskIO
41
41
private var buffer : [ UInt8 ]
42
42
private var currentPosition : Int
43
43
private var finished : Bool
44
44
private var streamIterator : Stream . AsyncIterator
45
45
private let continuation : Stream . Continuation
46
- private var needsNextChunk : Bool
46
+ private var bytesRemaining : Int
47
47
48
48
internal init ( diskIO: DiskIO , streamOptions: PlatformOptions . StreamOptions ) {
49
49
self . diskIO = diskIO
50
50
self . buffer = [ ]
51
51
self . currentPosition = 0
52
52
self . finished = false
53
- let ( stream, continuation) = AsyncThrowingStream < StreamStatus , Swift . Error > . makeStream ( )
53
+ let ( stream, continuation) = AsyncThrowingStream < Buffer , Swift . Error > . makeStream ( )
54
54
self . streamIterator = stream. makeAsyncIterator ( )
55
55
self . continuation = continuation
56
- self . needsNextChunk = true
56
+ self . bytesRemaining = 0
57
57
58
58
#if !os(Windows)
59
59
if let minimumBufferSize = streamOptions. minimumBufferSize {
@@ -68,28 +68,14 @@ public struct AsyncBufferSequence: AsyncSequence, Sendable {
68
68
69
69
public mutating func next( ) async throws -> Buffer ? {
70
70
71
- if needsNextChunk {
72
- diskIO . readChunk ( upToLength : readBufferSize , continuation : continuation )
73
- needsNextChunk = false
71
+ if bytesRemaining <= 0 {
72
+ bytesRemaining = readBufferSize
73
+ diskIO . stream ( upToLength : readBufferSize , continuation : continuation )
74
74
}
75
75
76
- if let status = try await streamIterator. next ( ) {
77
- switch status {
78
- case . data( let data) :
79
- return data
80
-
81
- case . endOfChunk( let data) :
82
- needsNextChunk = true
83
- return data
84
-
85
- case . endOfFile:
86
- #if os(Windows)
87
- try self . diskIO. close ( )
88
- #else
89
- self . diskIO. close ( )
90
- #endif
91
- return nil
92
- }
76
+ if let buffer = try await streamIterator. next ( ) {
77
+ bytesRemaining -= buffer. count
78
+ return buffer
93
79
} else {
94
80
#if os(Windows)
95
81
try self . diskIO. close ( )
@@ -114,17 +100,6 @@ public struct AsyncBufferSequence: AsyncSequence, Sendable {
114
100
}
115
101
}
116
102
117
- extension AsyncBufferSequence {
118
- #if SubprocessSpan
119
- @available ( SubprocessSpan, * )
120
- #endif
121
- internal enum StreamStatus {
122
- case data( AsyncBufferSequence . Buffer )
123
- case endOfChunk( AsyncBufferSequence . Buffer )
124
- case endOfFile
125
- }
126
- }
127
-
128
103
// MARK: - Page Size
129
104
import _SubprocessCShims
130
105
0 commit comments