From 903c38d7bb4e6ecad86f75ba444737de91fd8078 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Mon, 20 Nov 2017 13:40:37 +0100 Subject: [PATCH] Issue 17127 - bad example code for std.concurrency.Generator --- std/concurrency.d | 51 ++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/std/concurrency.d b/std/concurrency.d index 0e1b505bcdd..45a272601e7 100644 --- a/std/concurrency.d +++ b/std/concurrency.d @@ -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 @@ -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.