@@ -80,6 +80,30 @@ extension InlineArray where Element: ~Copyable {
80
80
unsafe UnsafeBufferPointer< Element > ( start: _address, count: count)
81
81
}
82
82
83
+ /// Returns a pointer to the first element in the array while performing stack
84
+ /// checking.
85
+ ///
86
+ /// Use this when the value of the pointer could potentially be directly used
87
+ /// by users (e.g. through the use of span or the unchecked subscript).
88
+ @available ( SwiftStdlib 6 . 2 , * )
89
+ @_alwaysEmitIntoClient
90
+ @_transparent
91
+ internal var _protectedAddress : UnsafePointer < Element > {
92
+ unsafe UnsafePointer< Element > ( Builtin . addressOfBorrow ( self ) )
93
+ }
94
+
95
+ /// Returns a buffer pointer over the entire array while performing stack
96
+ /// checking.
97
+ ///
98
+ /// Use this when the value of the pointer could potentially be directly used
99
+ /// by users (e.g. through the use of span or the unchecked subscript).
100
+ @available ( SwiftStdlib 6 . 2 , * )
101
+ @_alwaysEmitIntoClient
102
+ @_transparent
103
+ internal var _protectedBuffer : UnsafeBufferPointer < Element > {
104
+ unsafe UnsafeBufferPointer< Element > ( start: _protectedAddress, count: count)
105
+ }
106
+
83
107
/// Returns a mutable pointer to the first element in the array.
84
108
@available ( SwiftStdlib 6 . 2 , * )
85
109
@_alwaysEmitIntoClient
@@ -103,6 +127,37 @@ extension InlineArray where Element: ~Copyable {
103
127
}
104
128
}
105
129
130
+ /// Returns a mutable pointer to the first element in the array while
131
+ /// performing stack checking.
132
+ ///
133
+ /// Use this when the value of the pointer could potentially be directly used
134
+ /// by users (e.g. through the use of span or the unchecked subscript).
135
+ @available ( SwiftStdlib 6 . 2 , * )
136
+ @_alwaysEmitIntoClient
137
+ @_transparent
138
+ internal var _protectedMutableAddress : UnsafeMutablePointer < Element > {
139
+ mutating get {
140
+ unsafe UnsafeMutablePointer< Element > ( Builtin . addressOf ( & self ) )
141
+ }
142
+ }
143
+
144
+ /// Returns a mutable buffer pointer over the entire array while performing
145
+ /// stack checking.
146
+ ///
147
+ /// Use this when the value of the pointer could potentially be directly used
148
+ /// by users (e.g. through the use of span or the unchecked subscript).
149
+ @available ( SwiftStdlib 6 . 2 , * )
150
+ @_alwaysEmitIntoClient
151
+ @_transparent
152
+ internal var _protectedMutableBuffer : UnsafeMutableBufferPointer < Element > {
153
+ mutating get {
154
+ unsafe UnsafeMutableBufferPointer< Element > (
155
+ start: _protectedMutableAddress,
156
+ count: count
157
+ )
158
+ }
159
+ }
160
+
106
161
/// Converts the given raw pointer, which points at an uninitialized array
107
162
/// instance, to a mutable buffer suitable for initialization.
108
163
@available ( SwiftStdlib 6 . 2 , * )
@@ -402,12 +457,12 @@ extension InlineArray where Element: ~Copyable {
402
457
public subscript( unchecked i: Index ) -> Element {
403
458
@_transparent
404
459
unsafeAddress {
405
- unsafe _address + i
460
+ unsafe _protectedAddress + i
406
461
}
407
462
408
463
@_transparent
409
464
unsafeMutableAddress {
410
- unsafe _mutableAddress + i
465
+ unsafe _protectedMutableAddress + i
411
466
}
412
467
}
413
468
}
@@ -460,8 +515,7 @@ extension InlineArray where Element: ~Copyable {
460
515
@lifetime ( borrow self)
461
516
@_alwaysEmitIntoClient
462
517
borrowing get {
463
- let pointer = unsafe _address
464
- let span = unsafe Span( _unsafeStart: pointer, count: count)
518
+ let span = unsafe Span( _unsafeStart: _protectedAddress, count: count)
465
519
return unsafe _override Lifetime( span, borrowing : self)
466
520
}
467
521
}
@@ -471,36 +525,11 @@ extension InlineArray where Element: ~Copyable {
471
525
@lifetime ( & self )
472
526
@_alwaysEmitIntoClient
473
527
mutating get {
474
- let pointer = unsafe _mutableAddress
475
- let span = unsafe MutableSpan( _unsafeStart: pointer, count: count)
528
+ let span = unsafe MutableSpan(
529
+ _unsafeStart: _protectedMutableAddress,
530
+ count: count
531
+ )
476
532
return unsafe _override Lifetime ( span, mutating: & self )
477
533
}
478
534
}
479
535
}
480
-
481
- //===----------------------------------------------------------------------===//
482
- // MARK: - Unsafe APIs
483
- //===----------------------------------------------------------------------===//
484
-
485
- @available ( SwiftStdlib 6 . 2 , * )
486
- extension InlineArray where Element: ~ Copyable {
487
- // FIXME: @available(*, deprecated, renamed: "span.withUnsafeBufferPointer(_:)")
488
- @available ( SwiftStdlib 6 . 2 , * )
489
- @_alwaysEmitIntoClient
490
- @_transparent
491
- public borrowing func _withUnsafeBufferPointer< Result: ~ Copyable, E: Error > (
492
- _ body: ( UnsafeBufferPointer < Element > ) throws ( E ) -> Result
493
- ) throws ( E) -> Result {
494
- try unsafe body( _buffer)
495
- }
496
-
497
- // FIXME: @available(*, deprecated, renamed: "mutableSpan.withUnsafeMutableBufferPointer(_:)")
498
- @available ( SwiftStdlib 6 . 2 , * )
499
- @_alwaysEmitIntoClient
500
- @_transparent
501
- public mutating func _withUnsafeMutableBufferPointer< Result: ~ Copyable, E: Error > (
502
- _ body: ( UnsafeMutableBufferPointer < Element > ) throws ( E ) -> Result
503
- ) throws ( E) -> Result {
504
- try unsafe body( _mutableBuffer)
505
- }
506
- }
0 commit comments