From ae1751bb0ed50f83a035d3b23c5b0a080e20a5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Tue, 13 Feb 2024 21:25:14 +0100 Subject: [PATCH] Define proper copy semantics for RingBuffer. Previously did not behave well when copies were made: A RingBuffer copy that got destroyed would leave the other copies with an buffer full of destroyed elements. --- source/vibe/container/ringbuffer.d | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/source/vibe/container/ringbuffer.d b/source/vibe/container/ringbuffer.d index d539be6..0bd5535 100644 --- a/source/vibe/container/ringbuffer.d +++ b/source/vibe/container/ringbuffer.d @@ -24,6 +24,10 @@ module vibe.container.ringbuffer; Both, FIFO and LIFO operation modes are supported, using `removeFront` and `removeBack`. + + This struct has value semantics - copying the a `RingBuffer` value will copy + the whole buffer. Copy-on-write optimization may be implemented in the future, + but generally copying is discouraged. */ struct RingBuffer(T, size_t N = 0, bool INITIALIZE = true) { import std.traits : hasElaborateDestructor, isCopyable; @@ -43,6 +47,12 @@ struct RingBuffer(T, size_t N = 0, bool INITIALIZE = true) { /// Constructs a new rung buffer with given capacity (only if `N == 0`). this(size_t capacity) { m_buffer = new T[capacity]; } + this(this) + { + if (m_buffer.length) + m_buffer = m_buffer.dup; + } + ~this() { if (m_buffer.length > 0) {