Structure

Unsafe​Raw​Buffer​Pointer

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

Overview

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

An Unsafe​Raw​Buffer​Pointer 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 Unsafe​Raw​Buffer​Pointer instance also supports the load(from​Byte​Offset:​as:​) method provided by Unsafe​Raw​Pointer, 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 Unsafe​Raw​Buffer​Pointer instance is a view into memory and does not own the memory that it references. Copying a variable or constant of type Unsafe​Raw​Buffer​Pointer does not copy the underlying memory. However, initializing another collection with an Unsafe​Raw​Buffer​Pointer instance copies bytes out of the referenced memory and into the new collection.

The following example uses some​Bytes, an Unsafe​Raw​Buffer​Pointer 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 dest​Bytes creates a new, nonowning buffer pointer covering the first n bytes of the memory that some​Bytes references—nothing is copied:

var destBytes = someBytes[0..<n]

Next, the bytes referenced by dest​Bytes are copied into byte​Array, a new [UInt] array, and then the remainder of some​Bytes is appended to byte​Array:

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

Nested Types

Unsafe​Raw​Buffer​Pointer.Iterator

An iterator over the bytes viewed by a raw buffer pointer.

Symbols

Initializers

init<T>(Unsafe​Mutable​Buffer​Pointer<T>)

Creates a raw buffer over the contiguous bytes in the given typed buffer.

init<T>(Unsafe​Buffer​Pointer<T>)

Creates a raw buffer over the contiguous bytes in the given typed buffer.

init(Unsafe​Raw​Buffer​Pointer)

Creates a new buffer over the same memory as the given buffer.

init(Unsafe​Mutable​Raw​Buffer​Pointer)

Creates a new buffer over the same memory as the given buffer.

init(start:​ Unsafe​Raw​Pointer?, count:​ Int)

Creates a buffer over the specified number of contiguous bytes starting at the given pointer.

Instance Properties

var base​Address:​ Unsafe​Raw​Pointer?

A pointer to the first byte of the buffer.

var count:​ Int

The number of bytes in the buffer.

var count:​ Int

The number of elements in the collection.

var debug​Description:​ String

A textual representation of the buffer, suitable for debugging.

var end​Index:​ Int

The “past the end” position—that is, the position one greater than the last valid subscript argument.

var first:​ UInt8?

The first element of the collection.

var indices:​ Unsafe​Raw​Buffer​Pointer.Indices
var indices:​ Default​Random​Access​Indices<Unsafe​Raw​Buffer​Pointer>

The indices that are valid for subscripting the collection, in ascending order.

var indices:​ Countable​Range<Int>

The indices that are valid for subscripting the collection, in ascending order.

var is​Empty:​ Bool

A Boolean value indicating whether the collection is empty.

var last:​ UInt8?

The last element of the collection.

var lazy:​ Lazy​Bidirectional​Collection<Unsafe​Raw​Buffer​Pointer>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var lazy:​ Lazy​Sequence<Unsafe​Raw​Buffer​Pointer>

A sequence containing the same elements as this sequence, but on which some operations, such as map and filter, are implemented lazily.

var lazy:​ Lazy​Random​Access​Collection<Unsafe​Raw​Buffer​Pointer>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var lazy:​ Lazy​Collection<Unsafe​Raw​Buffer​Pointer>

A view onto this collection that provides lazy implementations of normally eager operations, such as map and filter.

var start​Index:​ Int

Always zero, which is the index of the first byte in a nonempty buffer.

var underestimated​Count:​ Int

Returns a value less than or equal to the number of elements in the sequence, nondestructively.

var underestimated​Count:​ Int

A value less than or equal to the number of elements in the collection.

Instance Methods

func contains(UInt8)

Returns a Boolean value indicating whether the sequence contains the given element.

func contains(where:​ (UInt8) -> Bool)

Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.

func deallocate()

Deallocates the memory viewed by this buffer pointer.

func distance(from:​ Int, to:​ Int)

Returns the distance between two indices.

func drop(while:​ (UInt8) -> Bool)

Returns a subsequence by skipping elements while predicate returns true and returning the remaining elements.

func drop​First()

Returns a subsequence containing all but the first element of the sequence.

func drop​First(Int)

Returns a subsequence containing all but the given number of initial elements.

func drop​Last()

Returns a subsequence containing all but the last element of the sequence.

func drop​Last(Int)

Returns a subsequence containing all but the specified number of final elements.

func elements​Equal<Other​Sequence>(Other​Sequence)

Returns a Boolean value indicating whether this sequence and another sequence contain the same elements in the same order.

