Contents

initializeMemory(as:repeating:)

Initializes the memory referenced by this buffer slice with the given value, binds the memory to the value’s type, and returns a typed buffer of the initialized memory.

Declaration

@discardableResult func initializeMemory<T>(as type: T.Type, repeating repeatedValue: T) -> UnsafeMutableBufferPointer<T>

Parameters

  • type:

    The type to bind this buffer’s memory to.

  • repeatedValue:

    The instance to copy into memory.

Return Value

A typed buffer of the memory referenced by this raw buffer. The typed buffer contains self.count / MemoryLayout<T>.stride instances of T.

Discussion

The memory referenced by this buffer slice must be uninitialized or initialized to a trivial type, and must be properly aligned for accessing T.

After calling this method on a raw buffer slice referencing memory starting at b = base.baseAddress + startIndex, the region starting at b and continuing up to b + self.count - self.count % MemoryLayout<T>.stride is bound to type T and is initialized. If T is a nontrivial type, you must eventually deinitialize or move the values in this region to avoid leaks. If base.baseAddress is nil, this function does nothing and returns an empty buffer pointer.