@@ -80,6 +80,34 @@ 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
+ #if $AddressOfProperty
93
+ unsafe UnsafePointer< Element > ( Builtin . addressOfBorrow ( _storage) )
94
+ #else
95
+ unsafe UnsafePointer< Element > ( Builtin . addressOfBorrow ( self ) )
96
+ #endif
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
+
83
111
/// Returns a mutable pointer to the first element in the array.
84
112
@available ( SwiftStdlib 6 . 2 , * )
85
113
@_alwaysEmitIntoClient
@@ -103,6 +131,41 @@ extension InlineArray where Element: ~Copyable {
103
131
}
104
132
}
105
133
134
+ /// Returns a mutable pointer to the first element in the array while
135
+ /// performing stack checking.
136
+ ///
137
+ /// Use this when the value of the pointer could potentially be directly used
138
+ /// by users (e.g. through the use of span or the unchecked subscript).
139
+ @available ( SwiftStdlib 6 . 2 , * )
140
+ @_alwaysEmitIntoClient
141
+ @_transparent
142
+ internal var _protectedMutableAddress : UnsafeMutablePointer < Element > {
143
+ mutating get {
144
+ #if $AddressOfProperty
145
+ unsafe UnsafeMutablePointer< Element > ( Builtin . addressof ( & _storage) )
146
+ #else
147
+ unsafe UnsafeMutablePointer< Element > ( Builtin . addressof ( & self ) )
148
+ #endif
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
+
106
169
/// Converts the given raw pointer, which points at an uninitialized array
107
170
/// instance, to a mutable buffer suitable for initialization.
108
171
@available ( SwiftStdlib 6 . 2 , * )
@@ -407,12 +470,12 @@ extension InlineArray where Element: ~Copyable {
407
470
public subscript( unchecked i: Index ) -> Element {
408
471
@_transparent
409
472
unsafeAddress {
410
- unsafe _address + i
473
+ unsafe _protectedAddress + i
411
474
}
412
475
413
476
@_transparent
414
477
unsafeMutableAddress {
415
- unsafe _mutableAddress + i
478
+ unsafe _protectedMutableAddress + i
416
479
}
417
480
}
418
481
}
@@ -459,53 +522,28 @@ extension InlineArray where Element: ~Copyable {
459
522
460
523
@available ( SwiftStdlib 6 . 2 , * )
461
524
extension InlineArray where Element: ~ Copyable {
462
-
463
525
@available ( SwiftStdlib 6 . 2 , * )
526
+ @_alwaysEmitIntoClient
464
527
public var span: Span< Element > {
465
528
@lifetime ( borrow self)
466
- @_alwaysEmitIntoClient
529
+ @_transparent
467
530
borrowing get {
468
- let pointer = unsafe _address
469
- let span = unsafe Span( _unsafeStart: pointer, count: count)
531
+ let span = unsafe Span( _unsafeStart: _protectedAddress, count: count)
470
532
return unsafe _override Lifetime( span, borrowing : self)
471
533
}
472
534
}
473
535
474
536
@available ( SwiftStdlib 6 . 2 , * )
537
+ @_alwaysEmitIntoClient
475
538
public var mutableSpan : MutableSpan < Element > {
476
539
@lifetime ( & self )
477
- @_alwaysEmitIntoClient
540
+ @_transparent
478
541
mutating get {
479
- let pointer = unsafe _mutableAddress
480
- let span = unsafe MutableSpan( _unsafeStart: pointer, count: count)
542
+ let span = unsafe MutableSpan(
543
+ _unsafeStart: _protectedMutableAddress,
544
+ count: count
545
+ )
481
546
return unsafe _override Lifetime ( span, mutating: & self )
482
547
}
483
548
}
484
549
}
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