17
17
extension AsyncBufferSequence {
18
18
/// A immutable collection of bytes
19
19
public struct Buffer : Sendable {
20
- #if os(Windows)
21
- internal let data : [ UInt8 ]
22
-
23
- internal init ( data: [ UInt8 ] ) {
24
- self . data = data
25
- }
26
-
27
- internal static func createFrom( _ data: [ UInt8 ] ) -> [ Buffer ] {
28
- return [ . init( data: data) ]
29
- }
30
- #else
31
- // We need to keep the backingData alive while _ContiguousBufferView is alive
20
+ #if canImport(Darwin)
21
+ // We need to keep the backingData alive while Slice is alive
32
22
internal let backingData : DispatchData
33
23
internal let data : DispatchData . _ContiguousBufferView
34
24
@@ -45,7 +35,17 @@ extension AsyncBufferSequence {
45
35
}
46
36
return slices. map { . init( data: $0, backingData: data) }
47
37
}
48
- #endif
38
+ #else
39
+ internal let data : [ UInt8 ]
40
+
41
+ internal init ( data: [ UInt8 ] ) {
42
+ self . data = data
43
+ }
44
+
45
+ internal static func createFrom( _ data: [ UInt8 ] ) -> [ Buffer ] {
46
+ return [ . init( data: data) ]
47
+ }
48
+ #endif // canImport(Darwin)
49
49
}
50
50
}
51
51
@@ -92,26 +92,23 @@ extension AsyncBufferSequence.Buffer {
92
92
93
93
// MARK: - Hashable, Equatable
94
94
extension AsyncBufferSequence . Buffer : Equatable , Hashable {
95
- #if os(Windows)
96
- // Compiler generated conformances
97
- #else
95
+ #if canImport(Darwin)
98
96
public static func == ( lhs: AsyncBufferSequence . Buffer , rhs: AsyncBufferSequence . Buffer ) -> Bool {
99
- return lhs. data. elementsEqual ( rhs. data)
97
+ return lhs. data == rhs. data
100
98
}
101
99
102
100
public func hash( into hasher: inout Hasher ) {
103
- self . data. withUnsafeBytes { ptr in
104
- hasher. combine ( bytes: ptr)
105
- }
101
+ hasher. combine ( self . data)
106
102
}
107
103
#endif
104
+ // else Compiler generated conformances
108
105
}
109
106
110
107
// MARK: - DispatchData.Block
111
108
#if canImport(Darwin) || canImport(Glibc) || canImport(Android) || canImport(Musl)
112
109
extension DispatchData {
113
110
/// Unfortunately `DispatchData.Region` is not available on Linux, hence our own wrapper
114
- internal struct _ContiguousBufferView : @unchecked Sendable , RandomAccessCollection {
111
+ internal struct _ContiguousBufferView : @unchecked Sendable , RandomAccessCollection , Hashable {
115
112
typealias Element = UInt8
116
113
117
114
internal let bytes : UnsafeBufferPointer < UInt8 >
@@ -127,6 +124,14 @@ extension DispatchData {
127
124
return try body ( UnsafeRawBufferPointer ( self . bytes) )
128
125
}
129
126
127
+ internal func hash( into hasher: inout Hasher ) {
128
+ hasher. combine ( bytes: UnsafeRawBufferPointer ( self . bytes) )
129
+ }
130
+
131
+ internal static func == ( lhs: DispatchData . _ContiguousBufferView , rhs: DispatchData . _ContiguousBufferView ) -> Bool {
132
+ return lhs. bytes. elementsEqual ( rhs. bytes)
133
+ }
134
+
130
135
subscript( position: Int ) -> UInt8 {
131
136
_read {
132
137
yield self . bytes [ position]
0 commit comments