Skip to content

Commit 03b896b

Browse files
charlieMonroelattner
authored andcommitted
Updated proposal: (#461)
- removing `withUnsafe[Mutable]Pointers` - keeping `withUnsafe[Mutable]Pointer` arg as `inout`
1 parent 44aa8b7 commit 03b896b

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

proposals/0127-cleaning-up-stdlib-ptr-buffer.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ The Swift standard library has been thoroughly updated to follow the new API gui
1919
the few places that need to be updated in pointer and buffer APIs:
2020

2121
- `withUnsafe[Mutable]Pointer`'s `arg` argument should have a `to:` label ([SR-1937](https://bugs.swift.org/browse/SR-1937))
22-
- `withUnsafePointer`'s `arg` argument should no longer be `inout` as it requires
23-
creation of temporary `var`s ([SR-1956](https://bugs.swift.org/browse/SR-1956)).
24-
- `unsafeAddressOf` should be removed since there is a limited number of use cases and there are better alternatives to it ([SR-1957](https://bugs.swift.org/browse/SR-1937)).
22+
- `withUnsafe[Mutable]Pointers` (multiple pointers) functions should be removed.
23+
- `unsafeAddressOf` should be removed since there is a limited number of use cases and there are
24+
better alternatives to it ([SR-1957](https://bugs.swift.org/browse/SR-1937)).
2525
- `ManagedProtoBuffer` should be removed. It is a superclass of `ManagedBuffer` and its
2626
sole purpose is to conceal the `header` property during invocation of the closure
2727
creating the initial header in `ManagedBuffer.create` since the `header` isn't
@@ -33,22 +33,25 @@ prevent something that should be considered programmer's error.
3333
`withUnsafe[Mutable]Pointer` methods will now include `to:` argument label:
3434

3535
```
36-
withUnsafePointer(to: x) { (ptr) -> Void in
36+
withUnsafePointer(to: &x) { (ptr) -> Void in
3737
// ...
3838
}
3939
```
4040

4141
---
4242

43-
Also, the non-mutable `withUnsafePointer`'s arguments will no longer be `inout`,
44-
allowing the following:
43+
The multiple-pointer variations of the methods (`withUnsafe[Mutable]Pointers`) should
44+
be removed since the use cases in which they can be used are very limited and their use can be
45+
easily worked around by using nested calls to the single-pointer variants:
4546

4647
```
47-
// This needs to be var in Swift 2.x
48-
let x = NSObject()
48+
var x = NSObject()
49+
var y = NSObject()
4950
50-
withUnsafePointer(to: x) { (ptr) -> Void in
51-
/// ...
51+
withUnsafePointer(to: &x) { (ptrX) -> Void in
52+
withUnsafePointer(to: &y) { (ptrY) -> Void in
53+
/// ...
54+
}
5255
}
5356
```
5457

@@ -72,8 +75,8 @@ will be moved onto `ManagedBuffer` instead.
7275
## Impact on existing code
7376

7477
`withUnsafe[Mutable]Pointer` usage will need to be updated to include the `to:` label
75-
and the non-mutable version will need to have the `&` reference removed since it will
76-
no longer be `inout`.
78+
and the multi-pointer versions will need to be removed by the user and nested calls to single-pointer
79+
variants need to be used instead.
7780

7881
Use of `unsafeAddressOf(x)` will need to be changed to `ObjectIdentifier(x).unsafeAddress`
7982
instead.
@@ -84,7 +87,16 @@ referenced in the code as an explicit type. Such occurrences can be renamed to
8487

8588
## Alternatives considered
8689

87-
- Keeping the argument of `withUnsafePointer` as `inout`.
90+
- `withUnsafePointer`'s argument is currently marked as `inout` which allows the function
91+
to provide the same address even for non-object values that are passed in as reference.
92+
This, however, may lead to unnecessary creation of `var` variables, instead of keeping
93+
them as immutable (`let`). Discussion on the mailing list brought up two suggestions:
94+
- eliminate `withUnsafePointer` altogether and only keep the mutable `withUnsafeMutablePointer`
95+
variant since it can be used instead of the immutable variant in all use cases. This change
96+
would, however, conceal the caller's intention of what is going to be done with the pointer.
97+
- The second suggestion was to introduce two variants of `withUnsafePointer` - one that maintains
98+
current behavior and one that that doesn't require `inout` argument. This has been viewed on as
99+
an additive change not in scope for Swift 3.
88100
- Remove `unsafeAddressOf` and use `Unmanaged.takeUnretainedValue(_:)` instead. This,
89101
however, requires the caller to deal with retain logic for something as simple as
90102
getting an object address.

0 commit comments

Comments
 (0)