Contents

init(capacity:initializingWith:)

Creates a data instance with the specified capacity, and then calls the given closure with an output span covering the instance’s uninitialized memory.

Declaration

init<E>(capacity: Int, initializingWith initializer: @_lifetime(span: copy span) (inout OutputSpan<UInt8>) throws(E) -> Void) throws(E) where E : Error

Parameters

  • capacity:

    The number of bytes to allocate space for in the new Data.

  • initializer:

    A closure to initialize the allocated memory.

    • Parameters:

      • span: An OutputSpan covering uninitialized memory with space for the specified number of elements.

Discussion

Inside the closure, initialize elements by appending to the OutputSpan. The OutputSpan keeps track of the initialized memory, ensuring safety. Its count at the end of the closure will become the count of the newly-initialized instance of Data.