[swift-evolution] [RFC] UnsafeBytePointer API for In-Memory Layout

Joe Groff jgroff at apple.com
Mon May 9 16:23:01 CDT 2016


> On May 9, 2016, at 2:18 PM, Andrew Trick <atrick at apple.com> wrote:
> 
>> 
>> On May 9, 2016, at 1:20 PM, Joe Groff <jgroff at apple.com> wrote:
>> 
>> Regarding the UnsafeBytePointer API:
>> 
>>> struct UnsafeBytePointer : Hashable
>>> , _Pointer {
>>> 
>>>   
>>> let _rawValue: Builtin.
>>> RawPointer
>>> 
>>>   
>>> var hashValue: Int {...
>>> }
>>> 
>>>   
>>> init<T>(_ : UnsafePointer<T>
>>> )
>>>   
>>> init<T>(_ : UnsafeMutablePointer<T>
>>> )
>>>   
>>> init?<T>(_ : UnsafePointer<T>
>>> ?)
>>>   
>>> init?<T>(_ : UnsafeMutablePointer<T>
>>> ?)
>>> 
>>>   
>>> init<T>(_ : OpaquePointer<T>
>>> )
>>>   
>>> init?<T>(_ : OpaquePointer<T>
>>> ?)
>>> 
>>>   
>>> init?(bitPattern: Int
>>> )
>>>   
>>> init?(bitPattern: UInt
>>> )
>>> 
>>>   
>>> func load<T>(_ : T.Type) ->
>>>  T
>>> 
>>>   
>>> @warn_unused_result
>>> 
>>>   
>>> init(allocatingBytes size: Int, alignedTo: Int
>>> )
>>> 
>>>   
>>> @warn_unused_result
>>> 
>>>   
>>> init<T>(allocatingCapacity count: Int, of: T.Type
>>> )
>>> 
>>>   
>>> func deallocateBytes(_ size: Int, alignedTo: Int
>>> )
>>> 
>>>   
>>> func deallocateCapacity<T>(_ num: Int, of: T.Type
>>> )
>>> 
>>>   
>>> // Returns a pointer one byte after the initialized memory.
>>> 
>>>   
>>> func initialize<T>(with newValue: T, count: Int = 1) ->
>>>  UnsafeBytePointer
>>> 
>>>   
>>> // Returns a pointer one byte after the initialized memory.
>>> 
>>>   
>>> func initialize<T>(from: UnsafePointer<T>, count: Int) ->
>>>  UnsafeBytePointer
>>> 
>>>   
>>> func initializeBackward<T>(from source: UnsafePointer<T>, count: Int
>>> )
>>> 
>>>   
>>> func deinitialize<T>(_ : T.Type, count: Int = 1
>>> )
>>> }
>>> 
>> 
>> Should we also have 'assign' methods, matching 'initialize'? Should 'deinitialize' be called 'destroy', matching 'UnsafeMutablePointer's API?
> 
> I was wondering if anyone would ask for ‘assign’. It presumes that you are storing the same type of object that was previously stored in your buffer. I didn’t want to proactively support that case because it’s a convenience and not really consistent with the pointer being type punned. You can always call deinitialize() first if you need to before calling ‘initialize'.

I see. I guess it makes sense that, once you've initialized as the new type, you can cast to UMP and type-safe-ly reassign via that interface.

>  I  used ‘deinitialize’ to be consistent with UnsafeMutablePointer.

My mistake, I hadn't noticed that 'destroy' was renamed there too.

-Joe


More information about the swift-evolution mailing list