@@ -34,13 +34,15 @@ public struct Unsafe${Mutable}BufferPointer<Element: ~Copyable>: Copyable {
34
34
35
35
@usableFromInline
36
36
@_preInverseGenerics
37
+ @safe
37
38
let _position: Unsafe ${ Mutable} Poin ter< Element>?
38
39
39
40
/// The number of elements in the buffer.
40
41
///
41
42
/// If the `baseAddress` of this buffer is `nil`, the count is zero. However,
42
43
/// a buffer can have a `count` of zero even with a non-`nil` base address.
43
44
@_preInverseGenerics
45
+ @safe
44
46
public let count : Int
45
47
46
48
// This works around _debugPrecondition() impacting the performance of
@@ -81,6 +83,7 @@ public struct Unsafe${Mutable}BufferPointer<Element: ~Copyable>: Copyable {
81
83
82
84
@inlinable // unsafe-performance
83
85
@_preInverseGenerics
86
+ @safe
84
87
public init( _empty: ( ) ) {
85
88
_position = nil
86
89
count = 0
@@ -105,6 +108,7 @@ public struct Unsafe${Mutable}BufferPointer<Element: ~Copyable>: Copyable {
105
108
/// - Parameter other: The mutable buffer pointer to convert.
106
109
@inlinable // unsafe-performance
107
110
@_preInverseGenerics
111
+ @safe
108
112
public init( _ other: UnsafeMutableBufferPointer < Element > ) {
109
113
_position = UnsafePointer < Element > ( other. _position)
110
114
count = other. count
@@ -139,6 +143,7 @@ extension UnsafeBufferPointer {
139
143
@frozen // unsafe-performance
140
144
public struct Iterator {
141
145
@usableFromInline
146
+ @unsafe
142
147
internal var _position, _end: UnsafePointer < Element > ?
143
148
144
149
@inlinable // unsafe-performance
@@ -147,31 +152,32 @@ extension UnsafeBufferPointer {
147
152
_position: UnsafePointer < Element > ? ,
148
153
_end: UnsafePointer < Element > ?
149
154
) {
150
- self . _position = _position
151
- self . _end = _end
155
+ unsafe self. _position = _position
156
+ unsafe self. _end = _end
152
157
}
153
158
}
154
159
}
155
160
156
161
@available( * , unavailable)
157
162
extension UnsafeBufferPointer. Iterator : Sendable { }
158
163
159
- extension UnsafeBufferPointer. Iterator : IteratorProtocol {
164
+ extension UnsafeBufferPointer. Iterator : @unsafe IteratorProtocol {
160
165
/// Advances to the next element and returns it, or `nil` if no next element
161
166
/// exists.
162
167
///
163
168
/// Once `nil` has been returned, all subsequent calls return `nil`.
164
169
@inlinable // unsafe-performance
170
+ @unsafe
165
171
public mutating func next( ) -> Element ? {
166
- guard let start = _position else {
172
+ guard let start = unsafe _position else {
167
173
return nil
168
174
}
169
- _internalInvariant ( _end != nil , " inconsistent _position, _end pointers " )
175
+ _internalInvariant ( unsafe _end != nil , " inconsistent _position, _end pointers " )
170
176
171
- if start == _end. _unsafelyUnwrappedUnchecked { return nil }
177
+ if unsafe start == _end. _unsafelyUnwrappedUnchecked { return nil }
172
178
173
179
let result = unsafe start. pointee
174
- _position = start + 1
180
+ unsafe _position = start + 1
175
181
return result
176
182
}
177
183
}
@@ -213,6 +219,7 @@ extension Unsafe${Mutable}BufferPointer: @unsafe Sequence {
213
219
}
214
220
215
221
@inlinable
222
+ @safe
216
223
public func withContiguousStorageIfAvailable< R> (
217
224
_ body: ( UnsafeBufferPointer < Element > ) throws -> R
218
225
) rethrows -> R ? {
@@ -232,6 +239,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
232
239
/// - Complexity: O(1)
233
240
@_alwaysEmitIntoClient
234
241
@_preInverseGenerics
242
+ @safe
235
243
public var isEmpty : Bool { count == 0 }
236
244
237
245
/// The index of the first element in a nonempty buffer.
@@ -240,6 +248,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
240
248
/// is always zero.
241
249
@inlinable
242
250
@_preInverseGenerics
251
+ @safe
243
252
public var startIndex : Int { 0 }
244
253
245
254
/// The "past the end" position---that is, the position one greater than the
@@ -249,10 +258,12 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
249
258
/// always identical to `count`.
250
259
@inlinable
251
260
@_preInverseGenerics
261
+ @safe
252
262
public var endIndex : Int { count }
253
263
254
264
@inlinable
255
265
@_preInverseGenerics
266
+ @unsafe
256
267
public func index( after i: Int ) -> Int {
257
268
// NOTE: this is a manual specialization of index movement for a Strideable
258
269
// index that is required for UnsafeBufferPointer performance. The
@@ -271,6 +282,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
271
282
272
283
@inlinable
273
284
@_preInverseGenerics
285
+ @unsafe
274
286
public func formIndex( after i: inout Int ) {
275
287
// NOTE: this is a manual specialization of index movement for a Strideable
276
288
// index that is required for UnsafeBufferPointer performance. The
@@ -285,6 +297,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
285
297
286
298
@inlinable
287
299
@_preInverseGenerics
300
+ @unsafe
288
301
public func index( before i: Int ) -> Int {
289
302
// NOTE: this is a manual specialization of index movement for a Strideable
290
303
// index that is required for UnsafeBufferPointer performance. The
@@ -299,6 +312,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
299
312
300
313
@inlinable
301
314
@_preInverseGenerics
315
+ @unsafe
302
316
public func formIndex( before i: inout Int ) {
303
317
// NOTE: this is a manual specialization of index movement for a Strideable
304
318
// index that is required for UnsafeBufferPointer performance. The
@@ -313,6 +327,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
313
327
314
328
@inlinable
315
329
@_preInverseGenerics
330
+ @unsafe
316
331
public func index( _ i: Int , offsetBy n: Int ) -> Int {
317
332
// NOTE: this is a manual specialization of index movement for a Strideable
318
333
// index that is required for UnsafeBufferPointer performance. The
@@ -327,6 +342,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
327
342
328
343
@inlinable
329
344
@_preInverseGenerics
345
+ @unsafe
330
346
public func index( _ i: Int , offsetBy n: Int , limitedBy limit: Int ) -> Int ? {
331
347
// NOTE: this is a manual specialization of index movement for a Strideable
332
348
// index that is required for UnsafeBufferPointer performance. The
@@ -349,6 +365,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
349
365
350
366
@inlinable
351
367
@_preInverseGenerics
368
+ @safe
352
369
public func distance( from start: Int , to end: Int ) -> Int {
353
370
// NOTE: this is a manual specialization of index movement for a Strideable
354
371
// index that is required for UnsafeBufferPointer performance. The
@@ -500,6 +517,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
500
517
/// - Parameter bounds: A valid range of indices within this buffer.
501
518
/// - Returns: A new buffer pointer over the items at `bounds`.
502
519
@_alwaysEmitIntoClient
520
+ @safe
503
521
public func extracting( _ bounds: Range < Int > ) -> Self {
504
522
_precondition ( bounds. lowerBound >= 0 && bounds. upperBound <= count,
505
523
" Index out of range " )
@@ -538,6 +556,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
538
556
/// - Parameter bounds: A valid range of indices within this buffer.
539
557
/// - Returns: A new buffer pointer over the items at `bounds`.
540
558
@_alwaysEmitIntoClient
559
+ @safe
541
560
public func extracting( _ bounds: some RangeExpression < Int > ) -> Self {
542
561
extracting ( bounds. relative ( to: unsafe Range( uncheckedBounds: ( 0 , count) ) ) )
543
562
}
@@ -557,6 +576,7 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
557
576
//
558
577
/// - Returns: The same buffer as `self`.
559
578
@_alwaysEmitIntoClient
579
+ @safe
560
580
public func extracting( _ bounds: UnboundedRange ) -> Self {
561
581
self
562
582
}
@@ -627,6 +647,7 @@ extension Unsafe${Mutable}BufferPointer {
627
647
628
648
@_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 1 )
629
649
@usableFromInline
650
+ @unsafe
630
651
internal subscript( _unchecked i: Int) -> Element {
631
652
get {
632
653
_internalInvariant ( i >= 0 )
@@ -655,20 +676,23 @@ extension Unsafe${Mutable}BufferPointer:
655
676
public typealias SubSequence = Slice < Unsafe${ Mutable} BufferPointer< Element>>
656
677
657
678
@inlinable // unsafe-performance
679
+ @safe
658
680
public func _failEarlyRangeCheck( _ index: Int, bounds: Range < Int > ) {
659
681
// NOTE: In release mode, this method is a no-op for performance reasons.
660
682
_debugPrecondition ( index >= bounds. lowerBound)
661
683
_debugPrecondition ( index < bounds. upperBound)
662
684
}
663
685
664
686
@inlinable // unsafe-performance
687
+ @safe
665
688
public func _failEarlyRangeCheck( _ range: Range < Int > , bounds: Range < Int > ) {
666
689
// NOTE: In release mode, this method is a no-op for performance reasons.
667
690
_debugPrecondition ( range. lowerBound >= bounds. lowerBound)
668
691
_debugPrecondition ( range. upperBound <= bounds. upperBound)
669
692
}
670
693
671
694
@inlinable // unsafe-performance
695
+ @safe
672
696
public var indices: Indices {
673
697
// Not checked because init forbids negative count.
674
698
return unsafe Indices( uncheckedBounds: ( startIndex, endIndex) )
@@ -741,14 +765,15 @@ extension Unsafe${Mutable}BufferPointer:
741
765
% if Mutable :
742
766
@inlinable
743
767
@available( * , deprecated, renamed: " withContiguousMutableStorageIfAvailable " )
768
+ @safe
744
769
public mutating func _withUnsafeMutableBufferPointerIfSupported< R> (
745
770
_ body: ( inout UnsafeMutableBufferPointer< Element > ) throws -> R
746
771
) rethrows -> R? {
747
772
return try body ( & self )
748
773
}
749
774
750
775
@inlinable
751
- @unsafe
776
+ @safe
752
777
public mutating func withContiguousMutableStorageIfAvailable< R> (
753
778
_ body: ( inout UnsafeMutableBufferPointer< Element > ) throws -> R
754
779
) rethrows -> R? {
@@ -875,6 +900,7 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
875
900
/// of `Element`.
876
901
@inlinable
877
902
@_preInverseGenerics
903
+ @safe
878
904
public static func allocate(
879
905
capacity count: Int
880
906
) -> UnsafeMutableBufferPointer < Element > {
@@ -1038,7 +1064,7 @@ extension UnsafeMutableBufferPointer {
1038
1064
while index < endIndex {
1039
1065
guard let element = iterator. next ( ) else { break }
1040
1066
unsafe _position. _unsafelyUnwrappedUnchecked [ index] = element
1041
- formIndex ( after: & index)
1067
+ unsafe formIndex ( after: & index)
1042
1068
}
1043
1069
return ( iterator, index)
1044
1070
}
@@ -1102,7 +1128,7 @@ extension UnsafeMutableBufferPointer {
1102
1128
break
1103
1129
}
1104
1130
unsafe _position. _unsafelyUnwrappedUnchecked [ index] = value
1105
- formIndex ( after: & index)
1131
+ unsafe formIndex ( after: & index)
1106
1132
}
1107
1133
return index
1108
1134
}
@@ -1453,6 +1479,7 @@ extension Unsafe${Mutable}BufferPointer: CustomDebugStringConvertible
1453
1479
where Element: ~ Copyable {
1454
1480
/// A textual representation of the buffer, suitable for debugging.
1455
1481
@_preInverseGenerics
1482
+ @safe
1456
1483
public var debugDescription: String {
1457
1484
return " Unsafe${Mutable}BufferPointer "
1458
1485
+ " (start: \( _position. map ( String . init ( describing: ) ) ?? " nil " ) , count: \( count) ) "
0 commit comments