Skip to content

Commit 1d4a970

Browse files
authored
Merge pull request #3788 from bob-wilson/withUnsafePointer
[SE-0127] changes to withUnsafe[Mutable]Pointer
2 parents 7680f41 + 58395b3 commit 1d4a970

File tree

14 files changed

+53
-88
lines changed

14 files changed

+53
-88
lines changed

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public func spawnChild(_ args: [String])
118118
// parent write pipe.
119119
let errnoSize = sizeof(errno.dynamicType)
120120
var execveErrno = errno
121-
let writtenBytes = withUnsafePointer(&execveErrno) {
121+
let writtenBytes = withUnsafePointer(to: &execveErrno) {
122122
write(childToParentPipe.writeFD, UnsafePointer($0), errnoSize)
123123
}
124124

stdlib/public/Platform/tgmath.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func _swift_Darwin_lgamma${f}_r(_: ${CT},
334334
@_transparent
335335
public func lgamma(_ x: ${T}) -> (${T}, Int) {
336336
var sign = Int32(0)
337-
let value = withUnsafeMutablePointer(&sign) {
337+
let value = withUnsafeMutablePointer(to: &sign) {
338338
(signp: UnsafeMutablePointer<Int32>) -> ${CT} in
339339
return _swift_Darwin_lgamma${f}_r(${CT}(x), signp)
340340
}

stdlib/public/SDK/Foundation/DateInterval.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable {
156156

157157
public var hashValue: Int {
158158
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate))
159-
return withUnsafeMutablePointer(&buf) {
159+
return withUnsafeMutablePointer(to: &buf) {
160160
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(sizeof(UInt.self) * 2)))
161161
}
162162
}

stdlib/public/SDK/Foundation/UUID.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv
2222

2323
/* Create a new UUID with RFC 4122 version 4 random bytes */
2424
public init() {
25-
withUnsafeMutablePointer(&uuid) {
25+
withUnsafeMutablePointer(to: &uuid) {
2626
uuid_generate_random(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self))
2727
}
2828
}
2929

3030
fileprivate init(reference: NSUUID) {
3131
var bytes: uuid_t = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
32-
withUnsafeMutablePointer(&bytes) {
32+
withUnsafeMutablePointer(to: &bytes) {
3333
reference.getBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self))
3434
}
3535
uuid = bytes
@@ -39,7 +39,7 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv
3939
///
4040
/// Returns nil for invalid strings.
4141
public init?(uuidString string: String) {
42-
let res = withUnsafeMutablePointer(&uuid) {
42+
let res = withUnsafeMutablePointer(to: &uuid) {
4343
return uuid_parse(string, unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self))
4444
}
4545
if res != 0 {
@@ -56,15 +56,17 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv
5656
public var uuidString: String {
5757
var bytes: uuid_string_t = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
5858
var localValue = uuid
59-
return withUnsafeMutablePointers(&localValue, &bytes) { val, str -> String in
60-
uuid_unparse(unsafeBitCast(val, to: UnsafePointer<UInt8>.self), unsafeBitCast(str, to: UnsafeMutablePointer<Int8>.self))
61-
return String(cString: unsafeBitCast(str, to: UnsafePointer<CChar>.self), encoding: .utf8)!
59+
return withUnsafeMutablePointer(to: &localValue) { val in
60+
withUnsafeMutablePointer(to: &bytes) { str in
61+
uuid_unparse(unsafeBitCast(val, to: UnsafePointer<UInt8>.self), unsafeBitCast(str, to: UnsafeMutablePointer<Int8>.self))
62+
return String(cString: unsafeBitCast(str, to: UnsafePointer<CChar>.self), encoding: .utf8)!
63+
}
6264
}
6365
}
6466

6567
public var hashValue: Int {
6668
var localValue = uuid
67-
return withUnsafeMutablePointer(&localValue) {
69+
return withUnsafeMutablePointer(to: &localValue) {
6870
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(sizeof(uuid_t.self))))
6971
}
7072
}
@@ -81,7 +83,7 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv
8183

8284
fileprivate var reference: NSUUID {
8385
var bytes = uuid
84-
return withUnsafePointer(&bytes) {
86+
return withUnsafePointer(to: &bytes) {
8587
return NSUUID(uuidBytes: unsafeBitCast($0, to: UnsafePointer<UInt8>.self))
8688
}
8789
}

stdlib/public/core/CocoaArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ internal struct _CocoaArrayWrapper : RandomAccessCollection {
6161
// subRange.upperBound items are stored contiguously. This is an
6262
// acceptable conservative behavior, but could potentially be
6363
// optimized for other cases.
64-
let contiguousCount = withUnsafeMutablePointer(&enumerationState) {
64+
let contiguousCount = withUnsafeMutablePointer(to: &enumerationState) {
6565
self.buffer.countByEnumerating(with: $0, objects: nil, count: 0)
6666
}
6767

stdlib/public/core/FloatingPointParsing.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extension ${Self} {
5656
let u16 = text.utf16
5757
func parseNTBS(_ chars: UnsafePointer<CChar>) -> (${Self}, Int) {
5858
var result: ${Self} = 0
59-
let endPtr = withUnsafeMutablePointer(&result) {
59+
let endPtr = withUnsafeMutablePointer(to: &result) {
6060
_swift_stdlib_strto${cFuncSuffix2[bits]}_clocale(
6161
chars, UnsafeMutablePointer($0))
6262
}

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4777,7 +4777,7 @@ final internal class _Cocoa${Self}Iterator : IteratorProtocol {
47774777
let cocoa${Self} = self.cocoa${Self}
47784778
if itemIndex == itemCount {
47794779
let stackBufCount = _fastEnumerationStackBuf.count
4780-
// We can't use `withUnsafeMutablePointers` here to get pointers to
4780+
// We can't use `withUnsafeMutablePointer` here to get pointers to
47814781
// properties, because doing so might introduce a writeback buffer, but
47824782
// fast enumeration relies on the pointer identity of the enumeration
47834783
// state struct.

stdlib/public/core/LifetimeManager.swift

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -60,79 +60,38 @@ public func _fixLifetime<T>(_ x: T) {
6060
/// result. Useful for calling Objective-C APIs that take "in/out"
6161
/// parameters (and default-constructible "out" parameters) by pointer.
6262
public func withUnsafeMutablePointer<T, Result>(
63-
_ arg: inout T,
63+
to arg: inout T,
6464
_ body: @noescape (UnsafeMutablePointer<T>) throws -> Result
6565
) rethrows -> Result
6666
{
6767
return try body(UnsafeMutablePointer<T>(Builtin.addressof(&arg)))
6868
}
6969

70-
/// Like `withUnsafeMutablePointer`, but passes pointers to `arg0` and `arg1`.
71-
public func withUnsafeMutablePointers<A0, A1, Result>(
72-
_ arg0: inout A0,
73-
_ arg1: inout A1,
74-
_ body: @noescape (
75-
UnsafeMutablePointer<A0>, UnsafeMutablePointer<A1>) throws -> Result
76-
) rethrows -> Result {
77-
return try body(
78-
UnsafeMutablePointer<A0>(Builtin.addressof(&arg0)),
79-
UnsafeMutablePointer<A1>(Builtin.addressof(&arg1)))
80-
}
81-
82-
/// Like `withUnsafeMutablePointer`, but passes pointers to `arg0`, `arg1`,
83-
/// and `arg2`.
84-
public func withUnsafeMutablePointers<A0, A1, A2, Result>(
85-
_ arg0: inout A0,
86-
_ arg1: inout A1,
87-
_ arg2: inout A2,
88-
_ body: @noescape (
89-
UnsafeMutablePointer<A0>,
90-
UnsafeMutablePointer<A1>,
91-
UnsafeMutablePointer<A2>
92-
) throws -> Result
93-
) rethrows -> Result {
94-
return try body(
95-
UnsafeMutablePointer<A0>(Builtin.addressof(&arg0)),
96-
UnsafeMutablePointer<A1>(Builtin.addressof(&arg1)),
97-
UnsafeMutablePointer<A2>(Builtin.addressof(&arg2)))
98-
}
99-
10070
/// Invokes `body` with an `UnsafePointer` to `arg` and returns the
10171
/// result. Useful for calling Objective-C APIs that take "in/out"
10272
/// parameters (and default-constructible "out" parameters) by pointer.
10373
public func withUnsafePointer<T, Result>(
104-
_ arg: inout T,
74+
to arg: inout T,
10575
_ body: @noescape (UnsafePointer<T>) throws -> Result
10676
) rethrows -> Result
10777
{
10878
return try body(UnsafePointer<T>(Builtin.addressof(&arg)))
10979
}
11080

111-
/// Like `withUnsafePointer`, but passes pointers to `arg0` and `arg1`.
112-
public func withUnsafePointers<A0, A1, Result>(
113-
_ arg0: inout A0,
114-
_ arg1: inout A1,
115-
_ body: @noescape (UnsafePointer<A0>, UnsafePointer<A1>) throws -> Result
116-
) rethrows -> Result {
117-
return try body(
118-
UnsafePointer<A0>(Builtin.addressof(&arg0)),
119-
UnsafePointer<A1>(Builtin.addressof(&arg1)))
81+
@available(*, unavailable, renamed: "withUnsafeMutablePointer(to:_:)")
82+
public func withUnsafeMutablePointer<T, Result>(
83+
_ arg: inout T,
84+
_ body: @noescape (UnsafeMutablePointer<T>) throws -> Result
85+
) rethrows -> Result
86+
{
87+
Builtin.unreachable()
12088
}
12189

122-
/// Like `withUnsafePointer`, but passes pointers to `arg0`, `arg1`,
123-
/// and `arg2`.
124-
public func withUnsafePointers<A0, A1, A2, Result>(
125-
_ arg0: inout A0,
126-
_ arg1: inout A1,
127-
_ arg2: inout A2,
128-
_ body: @noescape (
129-
UnsafePointer<A0>,
130-
UnsafePointer<A1>,
131-
UnsafePointer<A2>
132-
) throws -> Result
133-
) rethrows -> Result {
134-
return try body(
135-
UnsafePointer<A0>(Builtin.addressof(&arg0)),
136-
UnsafePointer<A1>(Builtin.addressof(&arg1)),
137-
UnsafePointer<A2>(Builtin.addressof(&arg2)))
90+
@available(*, unavailable, renamed: "withUnsafePointer(to:_:)")
91+
public func withUnsafePointer<T, Result>(
92+
_ arg: inout T,
93+
_ body: @noescape (UnsafePointer<T>) throws -> Result
94+
) rethrows -> Result
95+
{
96+
Builtin.unreachable()
13897
}

stdlib/public/core/Runtime.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func _float${bits}ToString(_ value: Float${bits}, debug: Bool) -> String {
379379
_sanityCheck(sizeof(_Buffer72.self) == 72)
380380

381381
var buffer = _Buffer32()
382-
return withUnsafeMutablePointer(&buffer) {
382+
return withUnsafeMutablePointer(to: &buffer) {
383383
(bufferPtr) in
384384
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
385385
let actualLength = _float${bits}ToStringImpl(bufferUTF8Ptr, 32, value, debug)
@@ -408,7 +408,7 @@ func _int64ToString(
408408
) -> String {
409409
if radix >= 10 {
410410
var buffer = _Buffer32()
411-
return withUnsafeMutablePointer(&buffer) {
411+
return withUnsafeMutablePointer(to: &buffer) {
412412
(bufferPtr) in
413413
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
414414
let actualLength =
@@ -420,7 +420,7 @@ func _int64ToString(
420420
}
421421
} else {
422422
var buffer = _Buffer72()
423-
return withUnsafeMutablePointer(&buffer) {
423+
return withUnsafeMutablePointer(to: &buffer) {
424424
(bufferPtr) in
425425
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
426426
let actualLength =
@@ -445,7 +445,7 @@ func _uint64ToString(
445445
) -> String {
446446
if radix >= 10 {
447447
var buffer = _Buffer32()
448-
return withUnsafeMutablePointer(&buffer) {
448+
return withUnsafeMutablePointer(to: &buffer) {
449449
(bufferPtr) in
450450
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
451451
let actualLength =
@@ -457,7 +457,7 @@ func _uint64ToString(
457457
}
458458
} else {
459459
var buffer = _Buffer72()
460-
return withUnsafeMutablePointer(&buffer) {
460+
return withUnsafeMutablePointer(to: &buffer) {
461461
(bufferPtr) in
462462
let bufferUTF8Ptr = UnsafeMutablePointer<UTF8.CodeUnit>(bufferPtr)
463463
let actualLength =

stdlib/public/core/UnsafeRawPointer.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer {
405405
"storeBytes to misaligned raw pointer")
406406

407407
var temp = value
408-
withUnsafeMutablePointer(&temp) { source in
408+
withUnsafeMutablePointer(to: &temp) { source in
409409
let rawSrc = UnsafeMutableRawPointer(source)._rawValue
410410
// FIXME: to be replaced by _memcpy when conversions are implemented.
411411
Builtin.int_memcpy_RawPointer_RawPointer_Int64(

0 commit comments

Comments
 (0)