12
12
13
13
// A MutableRawSpan represents a span of memory which
14
14
// contains initialized `Element` instances.
15
+ @safe
15
16
@frozen
16
17
@available ( SwiftStdlib 6 . 2 , * )
17
18
public struct MutableRawSpan: ~ Copyable & ~ Escapable {
@@ -23,7 +24,7 @@ public struct MutableRawSpan: ~Copyable & ~Escapable {
23
24
24
25
@_alwaysEmitIntoClient
25
26
internal func _start( ) -> UnsafeMutableRawPointer {
26
- _pointer. unsafelyUnwrapped
27
+ unsafe _pointer. unsafelyUnwrapped
27
28
}
28
29
29
30
@_alwaysEmitIntoClient
@@ -32,7 +33,7 @@ public struct MutableRawSpan: ~Copyable & ~Escapable {
32
33
_unchecked pointer: UnsafeMutableRawPointer ? ,
33
34
byteCount: Int
34
35
) {
35
- _pointer = pointer
36
+ _pointer = unsafe pointer
36
37
_count = byteCount
37
38
}
38
39
}
@@ -50,17 +51,17 @@ extension MutableRawSpan {
50
51
) {
51
52
let baseAddress = bytes. baseAddress
52
53
let span = MutableRawSpan ( _unchecked: baseAddress, byteCount: bytes. count)
53
- self = _overrideLifetime ( span, borrowing: bytes)
54
+ self = unsafe _override Lifetime ( span, borrowing: bytes)
54
55
}
55
56
56
57
@_alwaysEmitIntoClient
57
58
@lifetime ( borrow bytes)
58
59
public init (
59
60
_unsafeBytes bytes: borrowing Slice < UnsafeMutableRawBufferPointer >
60
61
) {
61
- let rebased = UnsafeMutableRawBufferPointer ( rebasing: bytes)
62
+ let rebased = unsafe UnsafeMutableRawBufferPointer( rebasing: bytes)
62
63
let span = MutableRawSpan ( _unsafeBytes: rebased)
63
- self = _overrideLifetime ( span, borrowing: bytes)
64
+ self = unsafe _override Lifetime ( span, borrowing: bytes)
64
65
}
65
66
66
67
@_alwaysEmitIntoClient
@@ -80,30 +81,30 @@ extension MutableRawSpan {
80
81
) {
81
82
let bytes = UnsafeMutableRawBufferPointer ( elements)
82
83
let span = MutableRawSpan ( _unsafeBytes: bytes)
83
- self = _overrideLifetime ( span, borrowing: elements)
84
+ self = unsafe _override Lifetime ( span, borrowing: elements)
84
85
}
85
86
86
87
@_alwaysEmitIntoClient
87
88
@lifetime ( borrow elements)
88
89
public init < Element: BitwiseCopyable > (
89
90
_unsafeElements elements: borrowing Slice < UnsafeMutableBufferPointer < Element > >
90
91
) {
91
- let rebased = UnsafeMutableBufferPointer ( rebasing: elements)
92
+ let rebased = unsafe UnsafeMutableBufferPointer( rebasing: elements)
92
93
let span = MutableRawSpan ( _unsafeElements: rebased)
93
- self = _overrideLifetime ( span, borrowing: elements)
94
+ self = unsafe _override Lifetime ( span, borrowing: elements)
94
95
}
95
96
96
97
@_alwaysEmitIntoClient
97
98
@lifetime ( elements)
98
99
public init < Element: BitwiseCopyable > (
99
100
_elements elements: consuming MutableSpan < Element >
100
101
) {
101
- let bytes = UnsafeMutableRawBufferPointer (
102
+ let bytes = unsafe UnsafeMutableRawBufferPointer(
102
103
start: elements. _pointer,
103
104
count: elements. count &* MemoryLayout< Element> . stride
104
105
)
105
106
let span = MutableRawSpan ( _unsafeBytes: bytes)
106
- self = _overrideLifetime ( span, copying: elements)
107
+ self = unsafe _override Lifetime ( span, copying: elements)
107
108
}
108
109
}
109
110
@@ -129,19 +130,19 @@ extension MutableRawSpan {
129
130
_ body: ( _ buffer: UnsafeRawBufferPointer ) throws ( E ) -> Result
130
131
) throws ( E ) -> Result {
131
132
guard let pointer = _pointer, _count > 0 else {
132
- return try body ( . init( start: nil , count: 0 ) )
133
+ return try unsafe body( . init( start: nil , count: 0 ) )
133
134
}
134
- return try body ( . init( start: pointer, count: _count) )
135
+ return try unsafe body( . init( start: pointer, count: _count) )
135
136
}
136
137
137
138
@_alwaysEmitIntoClient
138
139
public mutating func withUnsafeMutableBytes< E: Error , Result: ~ Copyable> (
139
140
_ body: ( UnsafeMutableRawBufferPointer ) throws ( E ) -> Result
140
141
) throws ( E ) -> Result {
141
142
guard let pointer = _pointer, _count > 0 else {
142
- return try body ( . init( start: nil , count: 0 ) )
143
+ return try unsafe body( . init( start: nil , count: 0 ) )
143
144
}
144
- return try body ( . init( start: pointer, count: _count) )
145
+ return try unsafe body( . init( start: pointer, count: _count) )
145
146
}
146
147
}
147
148
@@ -153,7 +154,7 @@ extension RawSpan {
153
154
public init ( _unsafeMutableRawSpan mutableSpan: borrowing MutableRawSpan ) {
154
155
let start = mutableSpan. _start ( )
155
156
let span = RawSpan ( _unsafeStart: start, byteCount: mutableSpan. byteCount)
156
- self = _overrideLifetime ( span, borrowing: mutableSpan)
157
+ self = unsafe _override Lifetime ( span, borrowing: mutableSpan)
157
158
}
158
159
}
159
160
@@ -174,9 +175,9 @@ extension MutableRawSpan {
174
175
public borrowing func _unsafeView< T: BitwiseCopyable > (
175
176
as type: T . Type
176
177
) -> Span < T > {
177
- let bytes = UnsafeRawBufferPointer ( start: _pointer, count: _count)
178
+ let bytes = unsafe UnsafeRawBufferPointer( start: _pointer, count: _count)
178
179
let span = Span < T > ( _unsafeBytes: bytes)
179
- return _overrideLifetime ( span, borrowing: self )
180
+ return unsafe _override Lifetime ( span, borrowing: self )
180
181
}
181
182
182
183
@unsafe
@@ -185,9 +186,11 @@ extension MutableRawSpan {
185
186
public mutating func _unsafeMutableView< T: BitwiseCopyable > (
186
187
as type: T . Type
187
188
) -> MutableSpan < T > {
188
- let bytes = UnsafeMutableRawBufferPointer ( start: _pointer, count: _count)
189
+ let bytes = unsafe UnsafeMutableRawBufferPointer(
190
+ start: _pointer, count: _count
191
+ )
189
192
let span = MutableSpan < T > ( _unsafeBytes: bytes)
190
- return _overrideLifetime ( span, mutating: & self )
193
+ return unsafe _override Lifetime ( span, mutating: & self )
191
194
}
192
195
}
193
196
@@ -221,7 +224,7 @@ extension MutableRawSpan {
221
224
MemoryLayout < T > . size <= ( _count &- offset) ,
222
225
" Byte offset range out of bounds "
223
226
)
224
- return unsafeLoad ( fromUncheckedByteOffset: offset, as: T . self)
227
+ return unsafe unsafeLoad( fromUncheckedByteOffset: offset, as: T . self)
225
228
}
226
229
227
230
/// Returns a new instance of the given type, constructed from the raw memory
@@ -247,7 +250,7 @@ extension MutableRawSpan {
247
250
public func unsafeLoad< T> (
248
251
fromUncheckedByteOffset offset: Int , as: T . Type
249
252
) -> T {
250
- _start ( ) . load ( fromByteOffset: offset, as: T . self)
253
+ unsafe _start( ) . load ( fromByteOffset: offset, as: T . self)
251
254
}
252
255
253
256
/// Returns a new instance of the given type, constructed from the raw memory
@@ -276,7 +279,7 @@ extension MutableRawSpan {
276
279
MemoryLayout < T > . size <= ( _count &- offset) ,
277
280
" Byte offset range out of bounds "
278
281
)
279
- return unsafeLoadUnaligned ( fromUncheckedByteOffset: offset, as: T . self)
282
+ return unsafe unsafeLoadUnaligned( fromUncheckedByteOffset: offset, as: T . self)
280
283
}
281
284
282
285
/// Returns a new instance of the given type, constructed from the raw memory
@@ -301,7 +304,7 @@ extension MutableRawSpan {
301
304
public func unsafeLoadUnaligned< T: BitwiseCopyable > (
302
305
fromUncheckedByteOffset offset: Int , as: T . Type
303
306
) -> T {
304
- _start ( ) . loadUnaligned ( fromByteOffset: offset, as: T . self)
307
+ unsafe _start( ) . loadUnaligned ( fromByteOffset: offset, as: T . self)
305
308
}
306
309
307
310
@_alwaysEmitIntoClient
@@ -313,15 +316,15 @@ extension MutableRawSpan {
313
316
MemoryLayout < T > . size <= ( _count &- offset) ,
314
317
" Byte offset range out of bounds "
315
318
)
316
- storeBytes ( of: value, toUncheckedByteOffset: offset, as: type)
319
+ unsafe storeBytes( of: value, toUncheckedByteOffset: offset, as: type)
317
320
}
318
321
319
322
@unsafe
320
323
@_alwaysEmitIntoClient
321
324
public func storeBytes< T: BitwiseCopyable > (
322
325
of value: T , toUncheckedByteOffset offset: Int , as type: T . Type
323
326
) {
324
- _start ( ) . storeBytes ( of: value, toByteOffset: offset, as: type)
327
+ unsafe _start( ) . storeBytes ( of: value, toByteOffset: offset, as: type)
325
328
}
326
329
}
327
330
@@ -347,7 +350,9 @@ extension MutableRawSpan {
347
350
var offset = byteOffset
348
351
while offset + MemoryLayout < Element > . stride <= _count {
349
352
guard let element = elements. next ( ) else { break }
350
- storeBytes ( of: element, toUncheckedByteOffset: offset, as: Element . self)
353
+ unsafe storeBytes(
354
+ of: element, toUncheckedByteOffset: offset, as: Element . self
355
+ )
351
356
offset &+= MemoryLayout< Element> . stride
352
357
}
353
358
return offset
@@ -403,8 +408,8 @@ extension MutableRawSpan {
403
408
) -> Int {
404
409
if source. byteCount == 0 { return byteOffset }
405
410
source. withUnsafeBytes {
406
- _start ( ) . advanced ( by: byteOffset)
407
- . copyMemory ( from: $0. baseAddress!, byteCount: $0. count)
411
+ unsafe _start( ) . advanced ( by: byteOffset)
412
+ . copyMemory ( from: $0. baseAddress!, byteCount: $0. count)
408
413
}
409
414
return byteOffset &+ source. byteCount
410
415
}
@@ -443,7 +448,7 @@ extension MutableRawSpan {
443
448
UInt ( bitPattern: bounds. upperBound) <= UInt ( bitPattern: _count) ,
444
449
" Index range out of bounds "
445
450
)
446
- return _extracting ( unchecked: bounds)
451
+ return unsafe _extracting( unchecked: bounds)
447
452
}
448
453
449
454
/// Constructs a new span over the items within the supplied range of
@@ -465,9 +470,9 @@ extension MutableRawSpan {
465
470
@_alwaysEmitIntoClient
466
471
@lifetime ( borrow self)
467
472
mutating public func _extracting( unchecked bounds: Range < Int > ) -> Self {
468
- let newStart = _pointer? . advanced ( by: bounds. lowerBound)
473
+ let newStart = unsafe _pointer? . advanced ( by: bounds. lowerBound)
469
474
let newSpan = Self ( _unchecked: newStart, byteCount: bounds. count)
470
- return _overrideLifetime ( newSpan, mutating: & self )
475
+ return unsafe _override Lifetime ( newSpan, mutating: & self )
471
476
}
472
477
473
478
/// Constructs a new span over the items within the supplied range of
@@ -488,7 +493,7 @@ extension MutableRawSpan {
488
493
mutating public func _extracting(
489
494
_ bounds: some RangeExpression < Int >
490
495
) -> Self {
491
- _extracting ( bounds. relative ( to: byteOffsets) . clamped ( to : byteOffsets ) )
496
+ _extracting ( bounds. relative ( to: byteOffsets) )
492
497
}
493
498
494
499
/// Constructs a new span over the items within the supplied range of
@@ -512,11 +517,10 @@ extension MutableRawSpan {
512
517
mutating public func _extracting(
513
518
unchecked bounds: some RangeExpression < Int >
514
519
) -> Self {
515
- _extracting (
516
- unchecked: bounds. relative ( to: byteOffsets) . clamped ( to: byteOffsets)
517
- )
520
+ unsafe _extracting( unchecked: bounds. relative ( to: byteOffsets) )
518
521
}
519
522
523
+ @unsafe
520
524
@_alwaysEmitIntoClient
521
525
@lifetime ( borrow self)
522
526
mutating public func _extracting(
@@ -525,7 +529,7 @@ extension MutableRawSpan {
525
529
let range = Range (
526
530
_uncheckedBounds: ( bounds. lowerBound, bounds. upperBound&+ 1 )
527
531
)
528
- return _extracting ( unchecked: range)
532
+ return unsafe _extracting( unchecked: range)
529
533
}
530
534
531
535
/// Constructs a new span over all the items of this span.
@@ -541,7 +545,7 @@ extension MutableRawSpan {
541
545
@lifetime ( borrow self)
542
546
mutating public func _extracting( _: UnboundedRange ) -> Self {
543
547
let newSpan = Self ( _unchecked: _start ( ) , byteCount: _count)
544
- return _overrideLifetime ( newSpan, mutating: & self )
548
+ return unsafe _override Lifetime ( newSpan, mutating: & self )
545
549
}
546
550
}
547
551
@@ -570,7 +574,7 @@ extension MutableRawSpan {
570
574
_precondition ( maxLength >= 0 , " Can't have a prefix of negative length " )
571
575
let newCount = min ( maxLength, byteCount)
572
576
let newSpan = Self ( _unchecked: _pointer, byteCount: newCount)
573
- return _overrideLifetime ( newSpan, mutating: & self )
577
+ return unsafe _override Lifetime ( newSpan, mutating: & self )
574
578
}
575
579
576
580
/// Returns a span over all but the given number of trailing elements.
@@ -593,7 +597,7 @@ extension MutableRawSpan {
593
597
_precondition ( k >= 0 , " Can't drop a negative number of elements " )
594
598
let dropped = min ( k, byteCount)
595
599
let newSpan = Self ( _unchecked: _pointer, byteCount: byteCount &- dropped)
596
- return _overrideLifetime ( newSpan, mutating: & self )
600
+ return unsafe _override Lifetime ( newSpan, mutating: & self )
597
601
}
598
602
599
603
/// Returns a span containing the final elements of the span,
@@ -616,9 +620,9 @@ extension MutableRawSpan {
616
620
mutating public func _extracting( last maxLength: Int ) -> Self {
617
621
_precondition ( maxLength >= 0 , " Can't have a suffix of negative length " )
618
622
let newCount = min ( maxLength, byteCount)
619
- let newStart = _pointer? . advanced ( by: byteCount &- newCount)
623
+ let newStart = unsafe _pointer? . advanced ( by: byteCount &- newCount)
620
624
let newSpan = Self ( _unchecked: newStart, byteCount: newCount)
621
- return _overrideLifetime ( newSpan, copying: self )
625
+ return unsafe _override Lifetime ( newSpan, copying: self )
622
626
}
623
627
624
628
/// Returns a span over all but the given number of initial elements.
@@ -640,8 +644,8 @@ extension MutableRawSpan {
640
644
mutating public func _extracting( droppingFirst k: Int ) -> Self {
641
645
_precondition ( k >= 0 , " Can't drop a negative number of bytes " )
642
646
let dropped = min ( k, byteCount)
643
- let newStart = _pointer? . advanced ( by: dropped)
647
+ let newStart = unsafe _pointer? . advanced ( by: dropped)
644
648
let newSpan = Self ( _unchecked: newStart, byteCount: byteCount &- dropped)
645
- return _overrideLifetime ( newSpan, mutating: & self )
649
+ return unsafe _override Lifetime ( newSpan, mutating: & self )
646
650
}
647
651
}
0 commit comments