extension Span where Element: ~Copyable { public subscript(_ position: Int) -> Element { _read } }
Note that we use a
_read
accessor for the subscript, a requirement in order toyield
a borrowed non-copyableElement
(see "Coroutines".) This will be updated to a final syntax at a later time, understanding that we intend the replacement to be source-compatible.
This is not exactly correct. We now know that _read
accessor coroutines will not suffice for element access -- the final form of Span
's subscripts will need to use very different semantics than what is being promised here.
The elements of a span are guaranteed to exist as long as the span does. We'll need the subscript accessors to fully embrace this fact: the borrows of the elements that we produce through subscript access need to have precisely the same lifetime dependencies as the span itself.
(Read accessors are allowed to materialize their result on demand; so the borrows we get from them are necessarily tied to the specific access to the span itself. This is far too complicated and it results in unusably narrow lifetime dependencies. To be able to build useful abstractions around spans (and borrowing access in general), we need to model direct accesses as direct accesses.)
We've been assuming that a coroutine-based (or unsafeAddress
-based) Span
subscript will be source-compatible with the eventual borrowing accessor construct (which does not exist yet). However, this is just an assumption; it should be called out in the proposal, and it deserves critical review.