You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-9720 Foundation Data API: add Data.copyBytes methods taking UnsafeRawBufferPointer.
Issue Description:
Data's current API's that interoperate with unsafe pointers are awkward, and misleading. They should be updated now that we have UnsafeRawBufferPointer (SE-0183).
I want to clarify the most important change proposed in this bug:
I also suggest that we deprecate this existing API:
public func withUnsafeBytes<ResultType, ContentType>
And reintroduce it as:
public func withUnsafePointer<ResultType, ContentType>
Deprecating the existing `withUnsafeBytes` in favor of one that passes
an UnsafeRawBufferPointer to its closure is the most important thing
to change. However, it should not simply be renamed to
`withUnsafePointer`. It should also take a range argument and pass a
buffer pointer to the closure like this:
funcwithUnsafePointer<Result, ContentType>(
inrange: Range<Int>,
apply: (UnsafeBufferPointer<ContentType>) throws -> Result
) rethrows -> Result
Additionally, even in its new form, this API is incompatibile with
`bytesNoCopy`. The problem is that `Data` has no knowledge of the
types stored in its memory. It simply references "raw" bytes. If the
client passes existing memory into Data via `bytesNoCopy` then that
memory could presumably be accessed by client code as any type. It
would be quite natural for the user to use `Data.withUnsafePointer` as
an effective pointer cast, but doing so would result in undefined
behavior. For example:
If we do still want to provide a `withUnsafePointer` API, then we
should also verify that the Data object has ownership of its memory
and was not constructed using `bytesNoCopy`. Then calling
`withUnsafePointer` could bind Data's memory to the requested
type. That would remain the memory's type until the next call to
`withUnsafePointer`.
Additional Detail from JIRA
md5: 47a2b35ea104f50c0451e7d810cc42b6
cloned to:
Issue Description:
Data's current API's that interoperate with unsafe pointers are awkward, and misleading. They should be updated now that we have UnsafeRawBufferPointer (SE-0183).
Prototype:
atrick/swift@796b08f
I also suggest that we deprecate this existing API:
And reintroduce it as:
It’s only purpose is to make it more convenient to call a C API passing a non-void pointer into Data.
If the C api takes void* and length, it would actually be safer to use the newly proposed:
because that doesn’t require knowledge of the type of values in memory.
9/9/16, 9:35 PM Andrew Trick:
As mentioned on the review thread, we should also deprecate these:
The reason for deprecating them is that the current versions allow buffer overflow. They do not actually bind memory, so that isn’t the problem.
9/9/16, 9:45 PM Andrew Trick:
This is the method that unnecessarily binds memory to UInt8:
We should deprecate it and add a safer overload:
The text was updated successfully, but these errors were encountered: