-- | For full documentation please refer to "Control.Concurrent.Chan".
module Control.Concurrent.Chan.Strict
  ( Chan'

    -- * Operations
  , newChan'
  , writeChan'
  , readChan'
  , dupChan'
  , getChan'Contents
  , writeList2Chan'
  ) where

import Control.Exception (evaluate)
import qualified Control.Concurrent.Chan as Base

-- | A strict (WHNF) variant of 'Base.Chan'.
newtype Chan' a = Chan' (Base.Chan a)
  deriving Chan' a -> Chan' a -> Bool
(Chan' a -> Chan' a -> Bool)
-> (Chan' a -> Chan' a -> Bool) -> Eq (Chan' a)
forall a. Chan' a -> Chan' a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Chan' a -> Chan' a -> Bool
== :: Chan' a -> Chan' a -> Bool
$c/= :: forall a. Chan' a -> Chan' a -> Bool
/= :: Chan' a -> Chan' a -> Bool
Eq

-- | 'Base.newChan' for 'Chan''.
newChan' :: IO (Chan' a)
newChan' :: forall a. IO (Chan' a)
newChan' = Chan a -> Chan' a
forall a. Chan a -> Chan' a
Chan' (Chan a -> Chan' a) -> IO (Chan a) -> IO (Chan' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Chan a)
forall a. IO (Chan a)
Base.newChan

-- | 'Base.writeChan' for 'Chan''.
--
-- Evaluates the value to WHNF.
writeChan' :: Chan' a -> a -> IO ()
writeChan' :: forall a. Chan' a -> a -> IO ()
writeChan' (Chan' Chan a
chan) a
a = Chan a -> a -> IO ()
forall a. Chan a -> a -> IO ()
Base.writeChan Chan a
chan (a -> IO ()) -> IO a -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< a -> IO a
forall a. a -> IO a
evaluate a
a

-- | 'Base.readChan' for 'Chan''.
readChan' :: Chan' a -> IO a
readChan' :: forall a. Chan' a -> IO a
readChan' (Chan' Chan a
chan) = Chan a -> IO a
forall a. Chan a -> IO a
Base.readChan Chan a
chan

-- | 'Base.dupChan' for 'Chan''.
dupChan' :: Chan' a -> IO (Chan' a)
dupChan' :: forall a. Chan' a -> IO (Chan' a)
dupChan' (Chan' Chan a
chan) = Chan a -> Chan' a
forall a. Chan a -> Chan' a
Chan' (Chan a -> Chan' a) -> IO (Chan a) -> IO (Chan' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Chan a -> IO (Chan a)
forall a. Chan a -> IO (Chan a)
Base.dupChan Chan a
chan

-- | 'Base.getChanContents' for 'Chan''.
getChan'Contents :: Chan' a -> IO [a]
getChan'Contents :: forall a. Chan' a -> IO [a]
getChan'Contents (Chan' Chan a
chan) = Chan a -> IO [a]
forall a. Chan a -> IO [a]
Base.getChanContents Chan a
chan

-- | 'Base.writeList2Chan' for 'Chan''.
--
-- Evaluates the values to WHNF.
writeList2Chan' :: Chan' a -> [a] -> IO ()
writeList2Chan' :: forall a. Chan' a -> [a] -> IO ()
writeList2Chan' = (a -> IO ()) -> [a] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((a -> IO ()) -> [a] -> IO ())
-> (Chan' a -> a -> IO ()) -> Chan' a -> [a] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Chan' a -> a -> IO ()
forall a. Chan' a -> a -> IO ()
writeChan'