Skip to content
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

[SR-131] Initializers for converting UnsafePointers to integers #42753

Closed
gribozavr opened this issue Dec 8, 2015 · 4 comments
Closed

[SR-131] Initializers for converting UnsafePointers to integers #42753

gribozavr opened this issue Dec 8, 2015 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. feature A feature request or implementation good first issue Good for newcomers standard library Area: Standard library umbrella

Comments

@gribozavr
Copy link
Collaborator

Previous ID SR-131
Radar None
Original Reporter @gribozavr
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, NewAPIRequest, StarterBug
Assignee Michael Buckley (JIRA)
Priority Medium

md5: 451e88b78c9a1f88b46a37ad8c5fa990

Issue Description:

The standard library has bitPattern initializers on pointers. But we are missing initializers to create integers from pointers.

Someone needs to propose these APIs, walk them through swift-evolution, write a patch for the library and add tests.

extension UInt {
  init<T>(bitPattern: UnsafePointer<T>) {
    self = UInt(Builtin.ptrtoint_Word(bitPattern._rawValue))
  }

  init<T>(bitPattern: UnsafeMutablePointer<T>) {
    self = UInt(Builtin.ptrtoint_Word(bitPattern._rawValue))
  }
}

extension Int {
  init<T>(bitPattern: UnsafePointer<T>) {
    self = Int(Builtin.ptrtoint_Word(bitPattern._rawValue))
  }

  init<T>(bitPattern: UnsafeMutablePointer<T>) {
    self = Int(Builtin.ptrtoint_Word(bitPattern._rawValue))
  }
}
@belkadan
Copy link
Contributor

belkadan commented Dec 8, 2015

Either this or add a bitPattern member to UnsafePointer and UnsafeMutablePointer. (The proposal should anticipate this question and explain why its preferred choice is better.)

@gribozavr
Copy link
Collaborator Author

The rationale is simple: type conversions are implemented as initializers where possible.

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 8, 2015

Comment by Michael Buckley (JIRA)

I'm assigning this to myself so it doesn't show as unassigned. There are currently some questions on swift-dev about what the motivations for this change are, and whether they can be achieved in a better way.

@swift-ci
Copy link
Collaborator

Comment by Michael Buckley (JIRA)

Closing, as this was completed a few months ago.

5501ec7

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation and removed API request labels Jan 27, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. feature A feature request or implementation good first issue Good for newcomers standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants