module Supply
  ( Supply,
    new,
    next,
  )
where

import Data.Atomics.Counter (AtomicCounter, incrCounter, newCounter)

newtype Supply
  = Supply AtomicCounter

new :: IO Supply
new :: IO Supply
new =
  AtomicCounter -> Supply
Supply (AtomicCounter -> Supply) -> IO AtomicCounter -> IO Supply
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> IO AtomicCounter
newCounter Int
0

next :: Supply -> IO Int
next :: Supply -> IO Int
next (Supply AtomicCounter
counter) =
  Int -> AtomicCounter -> IO Int
incrCounter Int
1 AtomicCounter
counter
{-# INLINE next #-}