Skip to content

Commit 3507a0c

Browse files
committed
[stdlib] Add support for noncopyables to withUnsafePointer(to:) family
1 parent 285c3d3 commit 3507a0c

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

stdlib/public/core/LifetimeManager.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,20 @@ public func _fixLifetime<T: ~Copyable>(_ x: borrowing T) {
7777
/// The pointer argument is valid only for the duration of the function's
7878
/// execution.
7979
/// - Returns: The return value, if any, of the `body` closure.
80-
@inlinable
8180
@_alwaysEmitIntoClient
82-
public func withUnsafeMutablePointer<T, E: Error, Result>(
81+
public func withUnsafeMutablePointer<
82+
T: ~Copyable, E: Error, Result: ~Copyable
83+
>(
8384
to value: inout T,
8485
_ body: (UnsafeMutablePointer<T>) throws(E) -> Result
8586
) throws(E) -> Result {
8687
try body(UnsafeMutablePointer<T>(Builtin.addressof(&value)))
8788
}
8889

90+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 2)
8991
@_silgen_name("$ss24withUnsafeMutablePointer2to_q_xz_q_SpyxGKXEtKr0_lF")
9092
@usableFromInline
91-
func __abi_se0413_withUnsafeMutablePointer<T, Result>(
93+
internal func __abi_se0413_withUnsafeMutablePointer<T, Result>(
9294
to value: inout T,
9395
_ body: (UnsafeMutablePointer<T>) throws -> Result
9496
) throws -> Result {
@@ -100,7 +102,9 @@ func __abi_se0413_withUnsafeMutablePointer<T, Result>(
100102
/// This function is similar to `withUnsafeMutablePointer`, except that it
101103
/// doesn't trigger stack protection for the pointer.
102104
@_alwaysEmitIntoClient
103-
public func _withUnprotectedUnsafeMutablePointer<T, E: Error, Result>(
105+
public func _withUnprotectedUnsafeMutablePointer<
106+
T: ~Copyable, E: Error, Result: ~Copyable
107+
>(
104108
to value: inout T,
105109
_ body: (UnsafeMutablePointer<T>) throws(E) -> Result
106110
) throws(E) -> Result
@@ -133,9 +137,8 @@ public func _withUnprotectedUnsafeMutablePointer<T, E: Error, Result>(
133137
/// `withUnsafeMutablePointer(to:_:)` instead.
134138
/// - Returns: The return value, if any, of the `body` closure.
135139
@_alwaysEmitIntoClient
136-
@inlinable
137-
public func withUnsafePointer<T, E, Result>(
138-
to value: T,
140+
public func withUnsafePointer<T: ~Copyable, E, Result: ~Copyable>(
141+
to value: borrowing T,
139142
_ body: (UnsafePointer<T>) throws(E) -> Result
140143
) throws(E) -> Result
141144
{
@@ -144,9 +147,10 @@ public func withUnsafePointer<T, E, Result>(
144147

145148
/// ABI: Historical withUnsafePointer(to:_:) rethrows, expressed as "throws",
146149
/// which is ABI-compatible with "rethrows".
150+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 2)
147151
@_silgen_name("$ss17withUnsafePointer2to_q_x_q_SPyxGKXEtKr0_lF")
148152
@usableFromInline
149-
func __abi_withUnsafePointer<T, Result>(
153+
internal func __abi_withUnsafePointer<T, Result>(
150154
to value: T,
151155
_ body: (UnsafePointer<T>) throws -> Result
152156
) throws -> Result
@@ -178,9 +182,8 @@ func __abi_withUnsafePointer<T, Result>(
178182
/// type. If you need to mutate the argument through the pointer, use
179183
/// `withUnsafeMutablePointer(to:_:)` instead.
180184
/// - Returns: The return value, if any, of the `body` closure.
181-
@inlinable
182185
@_alwaysEmitIntoClient
183-
public func withUnsafePointer<T, E: Error, Result>(
186+
public func withUnsafePointer<T: ~Copyable, E: Error, Result: ~Copyable>(
184187
to value: inout T,
185188
_ body: (UnsafePointer<T>) throws(E) -> Result
186189
) throws(E) -> Result {
@@ -189,9 +192,10 @@ public func withUnsafePointer<T, E: Error, Result>(
189192

190193
/// ABI: Historical withUnsafePointer(to:_:) rethrows,
191194
/// expressed as "throws", which is ABI-compatible with "rethrows".
195+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 2)
192196
@_silgen_name("$ss17withUnsafePointer2to_q_xz_q_SPyxGKXEtKr0_lF")
193197
@usableFromInline
194-
func __abi_se0413_withUnsafePointer<T, Result>(
198+
internal func __abi_se0413_withUnsafePointer<T, Result>(
195199
to value: inout T,
196200
_ body: (UnsafePointer<T>) throws -> Result
197201
) throws -> Result {
@@ -203,7 +207,9 @@ func __abi_se0413_withUnsafePointer<T, Result>(
203207
/// This function is similar to `withUnsafePointer`, except that it
204208
/// doesn't trigger stack protection for the pointer.
205209
@_alwaysEmitIntoClient
206-
public func _withUnprotectedUnsafePointer<T, E: Error, Result>(
210+
public func _withUnprotectedUnsafePointer<
211+
T: ~Copyable, E: Error, Result: ~Copyable
212+
>(
207213
to value: inout T,
208214
_ body: (UnsafePointer<T>) throws(E) -> Result
209215
) throws(E) -> Result {

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ extension ${Self} {
12281228
/// of the closure's execution.
12291229
/// - Returns: The return value, if any, of the `body` closure.
12301230
@_alwaysEmitIntoClient
1231-
public func withUnsafeMutableBytes<T, E: Error, Result>(
1231+
public func withUnsafeMutableBytes<T: ~Copyable, E: Error, Result: ~Copyable>(
12321232
of value: inout T,
12331233
_ body: (UnsafeMutableRawBufferPointer) throws(E) -> Result
12341234
) throws(E) -> Result {
@@ -1238,6 +1238,7 @@ public func withUnsafeMutableBytes<T, E: Error, Result>(
12381238

12391239
/// ABI: Historical withUnsafeMutableBytes(of:_:) rethrows,
12401240
/// expressed as "throws", which is ABI-compatible with "rethrows".
1241+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 2)
12411242
@_silgen_name("$ss22withUnsafeMutableBytes2of_q_xz_q_SwKXEtKr0_lF")
12421243
@usableFromInline
12431244
func __abi_se0413_withUnsafeMutableBytes<T, Result>(
@@ -1256,7 +1257,9 @@ func __abi_se0413_withUnsafeMutableBytes<T, Result>(
12561257
/// This function is similar to `withUnsafeMutableBytes`, except that it
12571258
/// doesn't trigger stack protection for the pointer.
12581259
@_alwaysEmitIntoClient
1259-
public func _withUnprotectedUnsafeMutableBytes<T, E: Error, Result>(
1260+
public func _withUnprotectedUnsafeMutableBytes<
1261+
T: ~Copyable, E: Error, Result: ~Copyable
1262+
>(
12601263
of value: inout T,
12611264
_ body: (UnsafeMutableRawBufferPointer) throws(E) -> Result
12621265
) throws(E) -> Result {
@@ -1292,7 +1295,7 @@ public func _withUnprotectedUnsafeMutableBytes<T, E: Error, Result>(
12921295
/// `withUnsafeMutableBytes(of:_:)` instead.
12931296
/// - Returns: The return value, if any, of the `body` closure.
12941297
@_alwaysEmitIntoClient
1295-
public func withUnsafeBytes<T, E: Error, Result>(
1298+
public func withUnsafeBytes<T: ~Copyable, E: Error, Result: ~Copyable>(
12961299
of value: inout T,
12971300
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
12981301
) throws(E) -> Result {
@@ -1302,6 +1305,7 @@ public func withUnsafeBytes<T, E: Error, Result>(
13021305

13031306
/// ABI: Historical withUnsafeBytes(of:_:) rethrows,
13041307
/// expressed as "throws", which is ABI-compatible with "rethrows".
1308+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 2)
13051309
@_silgen_name("$ss15withUnsafeBytes2of_q_xz_q_SWKXEtKr0_lF")
13061310
@usableFromInline
13071311
func __abi_se0413_withUnsafeBytes<T, Result>(
@@ -1319,7 +1323,9 @@ func __abi_se0413_withUnsafeBytes<T, Result>(
13191323
/// This function is similar to `withUnsafeBytes`, except that it
13201324
/// doesn't trigger stack protection for the pointer.
13211325
@_alwaysEmitIntoClient
1322-
public func _withUnprotectedUnsafeBytes<T, E: Error, Result>(
1326+
public func _withUnprotectedUnsafeBytes<
1327+
T: ~Copyable, E: Error, Result: ~Copyable
1328+
>(
13231329
of value: inout T,
13241330
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
13251331
) throws(E) -> Result {
@@ -1351,8 +1357,10 @@ public func _withUnprotectedUnsafeBytes<T, E: Error, Result>(
13511357
/// `withUnsafeMutableBytes(of:_:)` instead.
13521358
/// - Returns: The return value, if any, of the `body` closure.
13531359
@_alwaysEmitIntoClient
1354-
public func withUnsafeBytes<T, E: Error, Result>(
1355-
of value: T,
1360+
public func withUnsafeBytes<
1361+
T: ~Copyable, E: Error, Result: ~Copyable
1362+
>(
1363+
of value: borrowing T,
13561364
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
13571365
) throws(E) -> Result {
13581366
let addr = UnsafeRawPointer(Builtin.addressOfBorrow(value))
@@ -1361,6 +1369,7 @@ public func withUnsafeBytes<T, E: Error, Result>(
13611369

13621370
/// ABI: Historical withUnsafeBytes(of:_:) rethrows,
13631371
/// expressed as "throws", which is ABI-compatible with "rethrows".
1372+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 2)
13641373
@_silgen_name("$ss15withUnsafeBytes2of_q_x_q_SWKXEtKr0_lF")
13651374
@usableFromInline
13661375
func __abi_se0413_withUnsafeBytes<T, Result>(
@@ -1378,8 +1387,10 @@ func __abi_se0413_withUnsafeBytes<T, Result>(
13781387
/// This function is similar to `withUnsafeBytes`, except that it
13791388
/// doesn't trigger stack protection for the pointer.
13801389
@_alwaysEmitIntoClient
1381-
public func _withUnprotectedUnsafeBytes<T, E: Error, Result>(
1382-
of value: T,
1390+
public func _withUnprotectedUnsafeBytes<
1391+
T: ~Copyable, E: Error, Result: ~Copyable
1392+
>(
1393+
of value: borrowing T,
13831394
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
13841395
) throws(E) -> Result {
13851396
#if $BuiltinUnprotectedAddressOf

0 commit comments

Comments
 (0)