Skip to content

Commit 09a5018

Browse files
committed
[stdlib] convert withUnsafeBytes() to typed throws
1 parent 0cc0590 commit 09a5018

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,10 +1288,23 @@ public func _withUnprotectedUnsafeBytes<T, Result>(
12881288
/// `withUnsafeMutableBytes(of:_:)` instead.
12891289
/// - Returns: The return value, if any, of the `body` closure.
12901290
@inlinable
1291-
public func withUnsafeBytes<T, Result>(
1291+
@_alwaysEmitIntoClient
1292+
public func withUnsafeBytes<T, Result, E: Error>(
1293+
of value: T,
1294+
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
1295+
) throws(E) -> Result {
1296+
let addr = UnsafeRawPointer(Builtin.addressOfBorrow(value))
1297+
return try body(.init(start: addr, count: MemoryLayout<T>.size))
1298+
}
1299+
1300+
/// ABI: Historical withUnsafeBytes(of:_:) rethrows,
1301+
/// expressed as "throws", which is ABI-compatible with "rethrows".
1302+
@_silgen_name("$ss15withUnsafeBytes2of_q_x_q_SWKXEtKr0_lF")
1303+
@usableFromInline
1304+
func __abi_se0413_withUnsafeBytes<T, Result>(
12921305
of value: T,
12931306
_ body: (UnsafeRawBufferPointer) throws -> Result
1294-
) rethrows -> Result {
1307+
) throws -> Result {
12951308
let addr = UnsafeRawPointer(Builtin.addressOfBorrow(value))
12961309
let buffer = UnsafeRawBufferPointer(start: addr, count: MemoryLayout<T>.size)
12971310
return try body(buffer)
@@ -1303,10 +1316,10 @@ public func withUnsafeBytes<T, Result>(
13031316
/// This function is similar to `withUnsafeBytes`, except that it
13041317
/// doesn't trigger stack protection for the pointer.
13051318
@_alwaysEmitIntoClient
1306-
public func _withUnprotectedUnsafeBytes<T, Result>(
1319+
public func _withUnprotectedUnsafeBytes<T, Result, E: Error>(
13071320
of value: T,
1308-
_ body: (UnsafeRawBufferPointer) throws -> Result
1309-
) rethrows -> Result {
1321+
_ body: (UnsafeRawBufferPointer) throws(E) -> Result
1322+
) throws(E) -> Result {
13101323
#if $BuiltinUnprotectedAddressOf
13111324
let addr = UnsafeRawPointer(Builtin.unprotectedAddressOfBorrow(value))
13121325
#else

0 commit comments

Comments
 (0)