Added some functions to help exposed chans to streams.
authorLuke Hoersten <luke@hoersten.org>
Thu, 18 Dec 2014 11:43:53 -0600
changeset 4 1baf8e3b8ef2
parent 3 400d49213290
child 5 2cbc7b16cd08
Added some functions to help exposed chans to streams.
src/System/IO/Streams/Concurrent/Unagi.hs
src/System/IO/Streams/Concurrent/Unagi/Bounded.hs
unagi-streams.cabal
--- a/src/System/IO/Streams/Concurrent/Unagi.hs	Thu Dec 18 10:56:53 2014 -0600
+++ b/src/System/IO/Streams/Concurrent/Unagi.hs	Thu Dec 18 11:43:53 2014 -0600
@@ -6,13 +6,16 @@
        , chanToInput
        , chanToOutput
        , makeChanPipe
+       , chanToPipe
+       , dupStream
        ) where
 
 
 ------------------------------------------------------------------------------
 import           Control.Applicative           ((<$>), (<*>))
-import           Control.Concurrent.Chan.Unagi (InChan, OutChan, newChan,
-                                                readChan, writeChan)
+import           Control.Concurrent.Chan.Unagi (InChan, OutChan, dupChan,
+                                                newChan, readChan, writeChan)
+import           Control.Monad                 ((>=>))
 import           Prelude                       hiding (read)
 import           System.IO.Streams.Internal    (InputStream, OutputStream,
                                                 makeInputStream,
@@ -56,3 +59,20 @@
 makeChanPipe = do
     (inChan, outChan) <- newChan
     (,) <$> chanToInput outChan <*> chanToOutput inChan
+
+
+--------------------------------------------------------------------------------
+-- | Create a new pair of streams form the given 'Chan'. Everything written
+-- to the 'OutputStream' will appear as-is on the 'InputStream'.
+--
+-- Since reading from the 'InputStream' and writing to the 'OutputStream' are
+-- blocking calls, be sure to do so in different threads.
+chanToPipe :: (InChan (Maybe a), OutChan (Maybe a)) -> IO (InputStream a, OutputStream a)
+chanToPipe (inChan, outChan) = (,) <$> chanToInput outChan <*> chanToOutput inChan
+
+
+--------------------------------------------------------------------------------
+-- | Create a new input stream duplicated from the 'InChan'
+--
+dupStream :: InChan (Maybe a) -> IO (InputStream a)
+dupStream = dupChan >=> chanToInput
--- a/src/System/IO/Streams/Concurrent/Unagi/Bounded.hs	Thu Dec 18 10:56:53 2014 -0600
+++ b/src/System/IO/Streams/Concurrent/Unagi/Bounded.hs	Thu Dec 18 11:43:53 2014 -0600
@@ -6,14 +6,17 @@
        , chanToInput
        , chanToOutput
        , makeChanPipe
+       , chanToPipe
+       , dupStream
        ) where
 
 
 ------------------------------------------------------------------------------
 import           Control.Applicative                   ((<$>), (<*>))
 import           Control.Concurrent.Chan.Unagi.Bounded (InChan, OutChan,
-                                                        newChan, readChan,
-                                                        writeChan)
+                                                        dupChan, newChan,
+                                                        readChan, writeChan)
+import           Control.Monad                         ((>=>))
 import           Prelude                               hiding (read)
 import           System.IO.Streams.Internal            (InputStream,
                                                         OutputStream,
@@ -57,3 +60,20 @@
 makeChanPipe size = do
     (inChan, outChan) <- newChan size
     (,) <$> chanToInput outChan <*> chanToOutput inChan
+
+
+--------------------------------------------------------------------------------
+-- | Create a new pair of streams form the given 'Chan'. Everything written
+-- to the 'OutputStream' will appear as-is on the 'InputStream'.
+--
+-- Since reading from the 'InputStream' and writing to the 'OutputStream' are
+-- blocking calls, be sure to do so in different threads.
+chanToPipe :: (InChan (Maybe a), OutChan (Maybe a)) -> IO (InputStream a, OutputStream a)
+chanToPipe (inChan, outChan) = (,) <$> chanToInput outChan <*> chanToOutput inChan
+
+
+--------------------------------------------------------------------------------
+-- | Create a new input stream duplicated from the 'InChan'
+--
+dupStream :: InChan (Maybe a) -> IO (InputStream a)
+dupStream = dupChan >=> chanToInput
--- a/unagi-streams.cabal	Thu Dec 18 10:56:53 2014 -0600
+++ b/unagi-streams.cabal	Thu Dec 18 11:43:53 2014 -0600
@@ -1,9 +1,9 @@
 name:                unagi-streams
 version:             0.1.1.0
 synopsis:            Unagi Chan IO-Streams
-description:         IO-Streams implemented underneath with Unagi
-                     channels. This library is a straight port of Greg Collins' IO-Streams
-                     Chan implementation.
+description:         Io-streams chans implemented underneath with
+                     unagi-chans. This library is a straight port of
+                     the default io-streams chan implementation.
 License:             BSD3
 License-file:        LICENSE
 author:              Luke Hoersten