Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
use auto ref for assumeSafeAppend
Browse files Browse the repository at this point in the history
  • Loading branch information
monarchdodra committed Jul 8, 2013
1 parent 0152ea8 commit 91ffcb9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
6 changes: 1 addition & 5 deletions src/object.di
Expand Up @@ -646,15 +646,11 @@ size_t reserve(T)(ref T[] arr, size_t newcapacity) pure nothrow @trusted
return _d_arraysetcapacity(typeid(T[]), newcapacity, cast(void *)&arr);
}

ref T[] assumeSafeAppend(T)(ref T[] arr)
auto ref T[] assumeSafeAppend(T)(auto ref T[] arr)
{
_d_arrayshrinkfit(typeid(T[]), *(cast(void[]*)&arr));
return arr;
}
T[] assumeSafeAppend(T)(T[] arr)
{
return assumeSafeAppend(arr);
}

bool _ArrayEq(T1, T2)(T1[] a1, T2[] a2)
{
Expand Down
29 changes: 23 additions & 6 deletions src/object_.d
Expand Up @@ -2601,17 +2601,15 @@ unittest
*
* Calling this function, and then using references to data located after the
* given array results in undefined behavior.
*
* Returns:
* The input is returned.
*/
ref T[] assumeSafeAppend(T)(ref T[] arr)
auto ref T[] assumeSafeAppend(T)(auto ref T[] arr)
{
_d_arrayshrinkfit(typeid(T[]), *(cast(void[]*)&arr));
return arr;
}
/// ditto
T[] assumeSafeAppend(T)(T[] arr)
{
return assumeSafeAppend(arr);
}
///
unittest
{
Expand Down Expand Up @@ -2644,6 +2642,25 @@ unittest
assert(ptr == arr.ptr);
}

unittest
{
int[] arr = [1, 2, 3];
void foo(ref int[] i)
{
i ~= 5;
}
arr = arr[0 .. 2];
foo(assumeSafeAppend(arr)); //pass by ref
assert(arr[]==[1, 2, 5]);
arr = arr[0 .. 1].assumeSafeAppend(); //pass by value
}

//@@@10574@@@
version(none)unittest
{
immutable(int[]) arr;
assumeSafeAppend(arr); //IFTI failure here. Should work. Please fix me.
}

version (none)
{
Expand Down

0 comments on commit 91ffcb9

Please sign in to comment.