@@ -5,20 +5,20 @@
5{-# LANGUAGE DeriveDataTypeable #-}
5{-# LANGUAGE DeriveDataTypeable #-}
6module Data.Conduit
6module Data.Conduit
7 ( -- * Helper types
7 ( -- * Helper types
9− , SinkResult (..)
8+ SinkResult (..)
10 , ConduitResult (..)
9 , ConduitResult (..)
11 -- * Main types
10 -- * Main types
13 , Sink (..)
11 , Sink (..)
14 , SinkInside (..)
12 , SinkInside (..)
16 -- * Connect pieces together
14 -- * Connect pieces together
20− -- * Helper functions
19+ -- * Other modules
21− , mkSource
20+ , module Data.Conduit.Types.Source
21+ , module Data.Conduit.Util.Source
24import Control.Monad.Trans.Resource
24import Control.Monad.Trans.Resource
@@ -30,13 +30,8 @@ import qualified Data.IORef as I
30import Data.Typeable (Typeable)
30import Data.Typeable (Typeable)
31import Control.Monad (liftM)
31import Control.Monad (liftM)
32import Control.Applicative (Applicative (..))
32import Control.Applicative (Applicative (..))
33−import Data.Monoid (Monoid (mempty, mappend, mconcat))
33+import Data.Conduit.Types.Source
34−
34+import Data.Conduit.Util.Source
35−data Stream a = EOF | Chunks [a]
37−instance Functor Stream where
39− fmap f (Chunks l) = Chunks (map f l)
41data SinkResult input output = SinkResult [input] (Maybe output)
36data SinkResult input output = SinkResult [input] (Maybe output)
42instance Functor (SinkResult input) where
37instance Functor (SinkResult input) where
@@ -44,38 +39,6 @@ instance Functor (SinkResult input) where
45data ConduitResult input output = ConduitResult [input] (Stream output)
40data ConduitResult input output = ConduitResult [input] (Stream output)
47−data SourceInside m a = SourceInside
48− { sourcePull :: ResourceT m (Stream a)
49− , sourcePush :: [a] -> ResourceT m ()
51−newtype Source m a = Source { unSource :: ResourceT m (SourceInside m a) }
52−instance MonadBaseControl IO m => Monoid (Source m a) where
53− mempty = mkSource $ return EOF
54− mappend a b = mconcat [a, b]
56− mconcat (Source mnext:rest0) = Source $ do
58− istate <- liftBase $ I.newIORef $ Just (next0, rest0)
59− unSource $ mkSource $ go istate
62− state <- liftBase $ I.readIORef istate
64− Nothing -> return EOF
65− Just (next, rest) -> do
66− stream <- sourcePull next
72− liftBase $ I.writeIORef istate $ Just (a', as)
75− liftBase $ I.writeIORef istate Nothing
77− Chunks _ -> return stream
79type SinkInsideData a m b = Stream a -> ResourceT m (SinkResult a b)
42type SinkInsideData a m b = Stream a -> ResourceT m (SinkResult a b)
80data SinkInside a m b =
43data SinkInside a m b =
81 SinkData (SinkInsideData a m b)
44 SinkData (SinkInsideData a m b)
@@ -172,44 +135,31 @@ sinkJoin (Sink msinkI) = Sink $ do
173type Conduit a m b = Stream a -> ResourceT m (ConduitResult a b)
136type Conduit a m b = Stream a -> ResourceT m (ConduitResult a b)
175−mkSource :: MonadBaseControl IO m
176− => ResourceT m (Stream a)
178−mkSource pull = Source $ do
179− buffer <- liftBase $ I.newIORef []
182− x <- liftBase $ I.atomicModifyIORef buffer (\a -> ([], a))
185− else return $ Chunks x
186− , sourcePush = \s ->
190− I.atomicModifyIORef buffer (\a -> (s ++ a, ()))
193infixr 0 $$
138infixr 0 $$
195($$) :: MonadBaseControl IO m => Source m a -> Sink a m b -> ResourceT m b
140($$) :: MonadBaseControl IO m => Source m a -> Sink a m b -> ResourceT m b
196−Source msource $$ Sink msink = do
141+source $$ Sink msink = do
197 sinkI <- msink
142 sinkI <- msink
198 case sinkI of
143 case sinkI of
199 SinkNoData output -> return output
144 SinkNoData output -> return output
200 SinkData sink -> do
145 SinkData sink -> do
201− source <- msource
146+ bs <- bsource source
202− connect' source sink
147+ connect' bs sink
204− connect' source sink = do
149+ connect' bs sink = do
205− stream <- sourcePull source
150+ stream <- bsourcePull bs
206 SinkResult leftover mres <- sink stream
151 SinkResult leftover mres <- sink stream
207− sourcePush source leftover
152+ bsourceUnpull bs leftover
208 case (stream, mres) of
153 case (stream, mres) of
209 (_, Just res) -> return res
154 (_, Just res) -> return res
210− (Chunks _, Nothing) -> connect' source sink
155+ (Chunks _, Nothing) -> connect' bs sink
211 (EOF, Nothing) -> liftBase (throwIO NothingAfterEOF)
156 (EOF, Nothing) -> liftBase (throwIO NothingAfterEOF)
160+(<$$>) :: MonadBaseControl IO m => SourceM m a -> Sink a m b -> ResourceT m b
161+SourceM msrc <$$> sink = msrc >>= ($$ sink)
213data NothingAfterEOF = NothingAfterEOF
163data NothingAfterEOF = NothingAfterEOF
214 deriving (Show, Typeable)
164 deriving (Show, Typeable)
215instance Exception NothingAfterEOF
165instance Exception NothingAfterEOF
@@ -220,13 +170,15 @@ infixl 1 $=
220 => Source m a
170 => Source m a
221 -> Conduit a m b
171 -> Conduit a m b
222 -> Source m b
172 -> Source m b
223−Source msource $= pipe = Source $ do
173+_ $= _ =
224− source <- msource
174+ undefined
225 source' <- unSource $ mkSource $ do
176 source' <- unSource $ mkSource $ do
226 ConduitResult leftover result <- sourcePull source >>= pipe
177 ConduitResult leftover result <- sourcePull source >>= pipe
227 sourcePush source leftover
178 sourcePush source leftover
228 return result
179 return result
229 return source'
180 return source'
231infixr 0 =$
183infixr 0 =$