src/System/IO/Streams/Concurrent/Unagi.hs
author Luke Hoersten <luke@hoersten.org>
Tue, 09 Dec 2014 13:37:21 -0600
changeset 2 1760b7d150cf
parent 0 e77b750dfbda
child 4 1baf8e3b8ef2
permissions -rw-r--r--
Added bounded dup streams.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     1
{-# LANGUAGE BangPatterns #-}
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     2
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     3
module System.IO.Streams.Concurrent.Unagi
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     4
       ( -- * Channel conversions
2
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents: 0
diff changeset
     5
         inputToChan
0
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     6
       , chanToInput
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     7
       , chanToOutput
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     8
       , makeChanPipe
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
     9
       ) where
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    10
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    11
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    12
------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    13
import           Control.Applicative           ((<$>), (<*>))
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    14
import           Control.Concurrent.Chan.Unagi (InChan, OutChan, newChan,
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    15
                                                readChan, writeChan)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    16
import           Prelude                       hiding (read)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    17
import           System.IO.Streams.Internal    (InputStream, OutputStream,
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    18
                                                makeInputStream,
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    19
                                                makeOutputStream, read)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    20
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    21
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    22
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    23
------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    24
-- | Writes the contents of an input stream to a channel until the input stream
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    25
-- yields end-of-stream.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    26
inputToChan :: InputStream a -> InChan (Maybe a) -> IO ()
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    27
inputToChan is ch = go
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    28
  where
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    29
    go = do
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    30
        mb <- read is
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    31
        writeChan ch mb
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    32
        maybe (return $! ()) (const go) mb
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    33
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    34
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    35
------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    36
-- | Turns an 'OutChan' into an input stream.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    37
--
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    38
chanToInput :: OutChan (Maybe a) -> IO (InputStream a)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    39
chanToInput ch = makeInputStream $! readChan ch
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    40
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    41
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    42
------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    43
-- | Turns an 'InChan' into an output stream.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    44
--
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    45
chanToOutput :: InChan (Maybe a) -> IO (OutputStream a)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    46
chanToOutput = makeOutputStream . writeChan
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    47
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    48
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    49
--------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    50
-- | Create a new pair of streams using an underlying 'Chan'. Everything written
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    51
-- to the 'OutputStream' will appear as-is on the 'InputStream'.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    52
--
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    53
-- Since reading from the 'InputStream' and writing to the 'OutputStream' are
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    54
-- blocking calls, be sure to do so in different threads.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    55
makeChanPipe :: IO (InputStream a, OutputStream a)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    56
makeChanPipe = do
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    57
    (inChan, outChan) <- newChan
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    58
    (,) <$> chanToInput outChan <*> chanToOutput inChan