src.nth.io/

summaryrefslogtreecommitdiff
path: root/src/System/IO
diff options
context:
space:
mode:
authorLuke Hoersten <[email protected]>2014-12-18 11:43:53 -0600
committerLuke Hoersten <[email protected]>2014-12-18 11:43:53 -0600
commit1b07fb994363495a102d6b86a5f847a771a6f3b9 (patch)
treed92b598ac1c0bd937508614ed144e77c1bb84c45 /src/System/IO
parent3b40b5c3c97b636c4130e8b85e347c53aed1b25f (diff)
Added some functions to help exposed chans to streams.
Diffstat (limited to 'src/System/IO')
-rw-r--r--src/System/IO/Streams/Concurrent/Unagi.hs24
-rw-r--r--src/System/IO/Streams/Concurrent/Unagi/Bounded.hs24
2 files changed, 44 insertions, 4 deletions
diff --git a/src/System/IO/Streams/Concurrent/Unagi.hs b/src/System/IO/Streams/Concurrent/Unagi.hs
index 7ee3fd5..141902f 100644
--- a/src/System/IO/Streams/Concurrent/Unagi.hs
+++ b/src/System/IO/Streams/Concurrent/Unagi.hs
@@ -6,13 +6,16 @@ module System.IO.Streams.Concurrent.Unagi
, 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 :: IO (InputStream a, OutputStream a)
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
diff --git a/src/System/IO/Streams/Concurrent/Unagi/Bounded.hs b/src/System/IO/Streams/Concurrent/Unagi/Bounded.hs
index fe1e856..96fc6e4 100644
--- a/src/System/IO/Streams/Concurrent/Unagi/Bounded.hs
+++ b/src/System/IO/Streams/Concurrent/Unagi/Bounded.hs
@@ -6,14 +6,17 @@ module System.IO.Streams.Concurrent.Unagi.Bounded
, 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 :: Int -> IO (InputStream a, OutputStream a)
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