Contents

withTemporaryAllocation(of:capacity:_:)

Provides scoped access to an output span of the specified type and capacity.

Declaration

func withTemporaryAllocation<T, R, E>(of type: T.Type, capacity: Int, _ body: @_lifetime(0: copy 0) (inout OutputSpan<T>) throws(E) -> R) throws(E) -> R where E : Error, T : ~Copyable, R : ~Copyable

Parameters

  • type:

    The type of the elements in the buffer being temporarily allocated.

  • capacity:

    The capacity of the output span being temporarily allocated.

  • body:

    A closure to invoke and to which the allocated output span should be passed.

Return Value

Whatever is returned by body.

Discussion

This function is useful for cheaply allocating storage for a sequence of values for a brief duration. Storage may be allocated on the heap or on the stack, depending on the required size and alignment.

When body is called, it is passed an empty OutputSpan. body may append or initialize elements in the output span. Any elements that have been initialized when body returns are deinitialized automatically, and deallocation is also automatic.

See Also

Memory Access