never executed always true always false
1 module UnliftIO.MessageBox.Util.CallId
2 ( CallId (MkCallId),
3 HasCallIdCounter (..),
4 takeNext,
5 newCallIdCounter,
6 )
7 where
8
9 import Control.Monad.Reader (MonadReader, asks)
10 import UnliftIO.MessageBox.Util.Fresh
11 ( CounterVar,
12 incrementAndGet,
13 newCounterVar,
14 )
15 import UnliftIO (MonadIO, MonadUnliftIO)
16
17 -- | An identifier value every command send by 'call's.
18 newtype CallId = MkCallId Int
19 deriving newtype (Eq, Ord)
20
21 instance Show CallId where
22 showsPrec _ (MkCallId !i) =
23 showChar '<' . shows i . showChar '>'
24
25 -- | Class of environment records containing a 'CounterVar' for 'CallId's.
26 class HasCallIdCounter env where
27 getCallIdCounter :: env -> CounterVar CallId
28
29 instance HasCallIdCounter (CounterVar CallId) where
30 {-# INLINE getCallIdCounter #-}
31 getCallIdCounter = id
32
33 -- | Create a new 'CallId' 'CounterVar'.
34 {-# INLINE newCallIdCounter #-}
35 newCallIdCounter :: MonadIO m => m (CounterVar CallId)
36 newCallIdCounter = newCounterVar
37
38 -- | Increment and get a new 'CallId'.
39 {-# INLINE takeNext #-}
40 takeNext :: (MonadReader env m, HasCallIdCounter env, MonadUnliftIO m) => m CallId
41 takeNext = asks getCallIdCounter >>= incrementAndGet