[swift-dev] Starter project: Initializers for converting UnsafePointers to integers

Dmitri Gribenko gribozavr at gmail.com
Mon Dec 7 18:45:02 CST 2015


Hi everyone,

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))
  }
}

https://bugs.swift.org/browse/SR-131

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-dev mailing list