-
Notifications
You must be signed in to change notification settings - Fork 214
Proposal to generalize closure-based functions for Data #1649
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
Open
glessard
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
glessard:data+generalized-with-unsafe-bytes-proposal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
Proposals/nnnn-generalize-closure-based-functions-in-Data.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Generalize closure-based functions of `Data` | ||
|
|
||
| * Proposal: [SF-NNNN](nnnn-generalize-closure-based-functions-in-Data.md) | ||
| * Authors: [Guillaume Lessard](https://github.com/glessard) | ||
| * Review Manager: TBD | ||
| * Status: **Awaiting implementation** or **Awaiting review** | ||
| * Bug: *if applicable* [swiftlang/swift-foundation#1638](https://github.com/swiftlang/swift-foundation/issues/1638) | ||
|
|
||
| * Implementation: [swiftlang/swift-foundation#1622](https://github.com/swiftlang/swift-foundation/pull/1622) | ||
|
|
||
| * Review: ([pitch](https://forums.swift.org/...)) | ||
|
|
||
| ## Introduction | ||
|
|
||
| We propose to generalize the closure-taking API of `Data` for typed throws and for noncopyable return values, making them able to accept a greater variety of closures. | ||
|
|
||
| ## Motivation | ||
|
|
||
| Since [SE-0427](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0427-noncopyable-generics.md), noncopyable types can participate in Swift generics, but `Data` has not been adapted to allow working with noncopyable values. [SE-0437](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0437-noncopyable-stdlib-primitives.md) paved the way by generalizing low-level constructs such as `UnsafeBufferPointer<T>`, and as a result the community of Swift systems programmers now expects that generic functions such as `withUnsafeBytes()` can return noncopyable values. | ||
|
|
||
| In [SE-0413](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0413-typed-throws.md), we also added the ability for functions to throw typed errors. This ability is important in high-performance contexts such as embedded Swift, and functions such as `withUnsafeBytes()` should support closures with typed errors. | ||
|
|
||
| `Data`'s current closure-based functions such as `withUnsafeBytes()` cannot take advantage of either of these newer features: | ||
|
|
||
| ```swift | ||
| extension Data { | ||
| public func withUnsafeBytes<ResultType>( | ||
| _ apply: (UnsafeRawBufferPointer) throws -> ResultType | ||
| ) rethrows -> ResultType | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| ## Proposed solution | ||
|
|
||
| We will add support for typed throws and noncopyable return values to the functions of `Data` that take closure arguments with generic types. | ||
|
|
||
| ## Detailed design | ||
|
|
||
| The signatures of `Data`'s three closure-based functions will become: | ||
|
|
||
| ```swift | ||
| extension Data { | ||
| @_alwaysEmitIntoClient | ||
| public func withUnsafeBytes<E: Error, ResultType: ~Copyable>( | ||
| _ apply: (UnsafeRawBufferPointer) throws(E) -> ResultType | ||
| ) throws(E) -> ResultType | ||
|
|
||
| @_alwaysEmitIntoClient | ||
| public func withContiguousStorageIfAvailable<E: Error, ResultType: ~Copyable>( | ||
glessard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ body: (_ buffer: UnsafeBufferPointer<UInt8>) throws(E) -> ResultType | ||
| ) throws(E) -> ResultType? | ||
|
|
||
| @_alwaysEmitIntoClient | ||
| public mutating func withUnsafeMutableBytes<E: Error, ResultType: ~Copyable>( | ||
glessard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ body: (UnsafeMutableRawBufferPointer) throws(E) -> ResultType | ||
| ) throws(E) -> ResultType | ||
| } | ||
| ``` | ||
|
|
||
| These are new functions that replace existing functions in a source-compatible way. The older signature required `ResultType` type to be `Copyable`, allowing it to also satisfy the `~Copyable` constraint. The untyped errors thrown by existing closures are a special case of the typed error case, where `E == any Error`. | ||
|
|
||
| ## Source compatibility | ||
|
|
||
| This change is source-compatible with existing call sites. | ||
|
|
||
| ## Implications on adoption | ||
|
|
||
| On ABI-stable platforms, the existing ABI will be preserved. The new entry points will be declared `@_alwaysEmitIntoClient` in order to help the compiler specialize use sites in practice. | ||
|
|
||
| ## Alternatives considered | ||
|
|
||
| (none) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.