Skip to content
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
1 change: 1 addition & 0 deletions Sources/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(swiftCore
Int32.swift
Integers.swift
Never.swift
OpaquePointer.swift
Optional.swift
OptionSet.swift
Policy.swift
Expand Down
53 changes: 53 additions & 0 deletions Sources/Core/OpaquePointer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright © 2022 Max Desiatov <max@desiatov.com>.
// All Rights Reserved.
// SPDX-License-Identifier: BSD-3

// This `OpaquePointer` implementation is known to crash 5.4 and 5.5 compiler releases on Windows.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, that is a good idea to record!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatter I use has done a stupid (in my opinion) thing by indenting everything within #if block. I've pushed one more time to fix that.

#if swift(>=5.6)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newlines after this would've been nice, but eh.


@frozen
public struct OpaquePointer {
@usableFromInline
internal var _rawValue: Builtin.RawPointer

@usableFromInline @_transparent
internal init(_ v: Builtin.RawPointer) {
_rawValue = v
}

@_transparent
public init?(bitPattern: Int) {
if bitPattern == 0 { return nil }
_rawValue = Builtin.inttoptr_Word(bitPattern._value)
}

@_transparent
public init?(bitPattern: UInt) {
if bitPattern == 0 { return nil }
_rawValue = Builtin.inttoptr_Word(bitPattern._value)
}

@_transparent
public init<T>(@_nonEphemeral _ from: UnsafePointer<T>) {
_rawValue = from._rawValue
}

@_transparent
public init?<T>(@_nonEphemeral _ from: UnsafePointer<T>?) {
guard let unwrapped = from else { return nil }
self.init(unwrapped)
}

@_transparent
public init<T>(@_nonEphemeral _ from: UnsafeMutablePointer<T>) {
_rawValue = from._rawValue
}

@_transparent
public init?<T>(@_nonEphemeral _ from: UnsafeMutablePointer<T>?) {
guard let unwrapped = from else { return nil }
self.init(unwrapped)
}
}

#endif