Skip to content

Add UnsafePointer extension #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Sources/OpenSwiftUICore/Util/UnsafePointer+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// UnsafePointer+Extension.swift
// OpenSwiftUICore
//
// Audited for RELEASE_2024
// Status: Complete

extension UnsafePointer {
package subscript() -> Pointee {
@_transparent
unsafeAddress {
self
}
}

@_transparent
package static var null: UnsafePointer<Pointee> {
UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ff00) | (-MemoryLayout<Pointee>.alignment))!
}
}

extension UnsafeMutablePointer {
package subscript() -> Pointee {
@_transparent
unsafeAddress { UnsafePointer(self) }
@_transparent
nonmutating unsafeMutableAddress { self }
}

@_transparent
package static var null: UnsafeMutablePointer<Pointee> {
UnsafeMutablePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ff00) | (-MemoryLayout<Pointee>.alignment))!
}
}

extension UnsafeBufferPointer {
@_transparent
package var startAddress: UnsafePointer<Element> {
baseAddress ?? .null
}
}


extension UnsafeMutableBufferPointer {
@_transparent
package var startAddress: UnsafeMutablePointer<Element> {
baseAddress ?? .null
}
}
17 changes: 17 additions & 0 deletions Tests/OpenSwiftUICoreTests/Util/UnsafePointer+ExtensionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UnsafePointer+ExtensionTests.swift
// OpenSwiftUICoreTests

import Testing
import OpenSwiftUICore

struct UnsafePointer_ExtensionTests {
@Test
func nullPointer() {
#expect(UnsafePointer<Int64>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_fff8)))
#expect(UnsafePointer<Int32>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_fffc)))
#expect(UnsafePointer<Int16>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_fffe)))
#expect(UnsafePointer<Int8>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ffff)))
#expect(UnsafePointer<Bool>.null == UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ffff)))
}
}
Loading