[swift-evolution] [Draft] UnsafeRawPointer API

Andrew Trick atrick at apple.com
Mon Jun 27 18:53:41 CDT 2016


> On Jun 23, 2016, at 6:40 PM, Andrew Trick via swift-evolution <swift-evolution at swift.org> wrote:
> 
> https://github.com/atrick/swift-evolution/blob/voidpointer/proposals/XXXX-unsaferawpointer.md

Would anyone like to bikeshed the allocation API? Here are two options with a slight stylistic difference:

# Option 1:

extension UnsafeMutableRawPointer {
    init<T>(allocatingCapacity: Int, of: T.Type)

    func deallocate<T>(capacity: Int, of: T.Type)
}

let ptrToA = UnsafeMutableRawPointer(allocatingCapacity: 1, of: A.self)
  .initialize(A.self, with: A())
ptrToA.deinitialize(count: 1).deallocate(capacity: 1, of: A.self)

# Option 2:

extension UnsafeMutableRawPointer {
    static allocate<T>(capacity: Int, of: T.Type) -> UnsafeMutableRawPointer

    func deallocate<T>(capacity: Int, of: T.Type)
}

let ptrToA = UnsafeMutableRawPointer.allocate(capacity: 1, of: A.self)
  .initialize(A.self, with: A())
ptrToA.deinitialize(count: 1).deallocate(capacity: 1, of: A.self)

-Andy


More information about the swift-evolution mailing list