You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/0127-cleaning-up-stdlib-ptr-buffer.md
+25-13Lines changed: 25 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -19,9 +19,9 @@ The Swift standard library has been thoroughly updated to follow the new API gui
19
19
the few places that need to be updated in pointer and buffer APIs:
20
20
21
21
-`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)).
25
25
-`ManagedProtoBuffer` should be removed. It is a superclass of `ManagedBuffer` and its
26
26
sole purpose is to conceal the `header` property during invocation of the closure
27
27
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.
33
33
`withUnsafe[Mutable]Pointer` methods will now include `to:` argument label:
34
34
35
35
```
36
-
withUnsafePointer(to: x) { (ptr) -> Void in
36
+
withUnsafePointer(to: &x) { (ptr) -> Void in
37
37
// ...
38
38
}
39
39
```
40
40
41
41
---
42
42
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:
45
46
46
47
```
47
-
// This needs to be var in Swift 2.x
48
-
let x = NSObject()
48
+
var x = NSObject()
49
+
var y = NSObject()
49
50
50
-
withUnsafePointer(to: x) { (ptr) -> Void in
51
-
/// ...
51
+
withUnsafePointer(to: &x) { (ptrX) -> Void in
52
+
withUnsafePointer(to: &y) { (ptrY) -> Void in
53
+
/// ...
54
+
}
52
55
}
53
56
```
54
57
@@ -72,8 +75,8 @@ will be moved onto `ManagedBuffer` instead.
72
75
## Impact on existing code
73
76
74
77
`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.
77
80
78
81
Use of `unsafeAddressOf(x)` will need to be changed to `ObjectIdentifier(x).unsafeAddress`
79
82
instead.
@@ -84,7 +87,16 @@ referenced in the code as an explicit type. Such occurrences can be renamed to
84
87
85
88
## Alternatives considered
86
89
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.
88
100
- Remove `unsafeAddressOf` and use `Unmanaged.takeUnretainedValue(_:)` instead. This,
89
101
however, requires the caller to deal with retain logic for something as simple as
0 commit comments