on master help sign in
Reorder gbracket arguments
4428641Ranjeet Kumar Ranjan
core/src/Streamly/Internal/Data/Stream/StreamD/Exception.hs+35−23
@@ -59,12 +59,12 @@ data GbracketState s1 s2 v
59gbracket_
59gbracket_
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
69
69
70 where
70 where
@@ -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
74 r <- bef
74 r <- bef
75− return $ Skip $ GBracketNormal (fnormal r) r
75+ return $ Skip $ GBracketNormal (action r) r
76
76
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.
86 Left e ->
86 Left e ->
87+ return
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
119gbracket
120gbracket
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
130
131
131 where
132 where
@@ -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
143 r <- bef
144 r <- bef
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
147
148
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.
165+ stream <-
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
234--
236--
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
242+ return -- after
239 (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
243 (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
244+ (inline MC.try)
240− (const str)
245+ (const stream)
241
246
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_
268+ bef
269+ aft
263 (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e))
270 (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e))
271+ (inline MC.try)
264
272
265-- | See 'Streamly.Internal.Data.Stream.IsStream.bracket'.
273-- | See 'Streamly.Internal.Data.Stream.IsStream.bracket'.
266--
274--
@@ -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
285+ bef
286+ aft
277− (\a (e :: SomeException) _ -> exc a >> return (nilM (MC.throwM e)))
287+ (\a (e :: SomeException) _ -> onExc a >> return (nilM (MC.throwM e)))
288+ onGC
289+ (inline MC.try)
278
290
279data BracketState s v = BracketInit | BracketRun s v
291data BracketState s v = BracketInit | BracketRun s v
280
292
@@ -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)
327
339
328-- | See 'Streamly.Internal.Data.Stream.IsStream.handle'.
340-- | See 'Streamly.Internal.Data.Stream.IsStream.handle'.
329--
341--
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)
335
347
336-- | Alternate (custom) implementation of 'handle'.
348-- | Alternate (custom) implementation of 'handle'.
337--
349--
core/src/Streamly/Internal/Data/Unfold.hs+3−3
@@ -885,12 +885,12 @@ gbracket_ bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
885gbracket
885gbracket
886 :: MonadRunInIO m
886 :: MonadRunInIO m
887 => (a -> m c) -- ^ before
887 => (a -> m c) -- ^ before
888− -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
889 -> (c -> m d) -- ^ after, on normal stop, or GC
888 -> (c -> m d) -- ^ after, on normal stop, or GC
890 -> Unfold m (c, e) b -- ^ on exception
889 -> Unfold m (c, e) b -- ^ on exception
890+ -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
891 -> Unfold m c b -- ^ unfold to run
891 -> Unfold m c b -- ^ unfold to run
892 -> Unfold m a b
892 -> 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) =
894 Unfold step inject
894 Unfold step inject
895
895
896 where
896 where
@@ -907,7 +907,7 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
907
907
908 {-# INLINE_LATE step #-}
908 {-# INLINE_LATE step #-}
909 step (Right (st, v, ref)) = do
909 step (Right (st, v, ref)) = do
910− res <- exc $ step1 st
910+ res <- ftry $ step1 st
911 case res of
911 case res of
912 Right r -> case r of
912 Right r -> case r of
913 Yield x s -> return $ Yield x (Right (s, v, ref))
913 Yield x s -> return $ Yield x (Right (s, v, ref))