src/System/IO/Streams/Concurrent/Unagi/Bounded.hs
author Luke Hoersten <luke@hoersten.org>
Tue, 09 Dec 2014 13:37:21 -0600
changeset 2 1760b7d150cf
child 3 400d49213290
permissions -rw-r--r--
Added bounded dup streams.
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
       , dupStream
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    10
       , DupHandle
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    11
       ) where
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    12
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    13
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    14
------------------------------------------------------------------------------
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    15
import           Control.Applicative                   (pure, (<$>), (<*>))
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    16
import           Control.Concurrent.Chan.Unagi.Bounded (InChan, OutChan,
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    17
                                                        dupChan, newChan,
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    18
                                                        readChan, writeChan)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    19
import           Control.Monad                         ((>=>))
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    20
import           Prelude                               hiding (read)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    21
import           System.IO.Streams.Internal            (InputStream,
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    22
                                                        OutputStream,
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    23
                                                        makeInputStream,
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    24
                                                        makeOutputStream, read)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    25
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    26
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    27
newtype DupHandle a = DupHandle { unDupHandle :: InChan (Maybe a) }
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    28
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    29
------------------------------------------------------------------------------
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    30
-- | 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
    31
-- yields end-of-stream.
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    32
inputToChan :: InputStream a -> InChan (Maybe a) -> IO ()
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    33
inputToChan is ch = go
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    34
  where
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    35
    go = do
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    36
        mb <- read is
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    37
        writeChan ch mb
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    38
        maybe (return $! ()) (const go) mb
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    39
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    40
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
-- | Turns an 'OutChan' into an input stream.
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
chanToInput :: OutChan (Maybe a) -> IO (InputStream a)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    45
chanToInput ch = makeInputStream $! readChan ch
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    46
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    47
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
-- | Turns an 'InChan' into an output stream.
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
chanToOutput :: InChan (Maybe a) -> IO (OutputStream a)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    52
chanToOutput = makeOutputStream . writeChan
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
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    55
--------------------------------------------------------------------------------
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    56
-- | 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
    57
-- to the 'OutputStream' will appear as-is on the 'InputStream'.
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    58
--
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    59
-- Since reading from the 'InputStream' and writing to the 'OutputStream' are
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    60
-- blocking calls, be sure to do so in different threads.
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    61
makeChanPipe :: Int -> IO (InputStream a, OutputStream a, DupHandle a)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    62
makeChanPipe size = do
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    63
    (inChan, outChan) <- newChan size
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    64
    (,,) <$> chanToInput outChan <*> chanToOutput inChan <*> pure (DupHandle inChan)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    65
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    66
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    67
------------------------------------------------------------------------------
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    68
-- | Use a 'DupHandle' to replicate everything written on the
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    69
-- associated 'OutputStream' to the 'InputStream'.
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    70
--
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    71
dupStream :: DupHandle a -> IO (InputStream a)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    72
dupStream = dupChan . unDupHandle >=> chanToInput