Skip to content

Commit 8058bd2

Browse files
committed
Remove underscored with buffer pointer APIs on InlineArray
1 parent d385e8c commit 8058bd2

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
@@ -84,6 +84,30 @@ extension InlineArray where Element: ~Copyable {
8484
unsafe UnsafeBufferPointer<Element>(start: _address, count: count)
8585
}
8686

87+
/// Returns a pointer to the first element in the array while performing stack
88+
/// checking.
89+
///
90+
/// Use this when the value of the pointer could potentially be directly used
91+
/// by users (e.g. through the use of span or the unchecked subscript).
92+
@available(SwiftStdlib 6.2, *)
93+
@_alwaysEmitIntoClient
94+
@_transparent
95+
internal var _protectedAddress: UnsafePointer<Element> {
96+
unsafe UnsafePointer<Element>(Builtin.addressOfBorrow(self))
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+
87111
/// Returns a mutable pointer to the first element in the array.
88112
@available(SwiftStdlib 6.2, *)
89113
@_alwaysEmitIntoClient
@@ -111,6 +135,37 @@ extension InlineArray where Element: ~Copyable {
111135
}
112136
}
113137

138+
/// Returns a mutable pointer to the first element in the array while
139+
/// performing stack checking.
140+
///
141+
/// Use this when the value of the pointer could potentially be directly used
142+
/// by users (e.g. through the use of span or the unchecked subscript).
143+
@available(SwiftStdlib 6.2, *)
144+
@_alwaysEmitIntoClient
145+
@_transparent
146+
internal var _protectedMutableAddress: UnsafeMutablePointer<Element> {
147+
mutating get {
148+
unsafe UnsafeMutablePointer<Element>(Builtin.addressOf(&self))
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+
114169
/// Converts the given raw pointer, which points at an uninitialized array
115170
/// instance, to a mutable buffer suitable for initialization.
116171
@available(SwiftStdlib 6.2, *)
@@ -415,12 +470,12 @@ extension InlineArray where Element: ~Copyable {
415470
public subscript(unchecked i: Index) -> Element {
416471
@_transparent
417472
unsafeAddress {
418-
unsafe _address + i
473+
unsafe _protectedAddress + i
419474
}
420475

421476
@_transparent
422477
unsafeMutableAddress {
423-
unsafe _mutableAddress + i
478+
unsafe _protectedMutableAddress + i
424479
}
425480
}
426481
}
@@ -473,8 +528,7 @@ extension InlineArray where Element: ~Copyable {
473528
@lifetime(borrow self)
474529
@_alwaysEmitIntoClient
475530
borrowing get {
476-
let pointer = unsafe _address
477-
let span = unsafe Span(_unsafeStart: pointer, count: count)
531+
let span = unsafe Span(_unsafeStart: _protectedAddress, count: count)
478532
return unsafe _overrideLifetime(span, borrowing: self)
479533
}
480534
}
@@ -484,36 +538,11 @@ extension InlineArray where Element: ~Copyable {
484538
@lifetime(&self)
485539
@_alwaysEmitIntoClient
486540
mutating get {
487-
let pointer = unsafe _mutableAddress
488-
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
541+
let span = unsafe MutableSpan(
542+
_unsafeStart: _protectedMutableAddress,
543+
count: count
544+
)
489545
return unsafe _overrideLifetime(span, mutating: &self)
490546
}
491547
}
492548
}
493-
494-
//===----------------------------------------------------------------------===//
495-
// MARK: - Unsafe APIs
496-
//===----------------------------------------------------------------------===//
497-
498-
@available(SwiftStdlib 6.2, *)
499-
extension InlineArray where Element: ~Copyable {
500-
// FIXME: @available(*, deprecated, renamed: "span.withUnsafeBufferPointer(_:)")
501-
@available(SwiftStdlib 6.2, *)
502-
@_alwaysEmitIntoClient
503-
@_transparent
504-
public borrowing func _withUnsafeBufferPointer<Result: ~Copyable, E: Error>(
505-
_ body: (UnsafeBufferPointer<Element>) throws(E) -> Result
506-
) throws(E) -> Result {
507-
try unsafe body(_buffer)
508-
}
509-
510-
// FIXME: @available(*, deprecated, renamed: "mutableSpan.withUnsafeMutableBufferPointer(_:)")
511-
@available(SwiftStdlib 6.2, *)
512-
@_alwaysEmitIntoClient
513-
@_transparent
514-
public mutating func _withUnsafeMutableBufferPointer<Result: ~Copyable, E: Error>(
515-
_ body: (UnsafeMutableBufferPointer<Element>) throws(E) -> Result
516-
) throws(E) -> Result {
517-
try unsafe body(_mutableBuffer)
518-
}
519-
}

0 commit comments

Comments
 (0)