Skip to content

Commit

Permalink
takeOne for non-forward ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkuu committed Dec 24, 2014
1 parent 98088bb commit 4031a11
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion std/range/package.d
Expand Up @@ -1949,7 +1949,10 @@ auto takeOne(R)(R source) if (isInputRange!R)
@property auto ref front() { assert(!empty); return _source.front; }
void popFront() { assert(!empty); _empty = true; }
void popBack() { assert(!empty); _empty = true; }
@property auto save() { return Result(_source.save, empty); }
static if (isForwardRange!(Unqual!R))
{
@property auto save() { return Result(_source.save, empty); }
}
@property auto ref back() { assert(!empty); return _source.front; }
@property size_t length() const { return !empty; }
alias opDollar = length;
Expand Down Expand Up @@ -1984,6 +1987,21 @@ auto takeOne(R)(R source) if (isInputRange!R)
assert(s.empty);
}

unittest
{
struct NonForwardRange
{
enum empty = false;
int front() { return 42; }
void popFront() {}
}

static assert(!isForwardRange!NonForwardRange);

auto s = takeOne(NonForwardRange());
assert(s.front == 42);
}

/++
Returns an empty range which is statically known to be empty and is
guaranteed to have $(D length) and be random access regardless of $(D R)'s
Expand Down

0 comments on commit 4031a11

Please sign in to comment.