Skip to content

Commit f546bc8

Browse files
Azoyglessard
authored andcommitted
Remove underscored with buffer pointer APIs on InlineArray
1 parent 92fd571 commit f546bc8

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, *)
@@ -407,12 +462,12 @@ extension InlineArray where Element: ~Copyable {
407462
public subscript(unchecked i: Index) -> Element {
408463
@_transparent
409464
unsafeAddress {
410-
unsafe _address + i
465+
unsafe _protectedAddress + i
411466
}
412467

413468
@_transparent
414469
unsafeMutableAddress {
415-
unsafe _mutableAddress + i
470+
unsafe _protectedMutableAddress + i
416471
}
417472
}
418473
}
@@ -465,8 +520,7 @@ extension InlineArray where Element: ~Copyable {
465520
@lifetime(borrow self)
466521
@_alwaysEmitIntoClient
467522
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)
470524
return unsafe _overrideLifetime(span, borrowing: self)
471525
}
472526
}
@@ -476,36 +530,11 @@ extension InlineArray where Element: ~Copyable {
476530
@lifetime(&self)
477531
@_alwaysEmitIntoClient
478532
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+
)
481537
return unsafe _overrideLifetime(span, mutating: &self)
482538
}
483539
}
484540
}
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

Comments
 (0)