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