author | Luke Hoersten <luke@hoersten.org> |
Thu, 18 Dec 2014 10:56:53 -0600 | |
changeset 3 | 400d49213290 |
parent 2 | 1760b7d150cf |
child 4 | 1baf8e3b8ef2 |
permissions | -rw-r--r-- |
2 | 1 |
{-# LANGUAGE BangPatterns #-} |
2 |
||
3 |
module System.IO.Streams.Concurrent.Unagi.Bounded |
|
4 |
( -- * Channel conversions |
|
5 |
inputToChan |
|
6 |
, chanToInput |
|
7 |
, chanToOutput |
|
8 |
, makeChanPipe |
|
9 |
) where |
|
10 |
||
11 |
||
12 |
------------------------------------------------------------------------------ |
|
3
400d49213290
Removed opaque type for chan dupping.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
13 |
import Control.Applicative ((<$>), (<*>)) |
2 | 14 |
import Control.Concurrent.Chan.Unagi.Bounded (InChan, OutChan, |
3
400d49213290
Removed opaque type for chan dupping.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
15 |
newChan, readChan, |
400d49213290
Removed opaque type for chan dupping.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
16 |
writeChan) |
2 | 17 |
import Prelude hiding (read) |
18 |
import System.IO.Streams.Internal (InputStream, |
|
19 |
OutputStream, |
|
20 |
makeInputStream, |
|
21 |
makeOutputStream, read) |
|
22 |
||
23 |
||
24 |
------------------------------------------------------------------------------ |
|
25 |
-- | Writes the contents of an input stream to a channel until the input stream |
|
26 |
-- yields end-of-stream. |
|
27 |
inputToChan :: InputStream a -> InChan (Maybe a) -> IO () |
|
28 |
inputToChan is ch = go |
|
29 |
where |
|
30 |
go = do |
|
31 |
mb <- read is |
|
32 |
writeChan ch mb |
|
33 |
maybe (return $! ()) (const go) mb |
|
34 |
||
35 |
||
36 |
------------------------------------------------------------------------------ |
|
37 |
-- | Turns an 'OutChan' into an input stream. |
|
38 |
-- |
|
39 |
chanToInput :: OutChan (Maybe a) -> IO (InputStream a) |
|
40 |
chanToInput ch = makeInputStream $! readChan ch |
|
41 |
||
42 |
||
43 |
------------------------------------------------------------------------------ |
|
44 |
-- | Turns an 'InChan' into an output stream. |
|
45 |
-- |
|
46 |
chanToOutput :: InChan (Maybe a) -> IO (OutputStream a) |
|
47 |
chanToOutput = makeOutputStream . writeChan |
|
48 |
||
49 |
||
50 |
-------------------------------------------------------------------------------- |
|
51 |
-- | Create a new pair of streams using an underlying 'Chan'. Everything written |
|
52 |
-- to the 'OutputStream' will appear as-is on the 'InputStream'. |
|
53 |
-- |
|
54 |
-- Since reading from the 'InputStream' and writing to the 'OutputStream' are |
|
55 |
-- 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
|
56 |
makeChanPipe :: Int -> IO (InputStream a, OutputStream a) |
2 | 57 |
makeChanPipe size = do |
58 |
(inChan, outChan) <- newChan size |
|
3
400d49213290
Removed opaque type for chan dupping.
Luke Hoersten <luke@hoersten.org>
parents:
2
diff
changeset
|
59 |
(,) <$> chanToInput outChan <*> chanToOutput inChan |