From 857be9459e63998f424c4f5655be9407487d1b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Tue, 22 Jan 2019 10:49:57 +0100 Subject: [PATCH] Improve convenience of the Channel API. - allows all methods to be called on a `shared(Channel!T)` instance. - `Channel` pre-defines the `buffer_size` argument to 100, matching `createChannel` --- source/vibe/core/channel.d | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/source/vibe/core/channel.d b/source/vibe/core/channel.d index 9295cbc..b699811 100644 --- a/source/vibe/core/channel.d +++ b/source/vibe/core/channel.d @@ -34,7 +34,7 @@ Channel!(T, buffer_size) createChannel(T, size_t buffer_size = 100)() The implementation supports multiple-reader-multiple-writer operation across multiple tasks in multiple threads. */ -struct Channel(T, size_t buffer_size) { +struct Channel(T, size_t buffer_size = 100) { enum bufferSize = buffer_size; private shared ChannelImpl!(T, buffer_size) m_impl; @@ -51,6 +51,8 @@ struct Channel(T, size_t buffer_size) { `tryConsumeOne` in a multiple-reader scenario instead. */ @property bool empty() { return m_impl.empty; } + /// ditto + @property bool empty() shared { return m_impl.empty; } /** Closes the channel. @@ -59,6 +61,8 @@ struct Channel(T, size_t buffer_size) { been consumed. */ void close() { m_impl.close(); } + /// ditto + void close() shared { m_impl.close(); } /** Consumes a single element off the queue. @@ -66,6 +70,8 @@ struct Channel(T, size_t buffer_size) { property is `true`, an exception will be thrown. */ T consumeOne() { return m_impl.consumeOne(); } + /// ditto + T consumeOne() shared { return m_impl.consumeOne(); } /** Attempts to consume a single element. @@ -73,6 +79,8 @@ struct Channel(T, size_t buffer_size) { `false` is returned and `dst` is left untouched. */ bool tryConsumeOne(ref T dst) { return m_impl.tryConsumeOne(dst); } + /// ditto + bool tryConsumeOne(ref T dst) shared { return m_impl.tryConsumeOne(dst); } /** Attempts to consume all elements currently in the queue. @@ -86,12 +94,18 @@ struct Channel(T, size_t buffer_size) { bool consumeAll(ref FixedRingBuffer!(T, buffer_size) dst) in { assert(dst.empty); } body { return m_impl.consumeAll(dst); } + /// ditto + bool consumeAll(ref FixedRingBuffer!(T, buffer_size) dst) shared + in { assert(dst.empty); } + body { return m_impl.consumeAll(dst); } /** Enqueues an element. This function may block the the event that the internal buffer is full. */ void put(T item) { m_impl.put(item.move); } + /// ditto + void put(T item) shared { m_impl.put(item.move); } } @@ -235,3 +249,12 @@ unittest { // test basic operation and non-copyable struct compatiblity assert(ch.empty); assert(!ch.tryConsumeOne(v)); } + +unittest { // make sure shared(Channel!T) can also be used + shared ch = createChannel!int; + ch.put(1); + assert(!ch.empty); + assert(ch.consumeOne == 1); + ch.close(); + assert(ch.empty); +}