Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe Access to Contiguous Storage #2307

Open
wants to merge 71 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
8093cad
[draft proposal] Safe Access to Contiguous Storage
glessard Feb 6, 2024
548b250
edit placeholder proposal url
glessard Feb 6, 2024
7cfd46d
link to pitch thread
glessard Feb 6, 2024
9993694
declare typealiases correctly
glessard Feb 9, 2024
fece228
add “first” and “last” properties
glessard Feb 9, 2024
a3784fe
fix inits from raw pointers
glessard Feb 9, 2024
ceb261d
Update proposals/nnnn-safe-shared-contiguous-storage.md
glessard Feb 14, 2024
2480ca2
add `view(as: T)`
glessard Feb 14, 2024
441a5c8
incorporate feedback from pitch discussion
glessard Feb 15, 2024
b360a50
enclose index and iterator types in the main type
glessard Feb 17, 2024
9aa96ea
update protocol declaration
glessard Feb 23, 2024
1adba6d
link to additional related pitches
glessard Feb 24, 2024
300591d
fix a stored property type
glessard Apr 16, 2024
ed5fea2
rename type, adopt new syntax
glessard Apr 17, 2024
7b2bb1f
various updates
glessard Apr 19, 2024
f876043
add more RawSpan API, doc-comment fixes
glessard Apr 21, 2024
844e661
Added more prose, added TODOs for further clarification
milseman Apr 22, 2024
15de4ab
Update proposals/nnnn-safe-shared-contiguous-storage.md
glessard Apr 22, 2024
be180f1
remove some trailing whitespace from code blocks
glessard Apr 23, 2024
26a7637
Update
milseman May 6, 2024
50a38df
Update
glessard May 24, 2024
6add19b
lots of updates
glessard Jun 20, 2024
5d19ead
Apply suggestions from code review
glessard Jun 20, 2024
d46c815
Move byte parsing helpers into a future direction
milseman Jun 20, 2024
d87f041
Fill out the index appendix
milseman Jun 20, 2024
a5239b4
tweaks and corrections
glessard Jun 21, 2024
99f305a
add missing keywords
glessard Jun 21, 2024
b3db4b4
Apply editing suggestions from review
glessard Jun 21, 2024
d728217
annotation adjustments, various edits
glessard Jun 22, 2024
a0d3b87
some more edits
glessard Jun 22, 2024
90890a5
move `ContiguousStorage` to future directions
glessard Jun 22, 2024
2d463ab
edits about unsafe initializer usage
glessard Jun 25, 2024
c8b2d5c
remove “generally” from index-sharing note
glessard Jun 25, 2024
859a071
improve index validation functions
glessard Jun 25, 2024
e924bab
omit some duplicated documentation
glessard Jun 25, 2024
1844c97
add html anchors to important sections
glessard Jun 25, 2024
385cccb
add link to second pitch thread
glessard Jun 26, 2024
5266e65
more cleanup surrounding `ContiguousStorage`
glessard Jun 28, 2024
913f6e1
whitespace fixes
glessard Jun 28, 2024
ba482d9
Change some uses of the word “view” to “span” instead
glessard Jun 30, 2024
9370b13
fix misspelling
glessard Jun 30, 2024
572a236
add missing doc-comment paragraph
glessard Jun 30, 2024
c9c312c
change `uncheckedBounds` to `unchecked`
glessard Jun 30, 2024
42170bf
fix doc-comments
glessard Jun 30, 2024
1319b1d
rework `load` and company
glessard Jun 30, 2024
66bcb19
add the `SurjectiveBitPattern` future direction
glessard Jul 1, 2024
f84aefc
more about `SurjectiveBitPattern`, plus an alternative
glessard Jul 1, 2024
4b13bcd
move reference to SE-0256 to the ContiguousStorage item
glessard Jul 1, 2024
7a88571
reword coroutine accessors
glessard Jul 3, 2024
a183439
remove undesirable annotations and default values
glessard Jul 16, 2024
0a12619
add containment utilities
glessard Jul 16, 2024
3c9ef51
Apply suggestions from code review
glessard Jul 17, 2024
4496c54
remove extension to `Character.UTF8View`
glessard Aug 14, 2024
66a78f8
add closure-taking api, move initializers to future
glessard Aug 16, 2024
32adc87
shrink byte-parsing helpers future direction
glessard Aug 17, 2024
3d45c07
formatting, text moved around
glessard Aug 19, 2024
54912a7
rename file to include the word “span”
glessard Aug 22, 2024
8a26a9d
improve title
glessard Aug 22, 2024
c7e464a
add link to preview implementation
glessard Aug 30, 2024
9f32a16
lots of changes
glessard Sep 4, 2024
979f5bb
remove UBP.withUnsafeSpan and similar
glessard Sep 5, 2024
0235837
remove another ~Escapable that cannot be promised
glessard Sep 5, 2024
d656ead
add a missing blurb
glessard Sep 6, 2024
13f30ce
improve name of bounds-checking functions
glessard Sep 6, 2024
b7e1933
addition about closure-based unsafe escape-hatch functions
glessard Sep 6, 2024
2255c15
remove boundsPrecondition, add boundsContain overload
glessard Sep 7, 2024
92b2e2f
start pointer clarification
glessard Sep 9, 2024
3b12717
improve coroutine explanation
glessard Sep 9, 2024
38c6840
convert non-breaking spaces
glessard Sep 6, 2024
1279d70
fix extensions
glessard Sep 10, 2024
e18eafe
[feedback] mention initializers earlier
glessard Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
edits about unsafe initializer usage
  • Loading branch information
