[SR-11082] Support withMemoryRebound(to:) with mismatched sizes #53474
Labels
feature
A feature request or implementation
manual memory management
Area → standard library: Manual memory management APIs
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: 74cbbf4bb5d2a9863759a9e9d0cb9ec6
Issue Description:
We should fix both UnsafePointer and UnsafeBufferPointer to handle converting to a type with a different stride.
For UnsafePointer, document that the
capacity
argument pertains to the number of elements of theto
type (not thefrom
type).For UnsafeBufferPointer, the capacity is derived from the buffer capacity.
This can be done by adding checks to the stdlib implementation:
First check if the to/from strides are equal. If so, no additional code should be executed as a result of this change.
Next find the stride ratio. Either the
from
orto
type may be larger than the other, but the larger stride must be an exact multiple of the smaller.For UnsafeBufferPointer, call the first
bindMemory
with the adjusted capacity. ForUnsafePointer
the secondbindMemory
call's capacity changes.If the
to
type is larger, add an assertion that the buffer is aligned toto.align
.This merits a discussion on the swift-evolution list. But since it's only broadening the accepted input, I don't think it needs an SE proposal. The documentation does need to change.
This has been discussed before. I balked at supporting this right away because I wanted a zero-abstraction cost guarantee for all new memory binding APIs (relative to simple pointer conversion). But, given the code that I've seen since then, I think it's worth doing and just hoping compile always specializes the call.
The text was updated successfully, but these errors were encountered: