@@ -67,17 +67,13 @@ public struct AsyncBufferedByteIterator: AsyncIteratorProtocol, Sendable {
6767@frozen @usableFromInline
6868internal struct _AsyncBytesBuffer : @unchecked Sendable {
6969 @usableFromInline
70- final class Storage {
71- fileprivate let readFunction : @Sendable ( UnsafeMutableRawBufferPointer) async throws -> Int
70+ final class Storage : Sendable {
7271 fileprivate let buffer : UnsafeMutableRawBufferPointer
73- fileprivate var finished = false
7472
7573 init (
76- capacity: Int ,
77- readFunction: @Sendable @escaping ( UnsafeMutableRawBufferPointer) async throws -> Int
74+ capacity: Int
7875 ) {
7976 precondition ( capacity > 0 )
80- self . readFunction = readFunction
8177 buffer = UnsafeMutableRawBufferPointer . allocate (
8278 byteCount: capacity,
8379 alignment: MemoryLayout< AnyObject> . alignment
@@ -93,33 +89,37 @@ internal struct _AsyncBytesBuffer: @unchecked Sendable {
9389 @usableFromInline internal var nextPointer : UnsafeRawPointer
9490 @usableFromInline internal var endPointer : UnsafeRawPointer
9591
92+ internal let readFunction : @Sendable ( UnsafeMutableRawBufferPointer) async throws -> Int
93+ internal var finished = false
94+
9695 @usableFromInline init (
9796 capacity: Int ,
9897 readFunction: @Sendable @escaping ( UnsafeMutableRawBufferPointer) async throws -> Int
9998 ) {
100- let s = Storage ( capacity: capacity, readFunction: readFunction)
99+ let s = Storage ( capacity: capacity)
100+ self . readFunction = readFunction
101101 storage = s
102102 nextPointer = UnsafeRawPointer ( s. buffer. baseAddress!)
103103 endPointer = nextPointer
104104 }
105105
106106 @inline ( never) @usableFromInline
107107 internal mutating func reloadBufferAndNext( ) async throws -> UInt8 ? {
108- if storage . finished {
108+ if finished {
109109 return nil
110110 }
111111 try Task . checkCancellation ( )
112112 do {
113- let readSize : Int = try await storage . readFunction ( storage. buffer)
113+ let readSize : Int = try await readFunction ( storage. buffer)
114114 if readSize == 0 {
115- storage . finished = true
115+ finished = true
116116 nextPointer = endPointer
117117 return nil
118118 }
119119 nextPointer = UnsafeRawPointer ( storage. buffer. baseAddress!)
120120 endPointer = nextPointer + readSize
121121 } catch {
122- storage . finished = true
122+ finished = true
123123 nextPointer = endPointer
124124 throw error
125125 }
0 commit comments