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

Commit

Permalink
Fix 11761: aa.byKey and aa.byValue are not forward ranges.
Browse files Browse the repository at this point in the history
The problem is that Phobos' `isForwardRange` template requires that
`.save` must be `@property`, otherwise it does not acknowledge the range
as a forward range; however, aa.byKey and aa.byValue's `.save` weren't
marked as such. This PR fixes this problem and introduces a unittest to
ensure the forward range criteria are satisfied.
  • Loading branch information
H. S. Teoh committed Aug 14, 2014
1 parent 3ed62d6 commit 36736e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/object.di
Expand Up @@ -448,7 +448,7 @@ auto byKey(T : Value[Key], Value, Key)(T aa) pure nothrow @nogc
@property bool empty() { return _aaRangeEmpty(r); }
@property ref Key front() { return *cast(Key*)_aaRangeFrontKey(r); }
void popFront() { _aaRangePopFront(r); }
Result save() { return this; }
@property Result save() { return this; }
}

return Result(_aaRange(cast(void*)aa));
Expand All @@ -469,7 +469,7 @@ auto byValue(T : Value[Key], Value, Key)(T aa) pure nothrow @nogc
@property bool empty() { return _aaRangeEmpty(r); }
@property ref Value front() { return *cast(Value*)_aaRangeFrontValue(r); }
void popFront() { _aaRangePopFront(r); }
Result save() { return this; }
@property Result save() { return this; }
}

return Result(_aaRange(cast(void*)aa));
Expand Down
38 changes: 36 additions & 2 deletions src/object_.d
Expand Up @@ -2044,7 +2044,7 @@ auto byKey(T : Value[Key], Value, Key)(T aa) pure nothrow @nogc
@property bool empty() { return _aaRangeEmpty(r); }
@property ref Key front() { return *cast(Key*)_aaRangeFrontKey(r); }
void popFront() { _aaRangePopFront(r); }
Result save() { return this; }
@property Result save() { return this; }
}

return Result(_aaRange(cast(void*)aa));
Expand All @@ -2065,7 +2065,7 @@ auto byValue(T : Value[Key], Value, Key)(T aa) pure nothrow @nogc
@property bool empty() { return _aaRangeEmpty(r); }
@property ref Value front() { return *cast(Value*)_aaRangeFrontValue(r); }
void popFront() { _aaRangePopFront(r); }
Result save() { return this; }
@property Result save() { return this; }
}

return Result(_aaRange(cast(void*)aa));
Expand Down Expand Up @@ -2302,6 +2302,40 @@ pure nothrow unittest
map.rehash;
}

pure nothrow unittest
{
// bug 11761: test forward range functionality
auto aa = ["a": 1];

{
auto keys = aa.byKey;
assert(!keys.empty);
assert(keys.front == "a");
static assert(is(typeof(keys.save) == typeof(keys)));
auto saved = keys.save;
keys.popFront();
assert(keys.empty);
assert(!saved.empty);
assert(saved.front == "a");
saved.popFront();
assert(saved.empty);
}

{
auto values = aa.byValue;
assert(!values.empty);
assert(values.front == 1);
static assert(is(typeof(values.save) == typeof(values)));
auto saved = values.save;
values.popFront();
assert(values.empty);
assert(!saved.empty);
assert(saved.front == 1);
saved.popFront();
assert(saved.empty);
}
}

deprecated("Please use destroy instead of clear.")
alias destroy clear;

Expand Down

0 comments on commit 36736e4

Please sign in to comment.