"""A bounded buffer between tasks (the tasking corpus, in Vibe)."""

from verada import Array, Modular, Natural, Protected, entry, when


class Slot(Modular, modulus=4):
    pass


class Ring(Array[int], index=Slot):
    pass


class Buffer(Protected):
    items: Ring = Ring([0, 0, 0, 0])
    first: Slot = 0
    count: Natural = 0

    @entry
    def put(self, x: int) -> None:
        when(self.count < 4)
        self.items[self.first + Slot(self.count)] = x
        self.count = self.count + 1

    @entry
    def get(self) -> int:
        when(self.count > 0)
        x = self.items[self.first]
        self.first = self.first + 1
        self.count = self.count - 1
        return x


shared = Buffer()
