assumeSafeAppend

Assume that it is safe to append to this array. Appends made to this array after calling this function may append in place, even if the array was a slice of a larger array to begin with.

Use this only when it is certain there are no elements in use beyond the array in the memory block. If there are, those elements will be overwritten by appending to this array.

Warning: Calling this function, and then using references to data located after the given array results in undefined behavior.

ref nothrow @system
inout(T[])
assumeSafeAppend
(
T
)
(
auto ref inout(T[]) arr
)

Return Value

Type: inout(T[])

The input is returned.

Examples

1 int[] a = [1, 2, 3, 4];
2 
3 // Without assumeSafeAppend. Appending relocates.
4 int[] b = a [0 .. 3];
5 b ~= 5;
6 assert(a.ptr != b.ptr);
7 
8 debug(SENTINEL) {} else
9 {
10     // With assumeSafeAppend. Appending overwrites.
11     int[] c = a [0 .. 3];
12     c.assumeSafeAppend() ~= 5;
13     assert(a.ptr == c.ptr);
14 }
Suggestion Box / Bug Report