Skip to content

Commit 1699c5a

Browse files
committed
Switch withSpan example over to the "storage" property currently being proposed
1 parent 7f9488e commit 1699c5a

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

visions/memory-safety.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ One of the primary places where this doesn’t hold is with low-level access to
8787
* **Lifetime safety**: the unsafe buffer pointer should only be used within the closure, but there is no checking to establish that the pointer does not escape the closure. If it does escape, it could be used after the closure has returned and the pointer could have effectively been “freed.”
8888
* **Bounds safety**: the unsafe buffer pointer types do not perform bounds checking in release builds.
8989

90-
[Non-escapable types](https://github.com/swiftlang/swift-evolution/pull/2304) provide the ability to create types whose instances cannot escape out of the context in which they were created with no runtime overhead. Non-escapable types allow the creation of a [memory-safe counterpart to the unsafe buffer types](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0447-span-access-shared-contiguous-storage.md), proposed under the name `Span` . With `Span` , it becomes possible to access contiguous memory in an array in a manner that maintains memory safety. For example:
90+
[Non-escapable types](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0446-non-escapable.md) provide the ability to create types whose instances cannot escape out of the context in which they were created with no runtime overhead. Non-escapable types allow the creation of a [memory-safe counterpart to the unsafe buffer types](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0447-span-access-shared-contiguous-storage.md), `Span` . With `Span` , it becomes possible to access contiguous memory in an array in a manner that maintains memory safety. For example:
9191

9292
```swift
93-
myInts.withSpan { span in
94-
globalSpan = span // error: span value cannot escape the closure
95-
print(span[myArray.count]) // runtime error: out-of-bounds access
96-
return span.first ?? 0
97-
}
93+
let span = myInts.storage
94+
95+
globalSpan = span // error: span value cannot escape the scope of myInts
96+
print(span[myArray.count]) // runtime error: out-of-bounds access
97+
print(span.first ?? 0)
9898
```
9999

100-
[Lifetime dependencies](https://github.com/swiftlang/swift-evolution/pull/2305) can greatly improve the expressiveness of non-escaping types, providing the ability to work with types like `Span` without requiring deeply-nested `with` blocks. Additionally, they make it possible to build more complex data structures out of non-escaping types, extending Swift’s capabilities while maintaining memory safety.
100+
[Lifetime dependencies](https://github.com/swiftlang/swift-evolution/pull/2305) can greatly improve the expressiveness of non-escaping types, making it possible to build more complex data structures while maintaining memory safety.
101101

102102
## Expressing memory-safe interfaces for the C family of languages
103103

0 commit comments

Comments
 (0)