@@ -35,25 +35,25 @@ public struct AsyncBufferSequence: AsyncSequence, Sendable {
3535 @_nonSendable
3636 public struct Iterator : AsyncIteratorProtocol {
3737 public typealias Element = Buffer
38- internal typealias Stream = AsyncThrowingStream < StreamStatus , Swift . Error >
38+ internal typealias Stream = AsyncThrowingStream < Buffer , Swift . Error >
3939
4040 private let diskIO : DiskIO
4141 private var buffer : [ UInt8 ]
4242 private var currentPosition : Int
4343 private var finished : Bool
4444 private var streamIterator : Stream . AsyncIterator
4545 private let continuation : Stream . Continuation
46- private var needsNextChunk : Bool
46+ private var bytesRemaining : Int
4747
4848 internal init ( diskIO: DiskIO , streamOptions: PlatformOptions . StreamOptions ) {
4949 self . diskIO = diskIO
5050 self . buffer = [ ]
5151 self . currentPosition = 0
5252 self . finished = false
53- let ( stream, continuation) = AsyncThrowingStream < StreamStatus , Swift . Error > . makeStream ( )
53+ let ( stream, continuation) = AsyncThrowingStream < Buffer , Swift . Error > . makeStream ( )
5454 self . streamIterator = stream. makeAsyncIterator ( )
5555 self . continuation = continuation
56- self . needsNextChunk = true
56+ self . bytesRemaining = 0
5757
5858 #if !os(Windows)
5959 if let minimumBufferSize = streamOptions. minimumBufferSize {
@@ -68,28 +68,14 @@ public struct AsyncBufferSequence: AsyncSequence, Sendable {
6868
6969 public mutating func next( ) async throws -> Buffer ? {
7070
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 )
7474 }
7575
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
9379 } else {
9480 #if os(Windows)
9581 try self . diskIO. close ( )
@@ -114,17 +100,6 @@ public struct AsyncBufferSequence: AsyncSequence, Sendable {
114100 }
115101}
116102
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-
128103// MARK: - Page Size
129104import _SubprocessCShims
130105
0 commit comments