on master help sign in
Change the signature of "handle"
commit52d09f11fa1235c2fccdce35c045bbef3760f0fa authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateTue 1 Aug 2023, 18:30 (3 years ago) parent9bf458f
4 files changed+15−11
benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+2−1 core/docs/Changelog.md+3 core/src/Streamly/Internal/Data/Stream/StreamD/Exception.hs+8−8 src/Streamly/Internal/Data/Stream/IsStream/Exception.hs+2−2
benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+2−1
@@ -177,7 +177,8 @@ inspect $ hasNoTypeClasses 'readWriteOnExceptionStream
177177-- | Send the file contents to /dev/null with exception handling
178178readWriteHandleExceptionStream :: Handle -> Handle -> IO ()
179179readWriteHandleExceptionStream inh devNull =
180− let handler (_e :: SomeException) = Stream.fromEffect (hClose inh >> return 10)
180+ let handler (_e :: SomeException) =
181+ return $ Stream.fromEffect (hClose inh >> return 10)
181182 readEx = Stream.handle handler (Stream.unfold FH.reader inh)
182183 in Stream.fold (FH.write devNull) readEx
183184
core/docs/Changelog.md+3
@@ -8,6 +8,9 @@
88 programmers can choose to create them pinned to avoid a copy. To
99 create pinned arrays, use the internal APIs with the `pinned*` prefix.
1010
11+* Changed the signature of 'Streamly.Data.Stream.handle' to make the exception
12+ handler monadic.
13+
1114### Deprecations
1215
1316* `Streamly.Data.MutArray.newUnpinned` is renamed to
core/src/Streamly/Internal/Data/Stream/StreamD/Exception.hs+8−8
@@ -60,7 +60,7 @@ gbracket_
6060 :: Monad m
6161 => m c -- ^ before
6262 -> (c -> m d) -- ^ after, on normal stop
63− -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
63+ -> (c -> e -> Stream m b -> m (Stream m b)) -- ^ on exception
6464 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
6565 -> (c -> Stream m b) -- ^ stream generator
6666 -> Stream m b
@@ -83,9 +83,9 @@ gbracket_ bef aft onExc ftry action =
8383 Skip s -> return $ Skip (GBracketNormal (Stream step1 s) v)
8484 Stop -> aft v >> return Stop
8585 -- XXX Do not handle async exceptions, just rethrow them.
86− Left e ->
87− return
88− $ Skip (GBracketException (onExc v e (UnStream step1 st)))
86+ Left e -> do
87+ strm <- onExc v e (UnStream step1 st)
88+ return $ Skip (GBracketException strm)
8989 step gst (GBracketException (UnStream step1 st)) = do
9090 res <- step1 gst st
9191 case res of
@@ -265,7 +265,7 @@ onException action stream =
265265 gbracket_
266266 (return ()) -- before
267267 return -- after
268− (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
268+ (\_ (e :: MC.SomeException) _ -> action >> MC.throwM e)
269269 (inline MC.try)
270270 (const stream)
271271
@@ -301,7 +301,7 @@ bracketUnsafe bef aft =
301301 gbracket_
302302 bef
303303 aft
304− (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e))
304+ (\a (e :: SomeException) _ -> aft a >> MC.throwM e)
305305 (inline MC.try)
306306
307307-- For a use case of this see the "streamly-process" package. It needs to kill
@@ -437,7 +437,7 @@ finallyIO action xs = bracketIO3 (return ()) act act act (const xs)
437437--
438438{-# INLINE_NORMAL ghandle #-}
439439ghandle :: (MonadCatch m, Exception e)
440− => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
440+ => (e -> Stream m a -> m (Stream m a)) -> Stream m a -> Stream m a
441441ghandle f stream =
442442 gbracket_ (return ()) return (const f) (inline MC.try) (const stream)
443443
@@ -448,7 +448,7 @@ ghandle f stream =
448448--
449449{-# INLINE_NORMAL handle #-}
450450handle :: (MonadCatch m, Exception e)
451− => (e -> Stream m a) -> Stream m a -> Stream m a
451+ => (e -> m (Stream m a)) -> Stream m a -> Stream m a
452452handle f stream =
453453 gbracket_ (return ()) return (\_ e _ -> f e) (inline MC.try) (const stream)
454454
src/Streamly/Internal/Data/Stream/IsStream/Exception.hs+2−2
@@ -225,7 +225,7 @@ ghandle :: (IsStream t, MonadCatch m, Exception e)
225225 => (e -> t m a -> t m a) -> t m a -> t m a
226226ghandle handler =
227227 fromStreamD
228− . D.ghandle (\e xs -> toStreamD $ handler e (fromStreamD xs))
228+ . D.ghandle (\e xs -> return $ toStreamD $ handler e (fromStreamD xs))
229229 . toStreamD
230230
231231-- | When evaluating a stream if an exception occurs, stream evaluation aborts
@@ -238,7 +238,7 @@ ghandle handler =
238238handle :: (IsStream t, MonadCatch m, Exception e)
239239 => (e -> t m a) -> t m a -> t m a
240240handle handler xs =
241− fromStreamD $ D.handle (toStreamD . handler) $ toStreamD xs
241+ fromStreamD $ D.handle (return . toStreamD . handler) $ toStreamD xs
242242
243243
244244-- | @retry@ takes 3 arguments