StructureSwift

UnsafeRawBufferPointer

A nonowning collection interface to the bytes in a region of memory.

@frozen struct UnsafeRawBufferPointer

Overview

You can use an UnsafeRawBufferPointer instance in low-level operations to eliminate uniqueness checks and release mode bounds checks. Bounds checks are always performed in debug mode.

An UnsafeRawBufferPointer instance is a view of the raw bytes in a region of memory. Each byte in memory is viewed as a UInt8 value independent of the type of values held in that memory. Reading from memory through a raw buffer is an untyped operation.

In addition to its collection interface, an UnsafeRawBufferPointer instance also supports the load(fromByteOffset:as:) method provided by UnsafeRawPointer, including bounds checks in debug mode.

To access the underlying memory through typed operations, the memory must be bound to a trivial type.

UnsafeRawBufferPointer Semantics

An UnsafeRawBufferPointer instance is a view into memory and does not own the memory that it references. Copying a variable or constant of type UnsafeRawBufferPointer does not copy the underlying memory. However, initializing another collection with an UnsafeRawBufferPointer instance copies bytes out of the referenced memory and into the new collection.

The following example uses someBytes, an UnsafeRawBufferPointer instance, to demonstrate the difference between assigning a buffer pointer and using a buffer pointer as the source for another collection’s elements. Here, the assignment to destBytes creates a new, nonowning buffer pointer covering the first n bytes of the memory that someBytes references—nothing is copied:

var destBytes = someBytes[0..<n]

Next, the bytes referenced by destBytes are copied into byteArray, a new [UInt8] array, and then the remainder of someBytes is appended to byteArray:

var byteArray: [UInt8] = Array(destBytes)
byteArray += someBytes[n..<someBytes.count]

Members

Typealiases

  • typealias Element

    A type representing the sequence’s elements.

  • typealias Index

    A type that represents a position in the collection.

  • typealias Indices

    A type that represents the indices that are valid for subscripting the collection, in ascending order.

  • typealias SubSequence

    A collection representing a contiguous subrange of this collection’s elements. The subsequence shares indices with the original collection.

Initializers

Instance Subscripts

Instance Properties

Instance Methods

Structures

Conforms To

Removed Members

Instance Methods