# HG changeset patch # User Kim Alvefur # Date 1639653405 -3600 # Node ID 4860da718e872192470188c8003018338052a12e # Parent e880f5a130804733f41cf56ea3b30f0f4cd341a2 util.smqueue: Simplify compat table, fix dependent modules (thanks Martin) There was an off-by-one in the modulo calculation. Switching to a plain old array-table makes the apparent size of the queue wrong, but since some of the queue may not be available this is likely for the best. diff -r e880f5a13080 -r 4860da718e87 spec/util_smqueue_spec.lua --- a/spec/util_smqueue_spec.lua Thu Dec 16 12:16:08 2021 +0100 +++ b/spec/util_smqueue_spec.lua Thu Dec 16 12:16:45 2021 +0100 @@ -52,4 +52,30 @@ assert.same({ [7] = true; [8] = true; [9] = true; [10] = true; [11] = true; [12] = true }, resume); end) end) + + describe("#table", function () + it("produces a compat layer", function () + local q = smqueue.new(10); + for i = 1,10 do q:push(i); end + do + local t = q:table(); + assert.same({ 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 }, t); + end + do + for i = 11,20 do q:push(i); end + local t = q:table(); + assert.same({ 11; 12; 13; 14; 15; 16; 17; 18; 19; 20 }, t); + end + do + q:ack(15); + local t = q:table(); + assert.same({ 16; 17; 18; 19; 20 }, t); + end + do + q:ack(20); + local t = q:table(); + assert.same({}, t); + end + end) + end) end); diff -r e880f5a13080 -r 4860da718e87 teal-src/util/smqueue.tl --- a/teal-src/util/smqueue.tl Thu Dec 16 12:16:08 2021 +0100 +++ b/teal-src/util/smqueue.tl Thu Dec 16 12:16:45 2021 +0100 @@ -70,22 +70,13 @@ return self._queue:consume() end --- Compatibility wrapper, meant to look like a plain ol' array -local record compat_mt - _queue : smqueue -end - -function compat_mt:__index(i : integer) : any - if i < self._queue._tail then return nil end - return self._queue._queue._items[(i + self._queue._tail) % self._queue._queue.size]; -end - -function compat_mt:__len() : integer - return self._queue:count_unacked() -end - +-- Compatibility layer, plain ol' table function smqueue:table() : { any } - return setmetatable({ _queue = self }, compat_mt); + local t : { any } = {}; + for i, v in self:resume() do + t[i] = v; + end + return t; end local function freeze(q : smqueue) : { string:integer } diff -r e880f5a13080 -r 4860da718e87 util/smqueue.lua --- a/util/smqueue.lua Thu Dec 16 12:16:08 2021 +0100 +++ b/util/smqueue.lua Thu Dec 16 12:16:45 2021 +0100 @@ -38,17 +38,12 @@ function smqueue:consume() return self._queue:consume() end -local compat_mt = {} - -function compat_mt:__index(i) - if i < self._queue._tail then return nil end - return self._queue._queue._items[(i + self._queue._tail) % self._queue._queue.size] +function smqueue:table() + local t = {}; + for i, v in self:resume() do t[i] = v; end + return t end -function compat_mt:__len() return self._queue:count_unacked() end - -function smqueue:table() return setmetatable({ _queue = self }, compat_mt) end - local function freeze(q) return { head = q._head; tail = q._tail } end local queue_mt = { __name = "smqueue"; __index = smqueue; __len = smqueue.count_unacked; __freeze = freeze }