1717extension AsyncBufferSequence {
1818 /// A immutable collection of bytes
1919 public struct Buffer : Sendable {
20- #if canImport(Darwin)
20+ #if SUBPROCESS_ASYNCIO_DISPATCH
2121 // We need to keep the backingData alive while Slice is alive
2222 internal let backingData : DispatchData
2323 internal let data : DispatchData . Region
@@ -45,7 +45,7 @@ extension AsyncBufferSequence {
4545 internal static func createFrom( _ data: [ UInt8 ] ) -> [ Buffer ] {
4646 return [ . init( data: data) ]
4747 }
48- #endif // canImport(Darwin)
48+ #endif // SUBPROCESS_ASYNCIO_DISPATCH
4949 }
5050}
5151
@@ -92,7 +92,7 @@ extension AsyncBufferSequence.Buffer {
9292
9393// MARK: - Hashable, Equatable
9494extension AsyncBufferSequence . Buffer : Equatable , Hashable {
95- #if canImport(Darwin)
95+ #if SUBPROCESS_ASYNCIO_DISPATCH
9696 public static func == ( lhs: AsyncBufferSequence . Buffer , rhs: AsyncBufferSequence . Buffer ) -> Bool {
9797 return lhs. data == rhs. data
9898 }
@@ -104,7 +104,7 @@ extension AsyncBufferSequence.Buffer: Equatable, Hashable {
104104 // else Compiler generated conformances
105105}
106106
107- #if canImport(Darwin)
107+ #if SUBPROCESS_ASYNCIO_DISPATCH
108108extension DispatchData . Region {
109109 static func == ( lhs: DispatchData . Region , rhs: DispatchData . Region ) -> Bool {
110110 return lhs. withUnsafeBytes { lhsBytes in
@@ -120,5 +120,45 @@ extension DispatchData.Region {
120120 }
121121 }
122122}
123- #endif
123+ #if !canImport(Darwin)
124+ extension DispatchData {
125+ typealias Region = _ContiguousBufferView
126+
127+ var regions : [ Region ] {
128+ contiguousBufferViews
129+ }
130+
131+ /// Unfortunately `DispatchData.Region` is not available on Linux, hence our own wrapper
132+ internal struct _ContiguousBufferView : @unchecked Sendable , RandomAccessCollection {
133+ typealias Element = UInt8
134+
135+ internal let bytes : UnsafeBufferPointer < UInt8 >
136+
137+ internal var startIndex : Int { self . bytes. startIndex }
138+ internal var endIndex : Int { self . bytes. endIndex }
139+
140+ internal init ( bytes: UnsafeBufferPointer < UInt8 > ) {
141+ self . bytes = bytes
142+ }
143+
144+ internal func withUnsafeBytes< ResultType> ( _ body: ( UnsafeRawBufferPointer ) throws -> ResultType ) rethrows -> ResultType {
145+ return try body ( UnsafeRawBufferPointer ( self . bytes) )
146+ }
124147
148+ subscript( position: Int ) -> UInt8 {
149+ _read {
150+ yield self . bytes [ position]
151+ }
152+ }
153+ }
154+
155+ internal var contiguousBufferViews : [ _ContiguousBufferView ] {
156+ var slices = [ _ContiguousBufferView] ( )
157+ enumerateBytes { ( bytes, index, stop) in
158+ slices. append ( _ContiguousBufferView ( bytes: bytes) )
159+ }
160+ return slices
161+ }
162+ }
163+ #endif
164+ #endif
0 commit comments