func elements​Equal<Other​Sequence>(Other​Sequence, by:​ (UInt8, UInt8) -> Bool)

Returns a Boolean value indicating whether this sequence and another sequence contain equivalent elements, using the given predicate as the equivalence test.

func enumerated()

Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero, and x represents an element of the sequence.

func filter((UInt8) -> Bool)

Returns an array containing, in order, the elements of the sequence that satisfy the given predicate.

func first(where:​ (UInt8) -> Bool)

Returns the first element of the sequence that satisfies the given predicate.

func flat​Map<Segment​Of​Result>((UInt8) -> Segment​Of​Result)

Returns an array containing the concatenated results of calling the given transformation with each element of this sequence.

func flat​Map<Element​Of​Result>((UInt8) -> Element​Of​Result?)

Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

func for​Each((UInt8) -> Void)

Calls the given closure on each element in the sequence in the same order as a for-in loop.

func index(Int, offset​By:​ Int)

Returns an index that is the specified distance from the given index.

func index(after:​ Int)

Returns the position immediately after the given index.

func index(before:​ Int)

Returns the position immediately after the given index.

func index(of:​ UInt8)

Returns the first index where the specified value appears in the collection.

func index(where:​ (UInt8) -> Bool)

Returns the first index in which an element of the collection satisfies the given predicate.

func lexicographically​Precedes<Other​Sequence>(Other​Sequence)

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<) to compare elements.

func lexicographically​Precedes<Other​Sequence>(Other​Sequence, by:​ (UInt8, UInt8) -> Bool)

Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the given predicate to compare elements.

func load<T>(from​Byte​Offset:​ Int, as:​ T.Type)

Returns a new instance of the given type, read from the buffer pointer’s raw memory at the specified byte offset.

func make​Iterator()

Returns an iterator over the bytes of this sequence.

func map<T>((UInt8) -> T)

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func map<T>((UInt8) -> T)

Returns an array containing the results of mapping the given closure over the sequence’s elements.

func max()

Returns the maximum element in the sequence.

func max(by:​ (UInt8, UInt8) -> Bool)

Returns the maximum element in the sequence, using the given predicate as the comparison between elements.

func min()

Returns the minimum element in the sequence.

func min(by:​ (UInt8, UInt8) -> Bool)

Returns the minimum element in the sequence, using the given predicate as the comparison between elements.

func pop​First()

Removes and returns the first element of the collection.

func pop​Last()

Removes and returns the last element of the collection.

func prefix(Int)

Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.

func prefix(through:​ Int)

Returns a subsequence from the start of the collection through the specified position.

func prefix(up​To:​ Int)

Returns a subsequence from the start of the collection up to, but not including, the specified position.

func prefix(while:​ (UInt8) -> Bool)

Returns a subsequence containing the initial elements until predicate returns false and skipping the remaining elements.

func reduce<Result>(Result, (Result, UInt8) -> Result)

Returns the result of combining the elements of the sequence using the given closure.

func remove​First()

Removes and returns the first element of the collection.

func remove​First(Int)

Removes the specified number of elements from the beginning of the collection.

func remove​Last()

Removes and returns the last element of the collection.

func remove​Last(Int)

Removes the given number of elements from the end of the collection.

func reversed()

Returns a view presenting the elements of the collection in reverse order.

func sorted()

Returns the elements of the sequence, sorted.

func sorted(by:​ (UInt8, UInt8) -> Bool)

Returns the elements of the sequence, sorted using the given predicate as the comparison between elements.

func split(max​Splits:​ Int, omitting​Empty​Subsequences:​ Bool, where​Separator:​ (UInt8) -> Bool)

Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.

func split(separator:​ UInt8, max​Splits:​ Int, omitting​Empty​Subsequences:​ Bool)

Returns the longest possible subsequences of the collection, in order, around elements equal to the given element.

func starts<Possible​Prefix>(with:​ Possible​Prefix)

Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence.

func starts<Possible​Prefix>(with:​ Possible​Prefix, by:​ (UInt8, UInt8) -> Bool)

Returns a Boolean value indicating whether the initial elements of the sequence are equivalent to the elements in another sequence, using the given predicate as the equivalence test.

func suffix(Int)

Returns a subsequence, up to the given maximum length, containing the final elements of the collection.

func suffix(from:​ Int)

Returns a subsequence from the specified position to the end of the collection.

Subscripts

subscript(Int)

Accesses the byte at the given offset in the memory region as a UInt8 value.

subscript(Range<Int>)

Accesses the bytes in the specified memory region.

See Also

Related Symbols

Unsafe​Raw​Pointer, Unsafe​Buffer​Pointer