[swift-evolution] Proposal: Add Initializers For Converting UnsafePointers to Int and Unit

Brent Royal-Gordon brent at architechies.com
Wed Dec 9 14:56:05 CST 2015


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

I was helping someone in my NSCoder Night group struggle with UnsafePointer just last night. He was working with MIDIMetaEvent, a C struct with a variable-sized buffer at the end of it. There was just no good way to handle the allocation and use of this type; I ended up writing an extension full of bizarre magic just so he could access and use it..

Anyway, all this is to say I would have used these initializers if they’d existed, and the alignment stuff you’re suggesting probably wouldn’t have cut the mustard for some of what I was trying to do.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list