[SR-11087] Add a closure taking API: UnsafeRaw[Mutable][Buffer]Pointer.withMemoryRebound(to:[capacity:]) #53479
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
Additional Detail from JIRA
md5: 21b1f3b49a4fdfac61bbe80110d519eb
Issue Description:
Specifically:
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
andwithUnsafeMutableBytes
should be fixed to useinstead of
.
The text was updated successfully, but these errors were encountered: