Skip to content

Commit

Permalink
Issue 17127 - bad example code for std.concurrency.Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Mar 31, 2018
1 parent ed16238 commit 903c38d
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions std/concurrency.d
Expand Up @@ -1515,35 +1515,6 @@ private interface IsGenerator {}
/**
* A Generator is a Fiber that periodically returns values of type T to the
* caller via yield. This is represented as an InputRange.
*
* Example:
* ---
* import std.concurrency;
* import std.stdio;
*
*
* void main()
* {
* auto tid = spawn(
* {
* while (true)
* {
* writeln(receiveOnly!int());
* }
* });
*
* auto r = new Generator!int(
* {
* foreach (i; 1 .. 10)
* yield(i);
* });
*
* foreach (e; r)
* {
* tid.send(e);
* }
* }
* ---
*/
class Generator(T) :
Fiber, IsGenerator, InputRange!T
Expand Down Expand Up @@ -1727,6 +1698,28 @@ private:
T* m_value;
}

///
@system unittest
{
auto tid = spawn({
size_t i;
while (i < 9)
i = receiveOnly!size_t;

ownerTid.send(i * 2);
});

auto r = new Generator!int({
foreach (i; 1 .. 10)
yield(i);
});

foreach (e; r)
tid.send(e);

assert(receiveOnly!size_t == 18);
}

/**
* Yields a value of type T to the caller of the currently executing
* generator.
Expand Down

0 comments on commit 903c38d

Please sign in to comment.