on master help sign in
Reorder gbracket arguments
commit4428641646b7cb53789a083478f744e61f78e459 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateMon 21 Mar 2022, 12:15 (5 years ago) parent7e82570
2 files changed+38−26
core/src/Streamly/Internal/Data/Stream/StreamD/Exception.hs+35−23 core/src/Streamly/Internal/Data/Unfold.hs+3−3
core/src/Streamly/Internal/Data/Stream/StreamD/Exception.hs+35−23
@@ -59,12 +59,12 @@ data GbracketState s1 s2 v
5959gbracket_
6060 :: Monad m
6161 => m c -- ^ before
62− -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
6362 -> (c -> m d) -- ^ after, on normal stop
6463 -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
64+ -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
6565 -> (c -> Stream m b) -- ^ stream generator
6666 -> Stream m b
67−gbracket_ bef exc aft fexc fnormal =
67+gbracket_ bef aft onExc ftry action =
6868 Stream step GBracketInit
6969
7070 where
@@ -72,10 +72,10 @@ gbracket_ bef exc aft fexc fnormal =
7272 {-# INLINE_LATE step #-}
7373 step _ GBracketInit = do
7474 r <- bef
75− return $ Skip $ GBracketNormal (fnormal r) r
75+ return $ Skip $ GBracketNormal (action r) r
7676
7777 step gst (GBracketNormal (UnStream step1 st) v) = do
78− res <- exc $ step1 gst st
78+ res <- ftry $ step1 gst st
7979 case res of
8080 Right r -> case r of
8181 Yield x s ->
@@ -84,7 +84,8 @@ gbracket_ bef exc aft fexc fnormal =
8484 Stop -> aft v >> return Stop
8585 -- XXX Do not handle async exceptions, just rethrow them.
8686 Left e ->
87− return $ Skip (GBracketException (fexc v e (UnStream step1 st)))
87+ return
88+ $ Skip (GBracketException (onExc v e (UnStream step1 st)))
8889 step gst (GBracketException (UnStream step1 st)) = do
8990 res <- step1 gst st
9091 case res of
@@ -119,13 +120,13 @@ data GbracketIOState s1 s2 v wref
119120gbracket
120121 :: MonadRunInIO m
121122 => m c -- ^ before
122− -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
123123 -> (c -> m d1) -- ^ on normal stop
124− -> (c -> m d2) -- ^ on GC without normal stop or exception
125124 -> (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)
126127 -> (c -> Stream m b) -- ^ stream generator
127128 -> Stream m b
128−gbracket bef exc aft gc fexc fnormal =
129+gbracket bef aft onExc onGC ftry action =
129130 Stream step GBracketIOInit
130131
131132 where
@@ -141,12 +142,12 @@ gbracket bef exc aft gc fexc fnormal =
141142 -- Tutorial: https://markkarpov.com/tutorial/exceptions.html
142143 (r, ref) <- withRunInIO $ \run -> mask_ $ run $ do
143144 r <- bef
144− ref <- newIOFinalizer (gc r)
145+ ref <- newIOFinalizer (onGC r)
145146 return (r, ref)
146− return $ Skip $ GBracketIONormal (fnormal r) r ref
147+ return $ Skip $ GBracketIONormal (action r) r ref
147148
148149 step gst (GBracketIONormal (UnStream step1 st) v ref) = do
149− res <- exc $ step1 gst st
150+ res <- ftry $ step1 gst st
150151 case res of
151152 Right r -> case r of
152153 Yield x s ->
@@ -161,7 +162,8 @@ gbracket bef exc aft gc fexc fnormal =
161162 -- be atomic wrt async exceptions. Otherwise if we have cleared
162163 -- the finalizer and have not run the exception handler then we
163164 -- may leak the resource.
164− stream <- clearingIOFinalizer ref (fexc v e (UnStream step1 st))
165+ stream <-
166+ clearingIOFinalizer ref (onExc v e (UnStream step1 st))
165167 return $ Skip (GBracketIOException stream)
166168 step gst (GBracketIOException (UnStream step1 st)) = do
167169 res <- step1 gst st
@@ -234,10 +236,13 @@ after action (Stream step state) = Stream step' Nothing
234236--
235237{-# INLINE_NORMAL onException #-}
236238onException :: MonadCatch m => m b -> Stream m a -> Stream m a
237−onException action str =
238− gbracket_ (return ()) (inline MC.try) return
239+onException action stream =
240+ gbracket_
241+ (return ()) -- before
242+ return -- after
239243 (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
240− (const str)
244+ (inline MC.try)
245+ (const stream)
241246
242247{-# INLINE_NORMAL _onException #-}
243248_onException :: MonadCatch m => m b -> Stream m a -> Stream m a
@@ -259,8 +264,11 @@ _onException action (Stream step state) = Stream step' state
259264bracket_ :: MonadCatch m
260265 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
261266bracket_ bef aft =
262− gbracket_ bef (inline MC.try) aft
267+ gbracket_
268+ bef
269+ aft
263270 (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e))
271+ (inline MC.try)
264272
265273-- | See 'Streamly.Internal.Data.Stream.IsStream.bracket'.
266274--
@@ -272,9 +280,13 @@ bracket' :: (MonadAsync m, MonadCatch m) =>
272280 -> (b -> m e)
273281 -> (b -> Stream m a)
274282 -> Stream m a
275−bracket' bef aft exc gc =
276− gbracket bef (inline MC.try) aft gc
277− (\a (e :: SomeException) _ -> exc a >> return (nilM (MC.throwM e)))
283+bracket' bef aft onExc onGC =
284+ gbracket
285+ bef
286+ aft
287+ (\a (e :: SomeException) _ -> onExc a >> return (nilM (MC.throwM e)))
288+ onGC
289+ (inline MC.try)
278290
279291data BracketState s v = BracketInit | BracketRun s v
280292
@@ -322,16 +334,16 @@ finally action xs = bracket' (return ()) act act act (const xs)
322334{-# INLINE_NORMAL ghandle #-}
323335ghandle :: (MonadCatch m, Exception e)
324336 => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
325−ghandle f str =
326− gbracket_ (return ()) (inline MC.try) return (const f) (const str)
337+ghandle f stream =
338+ gbracket_ (return ()) return (const f) (inline MC.try) (const stream)
327339
328340-- | See 'Streamly.Internal.Data.Stream.IsStream.handle'.
329341--
330342{-# INLINE_NORMAL handle #-}
331343handle :: (MonadCatch m, Exception e)
332344 => (e -> Stream m a) -> Stream m a -> Stream m a
333−handle f str =
334− gbracket_ (return ()) (inline MC.try) return (\_ e _ -> f e) (const str)
345+handle f stream =
346+ gbracket_ (return ()) return (\_ e _ -> f e) (inline MC.try) (const stream)
335347
336348-- | Alternate (custom) implementation of 'handle'.
337349--
core/src/Streamly/Internal/Data/Unfold.hs+3−3
@@ -885,12 +885,12 @@ gbracket_ bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
885885gbracket
886886 :: MonadRunInIO m
887887 => (a -> m c) -- ^ before
888− -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
889888 -> (c -> m d) -- ^ after, on normal stop, or GC
890889 -> Unfold m (c, e) b -- ^ on exception
890+ -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
891891 -> Unfold m c b -- ^ unfold to run
892892 -> Unfold m a b
893−gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
893+gbracket bef aft (Unfold estep einject) ftry (Unfold step1 inject1) =
894894 Unfold step inject
895895
896896 where
@@ -907,7 +907,7 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
907907
908908 {-# INLINE_LATE step #-}
909909 step (Right (st, v, ref)) = do
910− res <- exc $ step1 st
910+ res <- ftry $ step1 st
911911 case res of
912912 Right r -> case r of
913913 Yield x s -> return $ Yield x (Right (s, v, ref))