{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE BlockArguments #-}

-- | Chan Transport Instance.

module Control.Churro.Transport.Unagi where
    
import Control.Churro.Types
import Control.Churro.Prelude

import Control.Concurrent.Chan.Unagi
import Data.Void

data Unagi a

instance Transport Unagi where
    data In  Unagi a = ChanIn  (InChan  a)
    data Out Unagi a = ChanOut (OutChan a)
    yank :: Out Unagi a -> IO a
yank (ChanOut c) = OutChan a -> IO a
forall a. OutChan a -> IO a
readChan  OutChan a
c
    yeet :: In Unagi a -> a -> IO ()
yeet (ChanIn  c) = InChan a -> a -> IO ()
forall a. InChan a -> a -> IO ()
writeChan InChan a
c
    flex :: IO (In Unagi a, Out Unagi a)
flex = do 
        (InChan a
i, OutChan a
o) <- IO (InChan a, OutChan a)
forall a. IO (InChan a, OutChan a)
newChan
        (In Unagi a, Out Unagi a) -> IO (In Unagi a, Out Unagi a)
forall (m :: * -> *) a. Monad m => a -> m a
return (InChan a -> In Unagi a
forall a. InChan a -> In Unagi a
ChanIn InChan a
i, OutChan a -> Out Unagi a
forall a. OutChan a -> Out Unagi a
ChanOut OutChan a
o)

type ChurroUnagi a = Churro a Unagi

-- | Convenience function for running a Churro with an Unagi Transport.
-- 
runWaitUnagi :: ChurroUnagi a Void Void -> IO a
runWaitUnagi :: ChurroUnagi a Void Void -> IO a
runWaitUnagi = ChurroUnagi a Void Void -> IO a
forall (t :: * -> *) a. Transport t => Churro a t Void Void -> IO a
runWait

-- | Convenience function for running a Churro into a List with an Unagi Transport.
-- 
runWaitListUnagi :: ChurroUnagi () Void o -> IO [o]
runWaitListUnagi :: ChurroUnagi () Void o -> IO [o]
runWaitListUnagi = ChurroUnagi () Void o -> IO [o]
forall (t :: * -> *) a b.
(Transport t, Monoid a) =>
Churro a t Void b -> IO [b]
runWaitList