[swift-evolution] Pitch: Replace UnsafeMutablePointer by UnsafePointer in non-mutating APIs

Dave Abrahams dabrahams at apple.com
Tue Jul 19 17:32:33 CDT 2016


on Tue Jul 19 2016, Arnold Schwaighofer <swift-evolution at swift.org> wrote:

> # Replace `UnsafeMutablePointer` by `UnsafePointer` in non-mutating APIs
>
> * Proposal: [SE-0000](0000-change_unsafe_mutable_pointer_to_unsafe_pointer.md)
> * Author: [Arnold Schwaighofer](https://github.com/aschwaighofer)
> * Status: **Pitch**
> * Review manager: TBD
>
> ## Introduction
>
> `UnsafeMutablePointer` didn't always have an immutable variant, and when it was
> introduced there could have been many places that should have been changed to
> use it, but were not. `UnsafeMutablePointer` is still pervasive. We should
> survey the uses and make sure they're all using mutability only as appropriate.
>
> The only such occurrence I found was the `dprintf` API which should have a
> read-only format argument.

In that case you don't really need to go through evolution for this.
dprintf is effectively an API imported from POSIX in an overlay.  We
should just fix it.

>
>
> - Swift bug: [SR-1958] (https://bugs.swift.org/browse/SR-1958)
> - Branch with changes to stdlib:
> [unsafe_mutable_pointer_to_unsafe_pointer]
> (https://github.com/aschwaighofer/swift/tree/unsafe_mutable_pointer_to_unsafe_pointer)
>
> ## Motivation
>
> We should change uses of `UnsafeMutablePointer` with `UnsafePointer` to clearly
> express the intent of the API where appropriate.
>
> ## Proposed solution
>
> The proposed solution is to change `dprintf` API to take a `UnsafePointer`
> argument.
>
> ## Detailed design
>
> Change:
> ```swift
> public func dprintf(_ fd: Int, _ format: UnsafeMutablePointer<Int8>, _ args: CVarArg...) -> Int32
> ```
>
> To
> ```swift
> public func dprintf(_ fd: Int, _ format: UnsafePointer<Int8>, _ args: CVarArg...) -> Int32
> ```
> ## Impact on existing code
>
> Existing coersions to `UnsafePointer` from `UnsafeMutablePointer` will allow
> existing code to work.
>
> ## Alternatives considered
>
> Leave as is.
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-- 
Dave



More information about the swift-evolution mailing list