4 ( -- * Channel conversions |
4 ( -- * Channel conversions |
5 inputToChan |
5 inputToChan |
6 , chanToInput |
6 , chanToInput |
7 , chanToOutput |
7 , chanToOutput |
8 , makeChanPipe |
8 , makeChanPipe |
|
9 , chanToPipe |
|
10 , dupStream |
9 ) where |
11 ) where |
10 |
12 |
11 |
13 |
12 ------------------------------------------------------------------------------ |
14 ------------------------------------------------------------------------------ |
13 import Control.Applicative ((<$>), (<*>)) |
15 import Control.Applicative ((<$>), (<*>)) |
14 import Control.Concurrent.Chan.Unagi.Bounded (InChan, OutChan, |
16 import Control.Concurrent.Chan.Unagi.Bounded (InChan, OutChan, |
15 newChan, readChan, |
17 dupChan, newChan, |
16 writeChan) |
18 readChan, writeChan) |
|
19 import Control.Monad ((>=>)) |
17 import Prelude hiding (read) |
20 import Prelude hiding (read) |
18 import System.IO.Streams.Internal (InputStream, |
21 import System.IO.Streams.Internal (InputStream, |
19 OutputStream, |
22 OutputStream, |
20 makeInputStream, |
23 makeInputStream, |
21 makeOutputStream, read) |
24 makeOutputStream, read) |
55 -- blocking calls, be sure to do so in different threads. |
58 -- blocking calls, be sure to do so in different threads. |
56 makeChanPipe :: Int -> IO (InputStream a, OutputStream a) |
59 makeChanPipe :: Int -> IO (InputStream a, OutputStream a) |
57 makeChanPipe size = do |
60 makeChanPipe size = do |
58 (inChan, outChan) <- newChan size |
61 (inChan, outChan) <- newChan size |
59 (,) <$> chanToInput outChan <*> chanToOutput inChan |
62 (,) <$> chanToInput outChan <*> chanToOutput inChan |
|
63 |
|
64 |
|
65 -------------------------------------------------------------------------------- |
|
66 -- | Create a new pair of streams form the given 'Chan'. Everything written |
|
67 -- to the 'OutputStream' will appear as-is on the 'InputStream'. |
|
68 -- |
|
69 -- Since reading from the 'InputStream' and writing to the 'OutputStream' are |
|
70 -- blocking calls, be sure to do so in different threads. |
|
71 chanToPipe :: (InChan (Maybe a), OutChan (Maybe a)) -> IO (InputStream a, OutputStream a) |
|
72 chanToPipe (inChan, outChan) = (,) <$> chanToInput outChan <*> chanToOutput inChan |
|
73 |
|
74 |
|
75 -------------------------------------------------------------------------------- |
|
76 -- | Create a new input stream duplicated from the 'InChan' |
|
77 -- |
|
78 dupStream :: InChan (Maybe a) -> IO (InputStream a) |
|
79 dupStream = dupChan >=> chanToInput |