assumeUnique

Trivial wrapper for a cast from any array to an array of immutable elements (e.g. from any string to an immutable string), to make code more readable. Its use is only legal if no one else has a reference to the contents of the input array. Cf. std.exception.assumeUnique in Phobos.

  1. immutable(T)[] assumeUnique(T[] input)
    immutable(T)[]
    assumeUnique
    (
    T
    )
    (
    T[] input
    )
  2. immutable(T)[] assumeUnique(T[] input)

Parameters

input T[]

slice whose contents to cast to immutable; this reference will be nullified to prevent any further access from this mutable handle

Return Value

Type: immutable(T)[]

slice of immutable elements corresponding to the same segment of memory referred to by input

Note: D1 does not allow overloading on rvalue vs lvalue, nor does it have anything similar to D2's auto ref feature. At the same time, to match Phobos semantics we need to nullify the slice that gets cast to immutable. Because of this in D1 assumeUnique accepts only rvalues: use temporary local variables to assign lvalues if any need to be used with assumeUnique.

D2 programs can use lvalues as well as rvalues.

Meta

Credits

This function is copied from phobos std.exception.assumeUnique ((c) Andrei Alexandrescu, Boost license) with minor modifications.