@@ -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 , * )
@@ -407,12 +462,12 @@ extension InlineArray where Element: ~Copyable {
407
462
public subscript( unchecked i: Index ) -> Element {
408
463
@_transparent
409
464
unsafeAddress {
410
- unsafe _address + i
465
+ unsafe _protectedAddress + i
411
466
}
412
467
413
468
@_transparent
414
469
unsafeMutableAddress {
415
- unsafe _mutableAddress + i
470
+ unsafe _protectedMutableAddress + i
416
471
}
417
472
}
418
473
}
@@ -465,8 +520,7 @@ extension InlineArray where Element: ~Copyable {
465
520
@lifetime ( borrow self)
466
521
@_alwaysEmitIntoClient
467
522
borrowing get {
468
- let pointer = unsafe _address
469
- let span = unsafe Span( _unsafeStart: pointer, count: count)
523
+ let span = unsafe Span( _unsafeStart: _protectedAddress, count: count)
470
524
return unsafe _override Lifetime( span, borrowing : self)
471
525
}
472
526
}
@@ -476,36 +530,11 @@ extension InlineArray where Element: ~Copyable {
476
530
@lifetime ( & self )
477
531
@_alwaysEmitIntoClient
478
532
mutating get {
479
- let pointer = unsafe _mutableAddress
480
- let span = unsafe MutableSpan( _unsafeStart: pointer, count: count)
533
+ let span = unsafe MutableSpan(
534
+ _unsafeStart: _protectedMutableAddress,
535
+ count: count
536
+ )
481
537
return unsafe _override Lifetime ( span, mutating: & self )
482
538
}
483
539
}
484
540
}
485
-
486
- //===----------------------------------------------------------------------===//
487
- // MARK: - Unsafe APIs
488
- //===----------------------------------------------------------------------===//
489
-
490
- @available ( SwiftStdlib 6 . 2 , * )
491
- extension InlineArray where Element: ~ Copyable {
492
- // FIXME: @available(*, deprecated, renamed: "span.withUnsafeBufferPointer(_:)")
493
- @available ( SwiftStdlib 6 . 2 , * )
494
- @_alwaysEmitIntoClient
495
- @_transparent
496
- public borrowing func _withUnsafeBufferPointer< Result: ~ Copyable, E: Error > (
497
- _ body: ( UnsafeBufferPointer < Element > ) throws ( E ) -> Result
498
- ) throws ( E) -> Result {
499
- try unsafe body( _buffer)
500
- }
501
-
502
- // FIXME: @available(*, deprecated, renamed: "mutableSpan.withUnsafeMutableBufferPointer(_:)")
503
- @available ( SwiftStdlib 6 . 2 , * )
504
- @_alwaysEmitIntoClient
505
- @_transparent
506
- public mutating func _withUnsafeMutableBufferPointer< Result: ~ Copyable, E: Error > (
507
- _ body: ( UnsafeMutableBufferPointer < Element > ) throws ( E ) -> Result
508
- ) throws ( E) -> Result {
509
- try unsafe body( _mutableBuffer)
510
- }
511
- }
0 commit comments