@@ -84,6 +84,30 @@ 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
+ unsafe UnsafePointer< Element > ( Builtin . addressOfBorrow ( self ) )
97
+ }
98
+
99
+ /// Returns a buffer pointer over the entire array while performing stack
100
+ /// checking.
101
+ ///
102
+ /// Use this when the value of the pointer could potentially be directly used
103
+ /// by users (e.g. through the use of span or the unchecked subscript).
104
+ @available ( SwiftStdlib 6 . 2 , * )
105
+ @_alwaysEmitIntoClient
106
+ @_transparent
107
+ internal var _protectedBuffer : UnsafeBufferPointer < Element > {
108
+ unsafe UnsafeBufferPointer< Element > ( start: _protectedAddress, count: count)
109
+ }
110
+
87
111
/// Returns a mutable pointer to the first element in the array.
88
112
@available ( SwiftStdlib 6 . 2 , * )
89
113
@_alwaysEmitIntoClient
@@ -111,6 +135,37 @@ extension InlineArray where Element: ~Copyable {
111
135
}
112
136
}
113
137
138
+ /// Returns a mutable pointer to the first element in the array while
139
+ /// performing stack checking.
140
+ ///
141
+ /// Use this when the value of the pointer could potentially be directly used
142
+ /// by users (e.g. through the use of span or the unchecked subscript).
143
+ @available ( SwiftStdlib 6 . 2 , * )
144
+ @_alwaysEmitIntoClient
145
+ @_transparent
146
+ internal var _protectedMutableAddress : UnsafeMutablePointer < Element > {
147
+ mutating get {
148
+ unsafe UnsafeMutablePointer< Element > ( Builtin . addressOf ( & self ) )
149
+ }
150
+ }
151
+
152
+ /// Returns a mutable buffer pointer over the entire array while performing
153
+ /// stack checking.
154
+ ///
155
+ /// Use this when the value of the pointer could potentially be directly used
156
+ /// by users (e.g. through the use of span or the unchecked subscript).
157
+ @available ( SwiftStdlib 6 . 2 , * )
158
+ @_alwaysEmitIntoClient
159
+ @_transparent
160
+ internal var _protectedMutableBuffer : UnsafeMutableBufferPointer < Element > {
161
+ mutating get {
162
+ unsafe UnsafeMutableBufferPointer< Element > (
163
+ start: _protectedMutableAddress,
164
+ count: count
165
+ )
166
+ }
167
+ }
168
+
114
169
/// Converts the given raw pointer, which points at an uninitialized array
115
170
/// instance, to a mutable buffer suitable for initialization.
116
171
@available ( SwiftStdlib 6 . 2 , * )
@@ -415,12 +470,12 @@ extension InlineArray where Element: ~Copyable {
415
470
public subscript( unchecked i: Index ) -> Element {
416
471
@_transparent
417
472
unsafeAddress {
418
- unsafe _address + i
473
+ unsafe _protectedAddress + i
419
474
}
420
475
421
476
@_transparent
422
477
unsafeMutableAddress {
423
- unsafe _mutableAddress + i
478
+ unsafe _protectedMutableAddress + i
424
479
}
425
480
}
426
481
}
@@ -473,8 +528,7 @@ extension InlineArray where Element: ~Copyable {
473
528
@lifetime ( borrow self)
474
529
@_alwaysEmitIntoClient
475
530
borrowing get {
476
- let pointer = unsafe _address
477
- let span = unsafe Span( _unsafeStart: pointer, count: count)
531
+ let span = unsafe Span( _unsafeStart: _protectedAddress, count: count)
478
532
return unsafe _override Lifetime( span, borrowing : self)
479
533
}
480
534
}
@@ -484,36 +538,11 @@ extension InlineArray where Element: ~Copyable {
484
538
@lifetime ( & self )
485
539
@_alwaysEmitIntoClient
486
540
mutating get {
487
- let pointer = unsafe _mutableAddress
488
- let span = unsafe MutableSpan( _unsafeStart: pointer, count: count)
541
+ let span = unsafe MutableSpan(
542
+ _unsafeStart: _protectedMutableAddress,
543
+ count: count
544
+ )
489
545
return unsafe _override Lifetime ( span, mutating: & self )
490
546
}
491
547
}
492
548
}
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