SE-0138: Add UnsafeRawBufferPointer and UnsafeMutableRawBufferPointer. #4954
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.
https://github.com/apple/swift-evolution/blob/master/proposals/0138-unsaferawbufferpointer.md
Unsafe[Mutable]RawBufferPointer is a non-owning view over a region of memory as
a Collection of bytes independent of the type of values held in that
memory. Each 8-bit byte in memory is viewed as a
UInt8
value.Reads and writes on memory via
Unsafe[Mutable]RawBufferPointer
are untypedoperations. Accessing this Collection's bytes does not bind the
underlying memory to
UInt8
. The underlying memory must be boundto some trivial type whenever it is accessed via a typed operation.
In addition to the
Collection
interface, the following methods fromUnsafe[Mutable]RawPointer
's interface to raw memory areprovided with debug mode bounds checks:
load(fromByteOffset:as:)
,storeBytes(of:toByteOffset:as:)
, andcopyBytes(from:count:)
.This is only a view into memory and does not own the memory. Copying a value of
type
UnsafeMutableRawBufferPointer
does not copy the underlyingmemory. Assigning an
Unsafe[Mutable]RawBufferPointer
into a value-basedcollection, such as
[UInt8]
copies bytes out of memory. Assigning into asubscript range of
UnsafeMutableRawBufferPointer
copies into memory.