-- |Description: Interpreter for 'SyncRead' that reinterprets to 'Sync'.
module Polysemy.Conc.Interpreter.SyncRead where

import qualified Polysemy.Conc.Effect.Sync as Sync
import Polysemy.Conc.Effect.Sync (Sync)
import qualified Polysemy.Conc.Effect.SyncRead as SyncRead
import Polysemy.Conc.Effect.SyncRead (SyncRead)

-- |Run 'SyncRead' in terms of 'Sync'.
syncRead ::
   d r .
  Member (Sync d) r =>
  InterpreterFor (SyncRead d) r
syncRead :: forall d (r :: [(* -> *) -> * -> *]).
Member (Sync d) r =>
InterpreterFor (SyncRead d) r
syncRead =
  (forall (rInitial :: [(* -> *) -> * -> *]) x.
 SyncRead d (Sem rInitial) x -> Sem r x)
-> Sem (SyncRead d : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e "interpret" =>
(forall (rInitial :: [(* -> *) -> * -> *]) x.
 e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret \case
    SyncRead d (Sem rInitial) x
SyncRead.Block ->
      Sem r x
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Sync d) r =>
Sem r d
Sync.block
    SyncRead.Wait u
u ->
      u -> Sem r (Maybe d)
forall d (r :: [(* -> *) -> * -> *]) u.
(MemberWithError (Sync d) r, TimeUnit u) =>
u -> Sem r (Maybe d)
Sync.wait u
u
    SyncRead d (Sem rInitial) x
SyncRead.Try ->
      Sem r x
forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Sync d) r =>
Sem r (Maybe d)
Sync.try
    SyncRead d (Sem rInitial) x
SyncRead.Empty ->
      forall d (r :: [(* -> *) -> * -> *]).
MemberWithError (Sync d) r =>
Sem r Bool
Sync.empty @d
{-# inline syncRead #-}