[swift-dev] `withUnsafePointer` mutates `self`

Joe Groff jgroff at apple.com
Wed Dec 16 14:02:02 CST 2015


> On Dec 16, 2015, at 11:59 AM, Kevin Ballard <kevin at sb.org> wrote:
> 
> On Wed, Dec 16, 2015, at 11:49 AM, Joe Groff wrote:
>>  
>>> On Dec 16, 2015, at 11:47 AM, Kevin Ballard <kevin at sb.org <mailto:kevin at sb.org>> wrote:
>>>  
>>> On Wed, Dec 16, 2015, at 11:42 AM, Joe Groff wrote:
>>>>  
>>>>> On Dec 16, 2015, at 11:24 AM, Kevin Ballard via swift-dev <swift-dev at swift.org <mailto:swift-dev at swift.org>> wrote:
>>>>>  
>>>>> On Wed, Dec 16, 2015, at 12:12 AM, Dave Abrahams wrote:
>>>>>>  
>>>>>>> Come to think of it, what's the actual use-case for withUnsafePointer()?
>>>>>>  
>>>>>> I'm not sure we still have one that isn't covered by &x; that's my point.
>>>>>>  
>>>>>>> If a value is mutable, you can already use &x or withUnsafeMutablePointer(), and if it's immutable, you can't call withUnsafePointer() today anyway. The proposed change would just make withUnsafePointer() into the equivalent of `var x = value; callSomethingWith(&x)`. The only reason to really want a withUnsafePointer() function is if it can give you an UnsafePointer to an immutable value without copying it, but we can't do that. I'm inclined to say we should just get rid of withUnsafePointer() entirely, at least until such time as Swift has a way to pass immutable values by-ref.
>>>>>>  
>>>>>> I'm inclined to agree.  Proposal?
>>>>>  
>>>>> Sure, I'll write one up. I suspect that withUnsafePointer() / withUnsafeMutablePointer() are likely to be rarely used today, and most uses can probably be trivially replaced with just passing a &x ref, so this shouldn't be a big deal.
>>>>  
>>>> We can't remove withUnsafe[Mutable]Pointer; as I mentioned to Dave, it's necessary to persist a pointer for more than one immediate call.
>>>  
>>> Rare cases like that can be covered by either declaring a nested function taking the pointer and calling it, calling an anonymous local closure of the right type, or even using withExtendedLifetime(&x) { (ptr: UnsafePointer<T>) in ... }.
>>  
>> These are all just withUnsafePointer with different spelling.
>  
> That's true, but they all have the benefit of not requiring extra stdlib functions. swiftdoc.org lists 36 global functions, a full 4 of which are withUnsafePointer + variants. The existence of the functions also implies that they're necessary to work with pointers (especially the documentation). My suspicion is that nearly all current uses of these functions can be replaced with &x refs with no change in functionality. Even the stdlib only has a handful of uses of withUnsafeMutablePointer (and none at all for withUnsafePointer), and looking through them quickly, it looks like only the ones in public/core/Runtime.swift.gyb require a workaround.
>  

The standard library isn't a representative sample, since it doesn't interact directly with that much C code. I would check out Github for more information; I've had a number of discussions with developers who were doing things that are only possible with withUnsafePointer.

-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20151216/761b1166/attachment.html>


More information about the swift-dev mailing list