Skip to content

Commit 8374d79

Browse files
committed
Remove underscored with buffer pointer APIs on InlineArray
1 parent 2f133b3 commit 8374d79

File tree

1 file changed

+62
-33
lines changed

1 file changed

+62
-33
lines changed

stdlib/public/core/InlineArray.swift

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,30 @@ extension InlineArray where Element: ~Copyable {
8080
unsafe UnsafeBufferPointer<Element>(start: _address, count: count)
8181
}
8282

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+
83107
/// Returns a mutable pointer to the first element in the array.
84108
@available(SwiftStdlib 6.2, *)
85109
@_alwaysEmitIntoClient
@@ -103,6 +127,37 @@ extension InlineArray where Element: ~Copyable {
103127
}
104128
}
105129

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+
106161
/// Converts the given raw pointer, which points at an uninitialized array
107162
/// instance, to a mutable buffer suitable for initialization.
108163
@available(SwiftStdlib 6.2, *)
@@ -402,12 +457,12 @@ extension InlineArray where Element: ~Copyable {
402457
public subscript(unchecked i: Index) -> Element {
403458
@_transparent
404459
unsafeAddress {
405-
unsafe _address + i
460+
unsafe _protectedAddress + i
406461
}
407462

408463
@_transparent
409464
unsafeMutableAddress {
410-
unsafe _mutableAddress + i
465+
unsafe _protectedMutableAddress + i
411466
}
412467
}
413468
}
@@ -460,8 +515,7 @@ extension InlineArray where Element: ~Copyable {
460515
@lifetime(borrow self)
461516
@_alwaysEmitIntoClient
462517
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)
465519
return unsafe _overrideLifetime(span, borrowing: self)
466520
}
467521
}
@@ -471,36 +525,11 @@ extension InlineArray where Element: ~Copyable {
471525
@lifetime(&self)
472526
@_alwaysEmitIntoClient
473527
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+
)
476532
return unsafe _overrideLifetime(span, mutating: &self)
477533
}
478534
}
479535
}
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

Comments
 (0)