Skip to content

Commit

Permalink
storage.push now accepts an optional value
Browse files Browse the repository at this point in the history
This is useful when the entry it's post-initialized after the push.
  • Loading branch information
Andre-LA committed Jan 16, 2023
1 parent d70f05c commit cc4f48c
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions rotor/storage.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,24 @@ local GenIdx = require 'rotor.gen_idx'
Otherwise, the entry will be inserted on the found free slot and this function will return `true`,
the generational index of the used slot and a reference to the inserted entry.
You can optionally pass a value to initialize this entry.
]]
function storage:push(value: T): (boolean, GenIdx, *T)
function storage:push(value: facultative(T)): (boolean, GenIdx, *T)
local available_found, available_idx = find_next_available(self, self.next_idx)

if not available_found then
warn(#[storage.value.nickname .. ": out of available entries, please increase the storage's size"]#)
return false
end

## if value.type.is_niltype then
self.entries[available_idx] = {}
## else
self.entries[available_idx] = value
## end

self.next_idx = (available_idx + 1) % SIZE
self.entries[available_idx] = value
self.unavailables[available_idx] = true
self.generations[available_idx] = self.generations[available_idx] + 1

Expand Down Expand Up @@ -152,10 +159,7 @@ local GenIdx = require 'rotor.gen_idx'
end
## end

self.entries = {}
self.unavailables = {}
self.generations = {}
self.next_idx = 0
$self = {}
end

## return storage
Expand Down

0 comments on commit cc4f48c

Please sign in to comment.