Structure

UnsafeMutableRawBufferPointer

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

Overview

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

An UnsafeMutableRawBufferPointer 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 and writing to memory through a raw buffer are untyped operations. Accessing this collection’s bytes does not bind the underlying memory to UInt8.

In addition to its collection interface, an UnsafeMutableRawBufferPointer instance also supports the following methods provided by UnsafeMutableRawPointer, including bounds checks in debug mode:

  • load(fromByteOffset:as:)

  • storeBytes(of:toByteOffset:as:)

  • copyBytes(from:count:)

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

UnsafeMutableRawBufferPointer Semantics

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

The following example uses someBytes, an UnsafeMutableRawBufferPointer 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 [UInt] array, and then the remainder of someBytes is appended to byteArray:

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

Assigning into a ranged subscript of an UnsafeMutableRawBufferPointer instance copies bytes into the memory. The next n bytes of the memory that someBytes references are copied in this code:

destBytes[0..<n] = someBytes[n..<(n + n)]

Topics

Initializers

init<T>(UnsafeMutableBufferPointer<T>)

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

init(UnsafeMutableRawBufferPointer)

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

init(mutating: UnsafeRawBufferPointer)

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

init(rebasing: MutableRandomAccessSlice<UnsafeMutableRawBufferPointer>)

Creates a raw buffer over the same memory as the given raw buffer slice.

Beta
init(start: UnsafeMutableRawPointer?, count: Int)

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

Instance Properties

var baseAddress: UnsafeMutableRawPointer?

A pointer to the first byte of the buffer.

var count: Int

The number of bytes in the buffer.

Beta
var count: Int

The number of elements in the collection.

Beta
var debugDescription: String

A textual representation of the buffer, suitable for debugging.

var endIndex: 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: DefaultRandomAccessIndices<UnsafeMutableRawBufferPointer>

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

var indices: CountableRange<Int>

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

var isEmpty: Bool

A Boolean value indicating whether the collection is empty.

var last: UInt8?

The last element of the collection.

var lazy: LazyBidirectionalCollection<UnsafeMutableRawBufferPointer>

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

var lazy: LazyCollection<UnsafeMutableRawBufferPointer>

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

var lazy: LazySequence<UnsafeMutableRawBufferPointer>

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

var lazy: LazyRandomAccessCollection<UnsafeMutableRawBufferPointer>

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

var startIndex: Int

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

var underestimatedCount: Int

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

Beta
var underestimatedCount: Int

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

Beta

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 copyBytes<C>(from: C)

Copies from a collection of UInt8 into this buffer’s memory.

func copyBytes(from: UnsafeRawBufferPointer)

Copies the specified number of bytes from the given raw pointer’s memory into this buffer’s memory.

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 dropFirst()

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

func dropFirst(Int)

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

func dropLast()

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

func dropLast(Int)

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

func elementsEqual<OtherSequence>(OtherSequence)

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

func elementsEqual<OtherSequence>(OtherSequence, 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 forEach((UInt8) -> Void)

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

func index(Int, offsetBy: 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(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 lexicographicallyPrecedes<OtherSequence>(OtherSequence)

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 lexicographicallyPrecedes<OtherSequence>(OtherSequence, 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>(fromByteOffset: 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 makeIterator()

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.

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

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

Beta
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 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(upTo: 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 reversed()

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

func sort(by: (UInt8, UInt8) -> Bool)

Sorts the collection in place, using the given predicate as the comparison between elements.

func sorted()

Returns the elements of the collection, sorted.

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

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

func split(maxSplits: Int, omittingEmptySubsequences: Bool, whereSeparator: (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, maxSplits: Int, omittingEmptySubsequences: Bool)

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

func starts<PossiblePrefix>(with: PossiblePrefix)

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

func starts<PossiblePrefix>(with: PossiblePrefix, 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 storeBytes<T>(of: T, toByteOffset: Int, as: T.Type)

Stores a value’s bytes into the buffer pointer’s raw memory at the specified byte offset.

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.

func swapAt(Int, Int)

Exchanges the values at the specified indices of the collection.

Beta

Type Methods

static func allocate(count: Int)

Returns a newly allocated buffer with the given size, in bytes.

Subscripts

subscript(Int)

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

subscript(Range<Int>)

Accesses a contiguous subrange of the collection’s elements.

Beta
subscript(Range<Int>)

Accesses the bytes in the specified memory region.

Beta

Structures

struct UnsafeMutableRawBufferPointer.Iterator

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

See Also

Raw Pointers

struct UnsafeRawPointer

A raw pointer for accessing untyped data.

struct UnsafeMutableRawPointer

A raw pointer for accessing and manipulating untyped data.

struct UnsafeRawBufferPointer

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software