glessard committed Jun 25, 2024
commit 2d463ab216d3f41bf87955054d66682e5c98be57
30 changes: 11 additions & 19 deletions proposals/nnnn-safe-shared-contiguous-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,30 +156,22 @@ extension Slice: ContiguousStorage where Base: ContiguousStorage {
}
```

In addition to the the safe types above gaining the `storage` property, the `UnsafeBufferPointer` family of types will also gain access to a `storage` property. This enables interoperability of `Span`-taking API. While a `Span` binding created from an `UnsafeBufferPointer` exists, the memory that underlies it must not be deinitialized or deallocated.
#### Using `Span` with C functions or other unsafe code:

The `UnsafeBufferPointer` family of types can be be adapted for use with `Span`-taking API by using unsafe `Span` initializers. A `Span` instance obtained this way loses a static guarantee of temporal safety, because it is possible to deinitialize or deallocate the source `UnsafeMutableBufferPointer` before the end of the `Span` instance's scope.

```swift
extension UnsafeBufferPointer {
// note: additional preconditions apply until the end of the scope
var storage: Span<Element> { get }
}
extension UnsafeMutableBufferPointer {
// note: additional preconditions apply until the end of the scope
var storage: Span<Element> { get }
}
extension UnsafeRawBufferPointer {
// note: additional preconditions apply until the end of the scope
var storage: Span<UInt8> { get }
extension HypotheticalBase64Decoder {
public func decode(bytes: Span<UInt8>) -> [UInt8]
}
extension UnsafeMutableRawBufferPointer {
// note: additional preconditions apply until the end of the scope
var storage: Span<UInt8> { get }

data.withUnsafeBytes { (buffer: UnsafeRawBufferPointer) in
let span = Span<UInt8>(unsafeBytes: buffer, owner: buffer)
let decoded = myBase64Decoder.decode(span)
}
```

glessard marked this conversation as resolved.
Show resolved Hide resolved
#### Using `Span` with C functions or other unsafe code:

`Span` has an unsafe hatch for use with unsafe code.
`Span` has an unsafe hatch for use with functions that take an unsafe argument:

```swift
extension Span where Element: ~Copyable & ~Escapable {
Expand All @@ -206,7 +198,7 @@ public struct Span<Element: ~Copyable & ~Escapable>: Copyable, ~Escapable {

##### Creating a `Span`:

The initialization of a `Span` instance from an unsafe pointer is an unsafe operation. When it is initialized correctly, subsequent uses of the borrowed instance are safe. Typically these initializers will be used internally to a container's implementation of functions or computed properties that return a borrowed `Span`.
The initialization of a `Span` instance from an unsafe pointer is an unsafe operation. Typically these initializers will be used internally to a container's implementation and return a borrowed `Span` tied to the container's lifetime. Safe usage relies on a guarantee that the represented storage is managed correctly and outlives the `Span` instance.

```swift
extension Span where Element: ~Copyable & ~Escapable {
Expand Down