@@ -32,19 +32,6 @@ public struct OutputRawSpan: ~Copyable, ~Escapable {
32
32
capacity = 0
33
33
_count = 0
34
34
}
35
-
36
- @unsafe
37
- @_alwaysEmitIntoClient
38
- @lifetime ( borrow start)
39
- internal init (
40
- _unchecked start: UnsafeMutableRawPointer ? ,
41
- capacity: Int ,
42
- initializedCount: Int
43
- ) {
44
- unsafe _pointer = start
45
- self . capacity = capacity
46
- _count = initializedCount
47
- }
48
35
}
49
36
50
37
@available ( SwiftStdlib 6 . 2 , * )
@@ -53,6 +40,8 @@ extension OutputRawSpan: @unchecked Sendable {}
53
40
@available ( SwiftStdlib 6 . 2 , * )
54
41
extension OutputRawSpan {
55
42
@_alwaysEmitIntoClient
43
+ @_transparent
44
+ @unsafe
56
45
internal func _start( ) -> UnsafeMutableRawPointer {
57
46
unsafe _pointer. _unsafelyUnwrappedUnchecked
58
47
}
@@ -64,16 +53,23 @@ extension OutputRawSpan {
64
53
// NOTE: `_pointer` must be known to be not-nil.
65
54
unsafe _start( ) . advanced ( by: _count)
66
55
}
56
+ }
67
57
58
+ @available ( SwiftStdlib 6 . 2 , * )
59
+ extension OutputRawSpan {
60
+ /// The number of initialized bytes in this span.
68
61
@_alwaysEmitIntoClient
69
- public var freeCapacity : Int { capacity &- _count }
62
+ public var byteCount : Int { _count }
70
63
64
+ /// The number of additional bytes that can be appended to this span.
71
65
@_alwaysEmitIntoClient
72
- public var byteCount : Int { _count }
66
+ public var freeCapacity : Int { capacity &- _count }
73
67
68
+ /// A Boolean value indicating whether the span is empty.
74
69
@_alwaysEmitIntoClient
75
70
public var isEmpty : Bool { _count == 0 }
76
71
72
+ /// A Boolean value indicating whether the span is full.
77
73
@_alwaysEmitIntoClient
78
74
public var isFull : Bool { _count == capacity }
79
75
}
@@ -102,7 +98,7 @@ extension OutputRawSpan {
102
98
///
103
99
/// - Parameters:
104
100
/// - buffer: an `UnsafeMutableBufferPointer` to be initialized
105
- /// - initializedCount: the number of initialized elements
101
+ /// - initializedCount: the number of initialized bytes
106
102
/// at the beginning of `buffer`.
107
103
@unsafe
108
104
@_alwaysEmitIntoClient
@@ -135,8 +131,9 @@ extension OutputRawSpan {
135
131
///
136
132
/// - Parameters:
137
133
/// - buffer: an `UnsafeMutableBufferPointer` to be initialized
138
- /// - initializedCount: the number of initialized elements
134
+ /// - initializedCount: the number of initialized bytes
139
135
/// at the beginning of `buffer`.
136
+ @unsafe
140
137
@_alwaysEmitIntoClient
141
138
@lifetime ( borrow buffer)
142
139
public init (
@@ -154,6 +151,7 @@ extension OutputRawSpan {
154
151
@available ( SwiftStdlib 6 . 2 , * )
155
152
extension OutputRawSpan {
156
153
154
+ /// Append a single byte to this span.
157
155
@_alwaysEmitIntoClient
158
156
@lifetime ( self : copy self )
159
157
public mutating func append( _ value: UInt8 ) {
@@ -162,6 +160,7 @@ extension OutputRawSpan {
162
160
_count &+= 1
163
161
}
164
162
163
+ /// Remove the last byte from this span.
165
164
@_alwaysEmitIntoClient
166
165
@discardableResult
167
166
@lifetime ( self : copy self )
@@ -171,6 +170,10 @@ extension OutputRawSpan {
171
170
return unsafe _tail( ) . load ( as: UInt8 . self)
172
171
}
173
172
173
+ /// Remove the last N elements, returning the memory they occupy
174
+ /// to the uninitialized state.
175
+ ///
176
+ /// `n` must not be greater than `count`
174
177
@_alwaysEmitIntoClient
175
178
@lifetime ( self : copy self )
176
179
public mutating func removeLast( _ k: Int ) {
@@ -179,6 +182,8 @@ extension OutputRawSpan {
179
182
_count &-= k
180
183
}
181
184
185
+ /// Remove all this span's elements and return its memory
186
+ /// to the uninitialized state.
182
187
@_alwaysEmitIntoClient
183
188
@lifetime ( self : copy self )
184
189
public mutating func removeAll( ) {
@@ -190,7 +195,7 @@ extension OutputRawSpan {
190
195
//MARK: bulk-append functions
191
196
@available ( SwiftStdlib 6 . 2 , * )
192
197
extension OutputRawSpan {
193
-
198
+ /// Appends the given value's bytes to this span's bytes.
194
199
@_alwaysEmitIntoClient
195
200
@lifetime ( self : copy self )
196
201
public mutating func append< T: BitwiseCopyable > ( _ value: T , as type: T . Type ) {
@@ -201,6 +206,7 @@ extension OutputRawSpan {
201
206
_count &+= MemoryLayout< T> . size
202
207
}
203
208
209
+ /// Appends the given value's bytes repeatedly to this span's bytes.
204
210
@_alwaysEmitIntoClient
205
211
@lifetime ( self : copy self )
206
212
public mutating func append< T: BitwiseCopyable > (
@@ -213,77 +219,11 @@ extension OutputRawSpan {
213
219
)
214
220
_count &+= total
215
221
}
216
-
217
- /// Returns true if the iterator has filled all the free capacity in the span.
218
- @_alwaysEmitIntoClient
219
- @lifetime ( self : copy self )
220
- @discardableResult
221
- public mutating func append< T: BitwiseCopyable > (
222
- from elements: inout some IteratorProtocol < T >
223
- ) -> Bool {
224
- // FIXME: It may be best to delay this API until
225
- // we have designed a chunking IteratorProtocol
226
- var p = unsafe _tail( )
227
- while ( _count + MemoryLayout < T > . stride) <= capacity {
228
- guard let element = elements. next ( ) else { return false }
229
- unsafe p. initializeMemory ( as: T . self, to: element)
230
- unsafe p = p. advanced ( by: MemoryLayout< T> . stride)
231
- _count &+= MemoryLayout< T> . stride
232
- }
233
- return freeCapacity == 0
234
- }
235
-
236
- @_alwaysEmitIntoClient
237
- @lifetime ( self : copy self )
238
- public mutating func append< T: BitwiseCopyable > (
239
- contentsOf source: some Sequence < T >
240
- ) {
241
- let done : Void ? = source. withContiguousStorageIfAvailable {
242
- unsafe append( contentsOf: UnsafeRawBufferPointer ( $0) )
243
- }
244
- if done != nil {
245
- return
246
- }
247
-
248
- let freeCapacity = freeCapacity
249
- var ( iterator, copied) = unsafe _tail( ) . withMemoryRebound (
250
- to: T . self, capacity: freeCapacity
251
- ) {
252
- let suffix = unsafe UnsafeMutableBufferPointer(
253
- start: $0, count: freeCapacity
254
- )
255
- return unsafe source. _copyContents ( initializing: suffix)
256
- }
257
- _precondition ( iterator. next ( ) == nil , " OutputRawSpan capacity overflow " )
258
- let total = copied * MemoryLayout< T> . stride
259
- _precondition ( _count + total <= capacity, " Invalid Sequence._copyContents " )
260
- _count &+= total
261
- }
262
-
263
- @_alwaysEmitIntoClient
264
- @lifetime ( self : copy self )
265
- public mutating func append(
266
- contentsOf source: UnsafeRawBufferPointer
267
- ) {
268
- guard unsafe !source. isEmpty else { return }
269
- let addedBytes = source. count
270
- _precondition ( addedBytes <= freeCapacity, " OutputRawSpan capacity overflow " )
271
- unsafe _tail( ) . copyMemory ( from: source. baseAddress!, byteCount: addedBytes)
272
- _count += addedBytes
273
- }
274
-
275
- @_alwaysEmitIntoClient
276
- @lifetime ( self : copy self )
277
- public mutating func append(
278
- contentsOf source: RawSpan
279
- ) {
280
- unsafe source. withUnsafeBytes { unsafe append( contentsOf: $0) }
281
- }
282
222
}
283
223
284
224
@available ( SwiftStdlib 6 . 2 , * )
285
225
extension OutputRawSpan {
286
-
226
+ /// Borrow the underlying initialized memory for read-only access.
287
227
@_alwaysEmitIntoClient
288
228
public var bytes : RawSpan {
289
229
@lifetime ( borrow self)
@@ -294,6 +234,7 @@ extension OutputRawSpan {
294
234
}
295
235
}
296
236
237
+ /// Exclusively borrow the underlying initialized memory for mutation.
297
238
@_alwaysEmitIntoClient
298
239
public var mutableBytes : MutableRawSpan {
299
240
@lifetime ( & self )
0 commit comments