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/XXXX-noncopyable-stdlib-primitives.md
+15-12Lines changed: 15 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1536,13 +1536,16 @@ When the array is destroyed, we need to properly deinitialize its elements and d
1536
1536
1537
1537
Note the use of the new `extracting` operation to get a buffer pointer that consists of just the slots that have been populated. We cannot call `_storage.deinitialize()` as it isn't necessarily fully initialized; and we also cannot use the classic slicing operation `_storage[..<count]`, as it would need to return a `Slice`, and that type doesn't support noncopyable elements.
1538
1538
1539
-
Hypoarrays can be declared sendable when their element type is sendable.
1539
+
Hypoarrays can be declared sendable when their element type is sendable.
We need to remember to suppress the copyability of `Element`. If we forgot that, then this conditional sendability would only apply if `Element` happened to be copyable.
1546
+
`Hypoarray` relies on unsafe pointer operations and dynamic memory allocation to implement its storage, so the compiler is not able to prove that it'll be correctly sendable. The `@unchecked` attribute acknowledges this and promises that the type is still following the rules of sendability.
1547
+
1548
+
We need to remember to suppress the copyability of `Element`. If we forgot that, then this conditional sendability would only apply if `Element` happened to be copyable.
1546
1549
1547
1550
Of course, an array needs to provide access to its contents, and it also needs operations to add and remove elements. The task of inventing variants of the `Sequence` and `Collection` protocols that allow noncopyable conforming types and element types is deferred to a subsequent proposal, but we can safely expect that even generalized array types will be based on the concept of an integer index, and that the existing indexing operations in `Collection` will largely translate into the noncopyable universe:
1548
1551
@@ -1587,7 +1590,7 @@ extension Hypoarray where Element: ~Copyable {
@@ -1639,13 +1642,13 @@ It would be desirable to allow iteration over `Hypoarray` instances. Unfortunate
1639
1642
// Example usage:
1640
1643
var array: Hypoarray<Int>=...
1641
1644
for i in array.startIndex..< array.endIndex { // a.k.a. 0 ..< array.count
1642
-
array.readElement(at: i) { print($0) }
1645
+
array.borrowElement(at: i) { print($0) }
1643
1646
}
1644
1647
```
1645
1648
1646
1649
Not having noncopyable container protocols also means that `Hypoarray` cannot conform to any, so subsequently it will not getany of the standard generic container algorithms for free: there is no `firstIndex(of:)`, there is no `map`, no `filter`, no slicing, no `sort`, no `reverse`. Indeed, many of these standard algorithms expect to work on `Equatable` or `Comparable` items, and those protocols are also yet to be generalized.
1647
1650
1648
-
Okay, so all we have is `readElement` and `modifyElement`, forborrowing and mutating access. What about consuming access, though?
1651
+
Okay, so all we have is `borrowElement` and `updateElement`, forborrowing and mutating access. What about consuming access, though?
1649
1652
1650
1653
Consuming an item of an array at a particular index would require either removing the item from the array, or destroying and discarding the rest of the array. Neither of these looks desirable as a primitive operation for accessing an element. However, we do expect arrays to provide a named operation for removing items, `remove(at:)`. This operation is easily implementable on `Hypoarray`:
1651
1654
@@ -1693,7 +1696,7 @@ We want insertions to have amortized O(1) complexity, so they need to be careful
0 commit comments