@@ -59,12 +59,12 @@ data GbracketState s1 s2 v
60 :: Monad m
60 :: Monad m
61 => m c -- ^ before
61 => m c -- ^ before
62− -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
63 -> (c -> m d) -- ^ after, on normal stop
62 -> (c -> m d) -- ^ after, on normal stop
64 -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
63 -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
64+ -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
65 -> (c -> Stream m b) -- ^ stream generator
65 -> (c -> Stream m b) -- ^ stream generator
66 -> Stream m b
66 -> Stream m b
67−gbracket_ bef exc aft fexc fnormal =
67+gbracket_ bef aft onExc ftry action =
68 Stream step GBracketInit
68 Stream step GBracketInit
@@ -72,10 +72,10 @@ gbracket_ bef exc aft fexc fnormal =
72 {-# INLINE_LATE step #-}
72 {-# INLINE_LATE step #-}
73 step _ GBracketInit = do
73 step _ GBracketInit = do
75− return $ Skip $ GBracketNormal (fnormal r) r
75+ return $ Skip $ GBracketNormal (action r) r
77 step gst (GBracketNormal (UnStream step1 st) v) = do
77 step gst (GBracketNormal (UnStream step1 st) v) = do
78− res <- exc $ step1 gst st
78+ res <- ftry $ step1 gst st
79 case res of
79 case res of
80 Right r -> case r of
80 Right r -> case r of
81 Yield x s ->
81 Yield x s ->
@@ -84,7 +84,8 @@ gbracket_ bef exc aft fexc fnormal =
84 Stop -> aft v >> return Stop
84 Stop -> aft v >> return Stop
85 -- XXX Do not handle async exceptions, just rethrow them.
85 -- XXX Do not handle async exceptions, just rethrow them.
87− return $ Skip (GBracketException (fexc v e (UnStream step1 st)))
88+ $ Skip (GBracketException (onExc v e (UnStream step1 st)))
88 step gst (GBracketException (UnStream step1 st)) = do
89 step gst (GBracketException (UnStream step1 st)) = do
89 res <- step1 gst st
90 res <- step1 gst st
90 case res of
91 case res of
@@ -119,13 +120,13 @@ data GbracketIOState s1 s2 v wref
120 :: MonadRunInIO m
121 :: MonadRunInIO m
121 => m c -- ^ before
122 => m c -- ^ before
122− -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
123 -> (c -> m d1) -- ^ on normal stop
123 -> (c -> m d1) -- ^ on normal stop
124− -> (c -> m d2) -- ^ on GC without normal stop or exception
125 -> (c -> e -> Stream m b -> m (Stream m b)) -- ^ on exception
124 -> (c -> e -> Stream m b -> m (Stream m b)) -- ^ on exception
125+ -> (c -> m d2) -- ^ on GC without normal stop or exception
126+ -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
126 -> (c -> Stream m b) -- ^ stream generator
127 -> (c -> Stream m b) -- ^ stream generator
127 -> Stream m b
128 -> Stream m b
128−gbracket bef exc aft gc fexc fnormal =
129+gbracket bef aft onExc onGC ftry action =
129 Stream step GBracketIOInit
130 Stream step GBracketIOInit
@@ -141,12 +142,12 @@ gbracket bef exc aft gc fexc fnormal =
141 -- Tutorial: https://markkarpov.com/tutorial/exceptions.html
142 -- Tutorial: https://markkarpov.com/tutorial/exceptions.html
142 (r, ref) <- withRunInIO $ \run -> mask_ $ run $ do
143 (r, ref) <- withRunInIO $ \run -> mask_ $ run $ do
144− ref <- newIOFinalizer (gc r)
145+ ref <- newIOFinalizer (onGC r)
145 return (r, ref)
146 return (r, ref)
146− return $ Skip $ GBracketIONormal (fnormal r) r ref
147+ return $ Skip $ GBracketIONormal (action r) r ref
148 step gst (GBracketIONormal (UnStream step1 st) v ref) = do
149 step gst (GBracketIONormal (UnStream step1 st) v ref) = do
149− res <- exc $ step1 gst st
150+ res <- ftry $ step1 gst st
150 case res of
151 case res of
151 Right r -> case r of
152 Right r -> case r of
152 Yield x s ->
153 Yield x s ->
@@ -161,7 +162,8 @@ gbracket bef exc aft gc fexc fnormal =
161 -- be atomic wrt async exceptions. Otherwise if we have cleared
162 -- be atomic wrt async exceptions. Otherwise if we have cleared
162 -- the finalizer and have not run the exception handler then we
163 -- the finalizer and have not run the exception handler then we
163 -- may leak the resource.
164 -- may leak the resource.
164− stream <- clearingIOFinalizer ref (fexc v e (UnStream step1 st))
166+ clearingIOFinalizer ref (onExc v e (UnStream step1 st))
165 return $ Skip (GBracketIOException stream)
167 return $ Skip (GBracketIOException stream)
166 step gst (GBracketIOException (UnStream step1 st)) = do
168 step gst (GBracketIOException (UnStream step1 st)) = do
167 res <- step1 gst st
169 res <- step1 gst st
@@ -234,10 +236,13 @@ after action (Stream step state) = Stream step' Nothing
235{-# INLINE_NORMAL onException #-}
237{-# INLINE_NORMAL onException #-}
236onException :: MonadCatch m => m b -> Stream m a -> Stream m a
238onException :: MonadCatch m => m b -> Stream m a -> Stream m a
237−onException action str =
239+onException action stream =
238− gbracket_ (return ()) (inline MC.try) return
240+ gbracket_
241+ (return ()) -- before
239 (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
243 (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
240− (const str)
245+ (const stream)
242{-# INLINE_NORMAL _onException #-}
247{-# INLINE_NORMAL _onException #-}
243_onException :: MonadCatch m => m b -> Stream m a -> Stream m a
248_onException :: MonadCatch m => m b -> Stream m a -> Stream m a
@@ -259,8 +264,11 @@ _onException action (Stream step state) = Stream step' state
259bracket_ :: MonadCatch m
264bracket_ :: MonadCatch m
260 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
265 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
261bracket_ bef aft =
266bracket_ bef aft =
262− gbracket_ bef (inline MC.try) aft
267+ gbracket_
263 (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e))
270 (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e))
265-- | See 'Streamly.Internal.Data.Stream.IsStream.bracket'.
273-- | See 'Streamly.Internal.Data.Stream.IsStream.bracket'.
@@ -272,9 +280,13 @@ bracket' :: (MonadAsync m, MonadCatch m) =>
272 -> (b -> m e)
280 -> (b -> m e)
273 -> (b -> Stream m a)
281 -> (b -> Stream m a)
274 -> Stream m a
282 -> Stream m a
275−bracket' bef aft exc gc =
283+bracket' bef aft onExc onGC =
276− gbracket bef (inline MC.try) aft gc
284+ gbracket
277− (\a (e :: SomeException) _ -> exc a >> return (nilM (MC.throwM e)))
287+ (\a (e :: SomeException) _ -> onExc a >> return (nilM (MC.throwM e)))
279data BracketState s v = BracketInit | BracketRun s v
291data BracketState s v = BracketInit | BracketRun s v
@@ -322,16 +334,16 @@ finally action xs = bracket' (return ()) act act act (const xs)
322{-# INLINE_NORMAL ghandle #-}
334{-# INLINE_NORMAL ghandle #-}
323ghandle :: (MonadCatch m, Exception e)
335ghandle :: (MonadCatch m, Exception e)
324 => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
336 => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
325−ghandle f str =
337+ghandle f stream =
326− gbracket_ (return ()) (inline MC.try) return (const f) (const str)
338+ gbracket_ (return ()) return (const f) (inline MC.try) (const stream)
328-- | See 'Streamly.Internal.Data.Stream.IsStream.handle'.
340-- | See 'Streamly.Internal.Data.Stream.IsStream.handle'.
330{-# INLINE_NORMAL handle #-}
342{-# INLINE_NORMAL handle #-}
331handle :: (MonadCatch m, Exception e)
343handle :: (MonadCatch m, Exception e)
332 => (e -> Stream m a) -> Stream m a -> Stream m a
344 => (e -> Stream m a) -> Stream m a -> Stream m a
333−handle f str =
345+handle f stream =
334− gbracket_ (return ()) (inline MC.try) return (\_ e _ -> f e) (const str)
346+ gbracket_ (return ()) return (\_ e _ -> f e) (inline MC.try) (const stream)
336-- | Alternate (custom) implementation of 'handle'.
348-- | Alternate (custom) implementation of 'handle'.