@@ -84,6 +84,34 @@ extension InlineArray where Element: ~Copyable {
84
84
unsafe UnsafeBufferPointer< Element > ( start: _address, count: count)
85
85
}
86
86
87
+ /// Returns a pointer to the first element in the array while performing stack
88
+ /// checking.
89
+ ///
90
+ /// Use this when the value of the pointer could potentially be directly used
91
+ /// by users (e.g. through the use of span or the unchecked subscript).
92
+ @available ( SwiftStdlib 6 . 2 , * )
93
+ @_alwaysEmitIntoClient
94
+ @_transparent
95
+ internal var _protectedAddress : UnsafePointer < Element > {
96
+ #if $AddressOfProperty
97
+ unsafe UnsafePointer< Element > ( Builtin . addressOfBorrow ( _storage) )
98
+ #else
99
+ unsafe UnsafePointer< Element > ( Builtin . addressOfBorrow ( self ) )
100
+ #endif
101
+ }
102
+
103
+ /// Returns a buffer pointer over the entire array while performing stack
104
+ /// checking.
105
+ ///
106
+ /// Use this when the value of the pointer could potentially be directly used
107
+ /// by users (e.g. through the use of span or the unchecked subscript).
108
+ @available ( SwiftStdlib 6 . 2 , * )
109
+ @_alwaysEmitIntoClient
110
+ @_transparent
111
+ internal var _protectedBuffer : UnsafeBufferPointer < Element > {
112
+ unsafe UnsafeBufferPointer< Element > ( start: _protectedAddress, count: count)
113
+ }
114
+
87
115
/// Returns a mutable pointer to the first element in the array.
88
116
@available ( SwiftStdlib 6 . 2 , * )
89
117
@_alwaysEmitIntoClient
@@ -111,6 +139,41 @@ extension InlineArray where Element: ~Copyable {
111
139
}
112
140
}
113
141
142
+ /// Returns a mutable pointer to the first element in the array while
143
+ /// performing stack checking.
144
+ ///
145
+ /// Use this when the value of the pointer could potentially be directly used
146
+ /// by users (e.g. through the use of span or the unchecked subscript).
147
+ @available ( SwiftStdlib 6 . 2 , * )
148
+ @_alwaysEmitIntoClient
149
+ @_transparent
150
+ internal var _protectedMutableAddress : UnsafeMutablePointer < Element > {
151
+ mutating get {
152
+ #if $AddressOfProperty
153
+ unsafe UnsafeMutablePointer< Element > ( Builtin . addressof ( & _storage) )
154
+ #else
155
+ unsafe UnsafeMutablePointer< Element > ( Builtin . addressof ( & self ) )
156
+ #endif
157
+ }
158
+ }
159
+
160
+ /// Returns a mutable buffer pointer over the entire array while performing
161
+ /// stack checking.
162
+ ///
163
+ /// Use this when the value of the pointer could potentially be directly used
164
+ /// by users (e.g. through the use of span or the unchecked subscript).
165
+ @available ( SwiftStdlib 6 . 2 , * )
166
+ @_alwaysEmitIntoClient
167
+ @_transparent
168
+ internal var _protectedMutableBuffer : UnsafeMutableBufferPointer < Element > {
169
+ mutating get {
170
+ unsafe UnsafeMutableBufferPointer< Element > (
171
+ start: _protectedMutableAddress,
172
+ count: count
173
+ )
174
+ }
175
+ }
176
+
114
177
/// Converts the given raw pointer, which points at an uninitialized array
115
178
/// instance, to a mutable buffer suitable for initialization.
116
179
@available ( SwiftStdlib 6 . 2 , * )
@@ -415,12 +478,12 @@ extension InlineArray where Element: ~Copyable {
415
478
public subscript( unchecked i: Index ) -> Element {
416
479
@_transparent
417
480
unsafeAddress {
418
- unsafe _address + i
481
+ unsafe _protectedAddress + i
419
482
}
420
483
421
484
@_transparent
422
485
unsafeMutableAddress {
423
- unsafe _mutableAddress + i
486
+ unsafe _protectedMutableAddress + i
424
487
}
425
488
}
426
489
}
@@ -467,53 +530,28 @@ extension InlineArray where Element: ~Copyable {
467
530
468
531
@available ( SwiftStdlib 6 . 2 , * )
469
532
extension InlineArray where Element: ~ Copyable {
470
-
471
533
@available ( SwiftStdlib 6 . 2 , * )
534
+ @_alwaysEmitIntoClient
472
535
public var span: Span< Element > {
473
536
@lifetime ( borrow self)
474
- @_alwaysEmitIntoClient
537
+ @_transparent
475
538
borrowing get {
476
- let pointer = unsafe _address
477
- let span = unsafe Span( _unsafeStart: pointer, count: count)
539
+ let span = unsafe Span( _unsafeStart: _protectedAddress, count: count)
478
540
return unsafe _override Lifetime( span, borrowing : self)
479
541
}
480
542
}
481
543
482
544
@available ( SwiftStdlib 6 . 2 , * )
545
+ @_alwaysEmitIntoClient
483
546
public var mutableSpan : MutableSpan < Element > {
484
547
@lifetime ( & self )
485
- @_alwaysEmitIntoClient
548
+ @_transparent
486
549
mutating get {
487
- let pointer = unsafe _mutableAddress
488
- let span = unsafe MutableSpan( _unsafeStart: pointer, count: count)
550
+ let span = unsafe MutableSpan(
551
+ _unsafeStart: _protectedMutableAddress,
552
+ count: count
553
+ )
489
554
return unsafe _override Lifetime ( span, mutating: & self )
490
555
}
491
556
}
492
557
}
493
-
494
- //===----------------------------------------------------------------------===//
495
- // MARK: - Unsafe APIs
496
- //===----------------------------------------------------------------------===//
497
-
498
- @available ( SwiftStdlib 6 . 2 , * )
499
- extension InlineArray where Element: ~ Copyable {
500
- // FIXME: @available(*, deprecated, renamed: "span.withUnsafeBufferPointer(_:)")
501
- @available ( SwiftStdlib 6 . 2 , * )
502
- @_alwaysEmitIntoClient
503
- @_transparent
504
- public borrowing func _withUnsafeBufferPointer< Result: ~ Copyable, E: Error > (
505
- _ body: ( UnsafeBufferPointer < Element > ) throws ( E ) -> Result
506
- ) throws ( E) -> Result {
507
- try unsafe body( _buffer)
508
- }
509
-
510
- // FIXME: @available(*, deprecated, renamed: "mutableSpan.withUnsafeMutableBufferPointer(_:)")
511
- @available ( SwiftStdlib 6 . 2 , * )
512
- @_alwaysEmitIntoClient
513
- @_transparent
514
- public mutating func _withUnsafeMutableBufferPointer< Result: ~ Copyable, E: Error > (
515
- _ body: ( UnsafeMutableBufferPointer < Element > ) throws ( E ) -> Result
516
- ) throws ( E) -> Result {
517
- try unsafe body( _mutableBuffer)
518
- }
519
- }
0 commit comments