Skip to content

Commit 9e98783

Browse files
committed
[stdlib] make OutputRawSpan changes from SE-0485
1 parent 5f5e6b6 commit 9e98783

File tree

1 file changed

+26
-85
lines changed

1 file changed

+26
-85
lines changed

stdlib/public/core/Span/OutputRawSpan.swift

Lines changed: 26 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,6 @@ public struct OutputRawSpan: ~Copyable, ~Escapable {
3232
capacity = 0
3333
_count = 0
3434
}
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-
}
4835
}
4936

5037
@available(SwiftStdlib 6.2, *)
@@ -53,6 +40,8 @@ extension OutputRawSpan: @unchecked Sendable {}
5340
@available(SwiftStdlib 6.2, *)
5441
extension OutputRawSpan {
5542
@_alwaysEmitIntoClient
43+
@_transparent
44+
@unsafe
5645
internal func _start() -> UnsafeMutableRawPointer {
5746
unsafe _pointer._unsafelyUnwrappedUnchecked
5847
}
@@ -64,16 +53,23 @@ extension OutputRawSpan {
6453
// NOTE: `_pointer` must be known to be not-nil.
6554
unsafe _start().advanced(by: _count)
6655
}
56+
}
6757

58+
@available(SwiftStdlib 6.2, *)
59+
extension OutputRawSpan {
60+
/// The number of initialized bytes in this span.
6861
@_alwaysEmitIntoClient
69-
public var freeCapacity: Int { capacity &- _count }
62+
public var byteCount: Int { _count }
7063

64+
/// The number of additional bytes that can be appended to this span.
7165
@_alwaysEmitIntoClient
72-
public var byteCount: Int { _count }
66+
public var freeCapacity: Int { capacity &- _count }
7367

68+
/// A Boolean value indicating whether the span is empty.
7469
@_alwaysEmitIntoClient
7570
public var isEmpty: Bool { _count == 0 }
7671

72+
/// A Boolean value indicating whether the span is full.
7773
@_alwaysEmitIntoClient
7874
public var isFull: Bool { _count == capacity }
7975
}
@@ -102,7 +98,7 @@ extension OutputRawSpan {
10298
///
10399
/// - Parameters:
104100
/// - buffer: an `UnsafeMutableBufferPointer` to be initialized
105-
/// - initializedCount: the number of initialized elements
101+
/// - initializedCount: the number of initialized bytes
106102
/// at the beginning of `buffer`.
107103
@unsafe
108104
@_alwaysEmitIntoClient
@@ -135,8 +131,9 @@ extension OutputRawSpan {
135131
///
136132
/// - Parameters:
137133
/// - buffer: an `UnsafeMutableBufferPointer` to be initialized
138-
/// - initializedCount: the number of initialized elements
134+
/// - initializedCount: the number of initialized bytes
139135
/// at the beginning of `buffer`.
136+
@unsafe
140137
@_alwaysEmitIntoClient
141138
@lifetime(borrow buffer)
142139
public init(
@@ -154,6 +151,7 @@ extension OutputRawSpan {
154151
@available(SwiftStdlib 6.2, *)
155152
extension OutputRawSpan {
156153

154+
/// Append a single byte to this span.
157155
@_alwaysEmitIntoClient
158156
@lifetime(self: copy self)
159157
public mutating func append(_ value: UInt8) {
@@ -162,6 +160,7 @@ extension OutputRawSpan {
162160
_count &+= 1
163161
}
164162

163+
/// Remove the last byte from this span.
165164
@_alwaysEmitIntoClient
166165
@discardableResult
167166
@lifetime(self: copy self)
@@ -171,6 +170,10 @@ extension OutputRawSpan {
171170
return unsafe _tail().load(as: UInt8.self)
172171
}
173172

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`
174177
@_alwaysEmitIntoClient
175178
@lifetime(self: copy self)
176179
public mutating func removeLast(_ k: Int) {
@@ -179,6 +182,8 @@ extension OutputRawSpan {
179182
_count &-= k
180183
}
181184

185+
/// Remove all this span's elements and return its memory
186+
/// to the uninitialized state.
182187
@_alwaysEmitIntoClient
183188
@lifetime(self: copy self)
184189
public mutating func removeAll() {
@@ -190,7 +195,7 @@ extension OutputRawSpan {
190195
//MARK: bulk-append functions
191196
@available(SwiftStdlib 6.2, *)
192197
extension OutputRawSpan {
193-
198+
/// Appends the given value's bytes to this span's bytes.
194199
@_alwaysEmitIntoClient
195200
@lifetime(self: copy self)
196201
public mutating func append<T: BitwiseCopyable>(_ value: T, as type: T.Type) {
@@ -201,6 +206,7 @@ extension OutputRawSpan {
201206
_count &+= MemoryLayout<T>.size
202207
}
203208

209+
/// Appends the given value's bytes repeatedly to this span's bytes.
204210
@_alwaysEmitIntoClient
205211
@lifetime(self: copy self)
206212
public mutating func append<T: BitwiseCopyable>(
@@ -213,77 +219,11 @@ extension OutputRawSpan {
213219
)
214220
_count &+= total
215221
}
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-
}
282222
}
283223

284224
@available(SwiftStdlib 6.2, *)
285225
extension OutputRawSpan {
286-
226+
/// Borrow the underlying initialized memory for read-only access.
287227
@_alwaysEmitIntoClient
288228
public var bytes: RawSpan {
289229
@lifetime(borrow self)
@@ -294,6 +234,7 @@ extension OutputRawSpan {
294234
}
295235
}
296236

237+
/// Exclusively borrow the underlying initialized memory for mutation.
297238
@_alwaysEmitIntoClient
298239
public var mutableBytes: MutableRawSpan {
299240
@lifetime(&self)

0 commit comments

Comments
 (0)