author | Luke Hoersten <luke@hoersten.org> |
Tue, 31 May 2016 22:18:36 -0500 | |
changeset 10 | 6b78f0dd0a6b |
parent 4 | 1baf8e3b8ef2 |
permissions | -rw-r--r-- |
0 | 1 |
{-# LANGUAGE BangPatterns #-} |
2 |
||
3 |
module System.IO.Streams.Concurrent.Unagi |
|
4 |
( -- * Channel conversions |
|
2 | 5 |
inputToChan |
0 | 6 |
, chanToInput |
7 |
, chanToOutput |
|
8 |
, makeChanPipe |
|
4
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
9 |
, chanToPipe |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
10 |
, dupStream |
0 | 11 |
) where |
12 |
||
13 |
||
14 |
------------------------------------------------------------------------------ |
|
15 |
import Control.Applicative ((<$>), (<*>)) |
|
4
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
16 |
import Control.Concurrent.Chan.Unagi (InChan, OutChan, dupChan, |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
17 |
newChan, readChan, writeChan) |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
18 |
import Control.Monad ((>=>)) |
0 | 19 |
import Prelude hiding (read) |
20 |
import System.IO.Streams.Internal (InputStream, OutputStream, |
|
21 |
makeInputStream, |
|
22 |
makeOutputStream, read) |
|
23 |
||
24 |
||
25 |
||
26 |
------------------------------------------------------------------------------ |
|
27 |
-- | Writes the contents of an input stream to a channel until the input stream |
|
28 |
-- yields end-of-stream. |
|
29 |
inputToChan :: InputStream a -> InChan (Maybe a) -> IO () |
|
30 |
inputToChan is ch = go |
|
31 |
where |
|
32 |
go = do |
|
33 |
mb <- read is |
|
34 |
writeChan ch mb |
|
35 |
maybe (return $! ()) (const go) mb |
|
36 |
||
37 |
||
38 |
------------------------------------------------------------------------------ |
|
39 |
-- | Turns an 'OutChan' into an input stream. |
|
40 |
-- |
|
41 |
chanToInput :: OutChan (Maybe a) -> IO (InputStream a) |
|
42 |
chanToInput ch = makeInputStream $! readChan ch |
|
43 |
||
44 |
||
45 |
------------------------------------------------------------------------------ |
|
46 |
-- | Turns an 'InChan' into an output stream. |
|
47 |
-- |
|
48 |
chanToOutput :: InChan (Maybe a) -> IO (OutputStream a) |
|
49 |
chanToOutput = makeOutputStream . writeChan |
|
50 |
||
51 |
||
52 |
-------------------------------------------------------------------------------- |
|
53 |
-- | Create a new pair of streams using an underlying 'Chan'. Everything written |
|
54 |
-- to the 'OutputStream' will appear as-is on the 'InputStream'. |
|
55 |
-- |
|
56 |
-- Since reading from the 'InputStream' and writing to the 'OutputStream' are |
|
57 |
-- blocking calls, be sure to do so in different threads. |
|
58 |
makeChanPipe :: IO (InputStream a, OutputStream a) |
|
59 |
makeChanPipe = do |
|
60 |
(inChan, outChan) <- newChan |
|
61 |
(,) <$> chanToInput outChan <*> chanToOutput inChan |
|
4
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
62 |
|
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
63 |
|
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
64 |
-------------------------------------------------------------------------------- |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
65 |
-- | 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:
2
diff
changeset
|
66 |
-- 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:
2
diff
changeset
|
67 |
-- |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
68 |
-- 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:
2
diff
changeset
|
69 |
-- 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:
2
diff
changeset
|
70 |
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:
2
diff
changeset
|
71 |
chanToPipe (inChan, outChan) = (,) <$> chanToInput outChan <*> chanToOutput inChan |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
72 |
|
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
73 |
|
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
74 |
-------------------------------------------------------------------------------- |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
75 |
-- | 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:
2
diff
changeset
|
76 |
-- |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
77 |
dupStream :: InChan (Maybe a) -> IO (InputStream a) |
1baf8e3b8ef2
Added some functions to help exposed chans to streams.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
78 |
dupStream = dupChan >=> chanToInput |