Skip to content

Commit 4272ed4

Browse files
committed
Merge pull request #2238 from jrose-apple/init-bitPattern-OpaquePointer
[stdlib] Add Int.init(bitPattern: OpaquePointer?) This is effectively an amendment to SE-0016; see the pull request for more discussion.
2 parents 74e035e + 09000e9 commit 4272ed4

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

stdlib/public/core/CTypes.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ extension OpaquePointer : CustomDebugStringConvertible {
162162
}
163163
}
164164

165+
extension Int {
166+
@warn_unused_result
167+
public init(bitPattern pointer: OpaquePointer?) {
168+
self.init(bitPattern: UnsafePointer<Void>(pointer))
169+
}
170+
}
171+
172+
extension UInt {
173+
@warn_unused_result
174+
public init(bitPattern pointer: OpaquePointer?) {
175+
self.init(bitPattern: UnsafePointer<Void>(pointer))
176+
}
177+
}
178+
165179
@warn_unused_result
166180
public func ==(lhs: OpaquePointer, rhs: OpaquePointer) -> Bool {
167181
return Bool(Builtin.cmp_eq_RawPointer(lhs._rawValue, rhs._rawValue))

stdlib/public/core/Runtime.swift.gyb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ func _stdlib_atomicCompareExchangeStrongPtrImpl(
2828
expected: UnsafeMutablePointer<OpaquePointer?>,
2929
desired: OpaquePointer?) -> Bool {
3030

31-
// Bit-cast directly from 'OpaquePointer?' to 'Builtin.Word' because
32-
// Builtin.RawPointer can't be nil.
31+
// We use Builtin.Word here because Builtin.RawPointer can't be nil.
3332
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Word(
3433
target._rawValue,
35-
unsafeBitCast(expected.pointee, to: Builtin.Word.self),
36-
unsafeBitCast(desired, to: Builtin.Word.self))
34+
UInt(bitPattern: expected.pointee)._builtinWordValue,
35+
UInt(bitPattern: desired)._builtinWordValue)
3736
expected.pointee = OpaquePointer(bitPattern: Int(oldValue))
3837
return Bool(won)
3938
}

test/1_stdlib/UnsafePointer.swift.gyb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ ${SelfName}TestSuite.test("Hashable") {
151151
}
152152
}
153153

154-
% if SelfName != 'OpaquePointer':
155154
${SelfName}TestSuite.test("toInteger") {
156155
do {
157156
let word: Int = 0x12345678
@@ -172,7 +171,6 @@ ${SelfName}TestSuite.test("toInteger") {
172171
expectEqual(UInt(0), UInt(bitPattern: ptr))
173172
}
174173
}
175-
% end
176174

177175
% end
178176

0 commit comments

Comments
 (0)