src/System/IO/Streams/Concurrent/Unagi.hs
author Luke Hoersten <luke@hoersten.org>
Thu, 18 Dec 2014 11:50:13 -0600
changeset 5 2cbc7b16cd08
parent 4 1baf8e3b8ef2
permissions -rw-r--r--
Removed cruft from LICENSE.
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
4
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
     9
       , chanToPipe
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    10
       , dupStream
0
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    11
       ) where
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    12
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    13
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    14
------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    15
import           Control.Applicative           ((<$>), (<*>))
4
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    16
import           Control.Concurrent.Chan.Unagi (InChan, OutChan, dupChan,
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    17
                                                newChan, readChan, writeChan)
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    18
import           Control.Monad                 ((>=>))
0
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    19
import           Prelude                       hiding (read)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    20
import           System.IO.Streams.Internal    (InputStream, OutputStream,
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    21
                                                makeInputStream,
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    22
                                                makeOutputStream, read)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    23
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    24
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    25
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    26
------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    27
-- | 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
    28
-- yields end-of-stream.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    29
inputToChan :: InputStream a -> InChan (Maybe a) -> IO ()
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    30
inputToChan is ch = go
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    31
  where
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    32
    go = do
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    33
        mb <- read is
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    34
        writeChan ch mb
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    35
        maybe (return $! ()) (const go) mb
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    36
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    37
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    38
------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    39
-- | Turns an 'OutChan' into an input stream.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    40
--
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    41
chanToInput :: OutChan (Maybe a) -> IO (InputStream a)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    42
chanToInput ch = makeInputStream $! readChan ch
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    43
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    44
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    45
------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    46
-- | Turns an 'InChan' into an output stream.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    47
--
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    48
chanToOutput :: InChan (Maybe a) -> IO (OutputStream a)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    49
chanToOutput = makeOutputStream . writeChan
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    50
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    51
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    52
--------------------------------------------------------------------------------
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    53
-- | Create a new pair of streams using an underlying 'Chan'. Everything written
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    54
-- to the 'OutputStream' will appear as-is on the 'InputStream'.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    55
--
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    56
-- Since reading from the 'InputStream' and writing to the 'OutputStream' are
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    57
-- blocking calls, be sure to do so in different threads.
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    58
makeChanPipe :: IO (InputStream a, OutputStream a)
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    59
makeChanPipe = do
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    60
    (inChan, outChan) <- newChan
e77b750dfbda Initial commit.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    61
    (,) <$> chanToInput outChan <*> chanToOutput inChan
4
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    62
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    63
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    64
--------------------------------------------------------------------------------
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    65
-- | Create a new pair of streams form the given 'Chan'. Everything written
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    66
-- to the 'OutputStream' will appear as-is on the 'InputStream'.
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    67
--
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    68
-- Since reading from the 'InputStream' and writing to the 'OutputStream' are
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    69
-- blocking calls, be sure to do so in different threads.
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    70
chanToPipe :: (InChan (Maybe a), OutChan (Maybe a)) -> IO (InputStream a, OutputStream a)
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    71
chanToPipe (inChan, outChan) = (,) <$> chanToInput outChan <*> chanToOutput inChan
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    72
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    73
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    74
--------------------------------------------------------------------------------
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    75
-- | Create a new input stream duplicated from the 'InChan'
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    76
--
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    77
dupStream :: InChan (Maybe a) -> IO (InputStream a)
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    78
dupStream = dupChan >=> chanToInput