Skip to content

Commit 70fe2d9

Browse files
authored
Add UnsafePointer extension (#139)
1 parent 1129537 commit 70fe2d9

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// UnsafePointer+Extension.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for RELEASE_2024
6+
// Status: Complete
7+
8+
extension UnsafePointer {
9+
package subscript() -> Pointee {
10+
@_transparent
11+
unsafeAddress {
12+
self
13+
}
14+
}
15+
16+
@_transparent
17+
package static var null: UnsafePointer<Pointee> {
18+
UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ff00) | (-MemoryLayout<Pointee>.alignment))!
19+
}
20+
}
21+
22+
extension UnsafeMutablePointer {
23+
package subscript() -> Pointee {
24+
@_transparent
25+
unsafeAddress { UnsafePointer(self) }
26+
@_transparent
27+
nonmutating unsafeMutableAddress { self }
28+
}
29+
30+
@_transparent
31+
package static var null: UnsafeMutablePointer<Pointee> {
32+
UnsafeMutablePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ff00) | (-MemoryLayout<Pointee>.alignment))!
33+
}
34+
}
35+
36+
extension UnsafeBufferPointer {
37+
@_transparent
38+
package var startAddress: UnsafePointer<Element> {
39+
baseAddress ?? .null
40+
}
41+
}
42+
43+
44+
extension UnsafeMutableBufferPointer {
45+
@_transparent
46+
package var startAddress: UnsafeMutablePointer<Element> {
47+
baseAddress ?? .null
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// UnsafePointer+ExtensionTests.swift
3+
// OpenSwiftUICoreTests
4+
5+
import Testing
6+
import OpenSwiftUICore
7+
8+
struct UnsafePointer_ExtensionTests {
9+
@Test
10+
func nullPointer() {
11+
#expect(UnsafePointer<Int64>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_fff8)))
12+
#expect(UnsafePointer<Int32>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_fffc)))
13+
#expect(UnsafePointer<Int16>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_fffe)))
14+
#expect(UnsafePointer<Int8>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ffff)))
15+
#expect(UnsafePointer<Bool>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ffff)))
16+
}
17+
}

0 commit comments

Comments
 (0)