on master help sign in
Change the signature of "handle"
52d09f1Ranjeet Kumar Ranjan
benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+2−1
@@ -177,7 +177,8 @@ inspect $ hasNoTypeClasses 'readWriteOnExceptionStream
177-- | Send the file contents to /dev/null with exception handling
177-- | Send the file contents to /dev/null with exception handling
178readWriteHandleExceptionStream :: Handle -> Handle -> IO ()
178readWriteHandleExceptionStream :: Handle -> Handle -> IO ()
179readWriteHandleExceptionStream inh devNull =
179readWriteHandleExceptionStream inh devNull =
180+ let handler (_e :: SomeException) =
180− let handler (_e :: SomeException) = Stream.fromEffect (hClose inh >> return 10)
181+ return $ Stream.fromEffect (hClose inh >> return 10)
181 readEx = Stream.handle handler (Stream.unfold FH.reader inh)
182 readEx = Stream.handle handler (Stream.unfold FH.reader inh)
182 in Stream.fold (FH.write devNull) readEx
183 in Stream.fold (FH.write devNull) readEx
183
184
core/docs/Changelog.md+3
@@ -8,6 +8,9 @@
8 programmers can choose to create them pinned to avoid a copy. To
8 programmers can choose to create them pinned to avoid a copy. To
9 create pinned arrays, use the internal APIs with the `pinned*` prefix.
9 create pinned arrays, use the internal APIs with the `pinned*` prefix.
10
10
11+* Changed the signature of 'Streamly.Data.Stream.handle' to make the exception
12+ handler monadic.
13+
11### Deprecations
14### Deprecations
12
15
13* `Streamly.Data.MutArray.newUnpinned` is renamed to
16* `Streamly.Data.MutArray.newUnpinned` is renamed to
core/src/Streamly/Internal/Data/Stream/StreamD/Exception.hs+8−8
@@ -60,7 +60,7 @@ gbracket_
60 :: Monad m
60 :: Monad m
61 => m c -- ^ before
61 => m c -- ^ before
62 -> (c -> m d) -- ^ after, on normal stop
62 -> (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
64 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
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
@@ -83,9 +83,9 @@ gbracket_ bef aft onExc ftry action =
83 Skip s -> return $ Skip (GBracketNormal (Stream step1 s) v)
83 Skip s -> return $ Skip (GBracketNormal (Stream step1 s) v)
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 -> do
87− return
88− $ Skip (GBracketException (onExc v e (UnStream step1 st)))
87+ strm <- onExc v e (UnStream step1 st)
88+ return $ Skip (GBracketException strm)
89 step gst (GBracketException (UnStream step1 st)) = do
89 step gst (GBracketException (UnStream step1 st)) = do
90 res <- step1 gst st
90 res <- step1 gst st
91 case res of
91 case res of
@@ -265,7 +265,7 @@ onException action stream =
265 gbracket_
265 gbracket_
266 (return ()) -- before
266 (return ()) -- before
267 return -- after
267 return -- after
268− (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
268+ (\_ (e :: MC.SomeException) _ -> action >> MC.throwM e)
269 (inline MC.try)
269 (inline MC.try)
270 (const stream)
270 (const stream)
271
271
@@ -301,7 +301,7 @@ bracketUnsafe bef aft =
301 gbracket_
301 gbracket_
302 bef
302 bef
303 aft
303 aft
304− (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e))
304+ (\a (e :: SomeException) _ -> aft a >> MC.throwM e)
305 (inline MC.try)
305 (inline MC.try)
306
306
307-- For a use case of this see the "streamly-process" package. It needs to kill
307-- 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)
437--
437--
438{-# INLINE_NORMAL ghandle #-}
438{-# INLINE_NORMAL ghandle #-}
439ghandle :: (MonadCatch m, Exception e)
439ghandle :: (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
441ghandle f stream =
441ghandle f stream =
442 gbracket_ (return ()) return (const f) (inline MC.try) (const stream)
442 gbracket_ (return ()) return (const f) (inline MC.try) (const stream)
443
443
@@ -448,7 +448,7 @@ ghandle f stream =
448--
448--
449{-# INLINE_NORMAL handle #-}
449{-# INLINE_NORMAL handle #-}
450handle :: (MonadCatch m, Exception e)
450handle :: (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
452handle f stream =
452handle f stream =
453 gbracket_ (return ()) return (\_ e _ -> f e) (inline MC.try) (const stream)
453 gbracket_ (return ()) return (\_ e _ -> f e) (inline MC.try) (const stream)
454
454
src/Streamly/Internal/Data/Stream/IsStream/Exception.hs+2−2
@@ -225,7 +225,7 @@ ghandle :: (IsStream t, MonadCatch m, Exception e)
225 => (e -> t m a -> t m a) -> t m a -> t m a
225 => (e -> t m a -> t m a) -> t m a -> t m a
226ghandle handler =
226ghandle handler =
227 fromStreamD
227 fromStreamD
228− . D.ghandle (\e xs -> toStreamD $ handler e (fromStreamD xs))
228+ . D.ghandle (\e xs -> return $ toStreamD $ handler e (fromStreamD xs))
229 . toStreamD
229 . toStreamD
230
230
231-- | When evaluating a stream if an exception occurs, stream evaluation aborts
231-- | When evaluating a stream if an exception occurs, stream evaluation aborts
@@ -238,7 +238,7 @@ ghandle handler =
238handle :: (IsStream t, MonadCatch m, Exception e)
238handle :: (IsStream t, MonadCatch m, Exception e)
239 => (e -> t m a) -> t m a -> t m a
239 => (e -> t m a) -> t m a -> t m a
240handle handler xs =
240handle handler xs =
241− fromStreamD $ D.handle (toStreamD . handler) $ toStreamD xs
241+ fromStreamD $ D.handle (return . toStreamD . handler) $ toStreamD xs
242
242
243
243
244-- | @retry@ takes 3 arguments
244-- | @retry@ takes 3 arguments