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

[SR-11087] Add a closure taking API: UnsafeRaw[Mutable][Buffer]Pointer.withMemoryRebound(to:[capacity:]) #53479

Closed
atrick opened this issue Jul 8, 2019 · 1 comment
Assignees
Labels
feature A feature request or implementation standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented

Comments

@atrick
Copy link
Member

atrick commented Jul 8, 2019

Previous ID SR-11087
Radar rdar://81800124
Original Reporter @atrick
Type New Feature
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels New Feature, LanguageFeatureRequest
Assignee @glessard
Priority Medium

md5: 21b1f3b49a4fdfac61bbe80110d519eb

Issue Description:

Specifically:

extension Unsafe[Mutable]RawPointer {
  public func withMemoryRebound<T, Result>(to type: T.Type, capacity count: Int,
    _ body: (UnsafePointer<T>) throws -> Result
  ) rethrows -> Result
  {...}
}

extension Unsafe[Mutable]RawBufferPointer {
  public func withMemoryRebound<T, Result>(
    to type: T.Type, _ body: (${Self}<T>) throws -> Result
  ) rethrows -> Result
  {...}
}

This API will allow code to temporarily view raw memory as a memory holding the specified type. Within the closure scope, it will be legal to access memory via a typed pointer of the rebound type.

This is both a standard library addition and SIL-level compiler feature.

Note that we still don't want higher-level APIs (e.g. Foundation Data) that already erased the pointer type to vend a typed pointer to the user in any way, unless we can guarantee that they have exclusive access to the memory. In other words, providing the user with a non-unique typed pointer into raw memory of unknown provenance is inherently unsafe and shouldn't be hidden behind a commonly used convenience API that implies safety.

Suggested design:

In SIL, add a return type to the bind_memory memory instruction. The returned $Builtin.Word would be a token representing the previously bound memory state. The token is not actually a type and can't be used for anything other than rebinding the memory. Add a new rebind_memory instruction that takes this token instead of a type parameter.

If the prior bound memory type is available via static analysis, then the rebind_memory instruction can be substituted with a normal bind_memory.

Note: Although deprecated, the Foundation Data APIs withUnsafeBytes and withUnsafeMutableBytes should be fixed to use

withMemoryRebound(to:)

instead of

assumingMemoryBound(to:)

.

@glessard
Copy link
Contributor

glessard commented Feb 4, 2022

Addressed by #40156 SE-0333 and #39529

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented and removed new feature labels Nov 11, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented
Projects
None yet
Development

No branches or pull requests

3 participants