src/System/IO/Streams/Concurrent/Unagi/Bounded.hs
author Luke Hoersten <luke@hoersten.org>
Tue, 31 May 2016 22:18:36 -0500
changeset 10 6b78f0dd0a6b
parent 4 1baf8e3b8ef2
permissions -rw-r--r--
Added metadata files.
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
4
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
     9
       , chanToPipe
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    10
       , dupStream
2
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
------------------------------------------------------------------------------
3
400d49213290 Removed opaque type for chan dupping.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    15
import           Control.Applicative                   ((<$>), (<*>))
2
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    16
import           Control.Concurrent.Chan.Unagi.Bounded (InChan, OutChan,
4
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    17
                                                        dupChan, newChan,
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    18
                                                        readChan, writeChan)
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    19
import           Control.Monad                         ((>=>))
2
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
------------------------------------------------------------------------------
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    28
-- | 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
    29
-- yields end-of-stream.
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    30
inputToChan :: InputStream a -> InChan (Maybe a) -> IO ()
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    31
inputToChan is ch = go
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    32
  where
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    33
    go = do
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    34
        mb <- read is
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    35
        writeChan ch mb
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    36
        maybe (return $! ()) (const go) mb
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    37
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
------------------------------------------------------------------------------
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    40
-- | Turns an 'OutChan' into an input stream.
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
chanToInput :: OutChan (Maybe a) -> IO (InputStream a)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    43
chanToInput ch = makeInputStream $! readChan ch
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    44
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
------------------------------------------------------------------------------
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    47
-- | Turns an 'InChan' into an output stream.
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
chanToOutput :: InChan (Maybe a) -> IO (OutputStream a)
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    50
chanToOutput = makeOutputStream . writeChan
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    51
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    52
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
-- | 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
    55
-- to the 'OutputStream' will appear as-is on the 'InputStream'.
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    56
--
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    57
-- Since reading from the 'InputStream' and writing to the 'OutputStream' are
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    58
-- 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
    59
makeChanPipe :: Int -> IO (InputStream a, OutputStream a)
2
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    60
makeChanPipe size = do
1760b7d150cf Added bounded dup streams.
Luke Hoersten <luke@hoersten.org>
parents:
diff changeset
    61
    (inChan, outChan) <- newChan size
3
400d49213290 Removed opaque type for chan dupping.
Luke Hoersten <luke@hoersten.org>
parents: 2
diff changeset
    62
    (,) <$> chanToInput outChan <*> chanToOutput inChan
4
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    63
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    64
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    65
--------------------------------------------------------------------------------
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    66
-- | 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: 3
diff changeset
    67
-- 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: 3
diff changeset
    68
--
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    69
-- 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: 3
diff changeset
    70
-- 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: 3
diff changeset
    71
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: 3
diff changeset
    72
chanToPipe (inChan, outChan) = (,) <$> chanToInput outChan <*> chanToOutput inChan
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    73
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    74
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    75
--------------------------------------------------------------------------------
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    76
-- | 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: 3
diff changeset
    77
--
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    78
dupStream :: InChan (Maybe a) -> IO (InputStream a)
1baf8e3b8ef2 Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents: 3
diff changeset
    79
dupStream = dupChan >=> chanToInput