on master help sign in
Make the finalizer based exception handling routines the default.
960def1Pranay Sashank
Changelog.md+6
@@ -6,6 +6,10 @@
6 accumulator monadic.
6 accumulator monadic.
7* Change the signature of `concatMapWith` to ensure that it can be
7* Change the signature of `concatMapWith` to ensure that it can be
8 used with a wide variety of combining functions.
8 used with a wide variety of combining functions.
9+* Exception handling functions `bracket`, `handle`, `finally` now
10+ require an additional `MonadAsync` constraint. Several other
11+ functions that used these functions also now require the additional
12+ constraint.
9* Change the associativity of combinators `serial`, `wSerial`,
13* Change the associativity of combinators `serial`, `wSerial`,
10 `ahead`, `async`, `wAsync`, `parallel` to be the same as `<>`.
14 `ahead`, `async`, `wAsync`, `parallel` to be the same as `<>`.
11* Drop support for GHC 7.10.3.
15* Drop support for GHC 7.10.3.
@@ -15,6 +19,8 @@
15* `accept*` and `connect` APIs in `Streamly.Network.Inet.TCP` and the `accept`
19* `accept*` and `connect` APIs in `Streamly.Network.Inet.TCP` and the `accept`
16 API in `Streamly.Network.Socket` now close the socket if an exception is
20 API in `Streamly.Network.Socket` now close the socket if an exception is
17 thrown.
21 thrown.
22+* `bracket`, `handle`, and `finally` now work on streams that aren't
23+ fully drained.
18
24
19## 0.7.2
25## 0.7.2
20
26
benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+54−54
@@ -110,19 +110,19 @@ inspect $ hasNoTypeClasses 'readChunksOnException
110#endif
110#endif
111
111
112-- | Send the file contents to /dev/null with exception handling
112-- | Send the file contents to /dev/null with exception handling
113−readChunksBracket :: Handle -> Handle -> IO ()
113+readChunksBracket_ :: Handle -> Handle -> IO ()
114−readChunksBracket inh devNull =
114+readChunksBracket_ inh devNull =
115− let readEx = IUF.bracket return (\_ -> hClose inh) FH.readChunks
115+ let readEx = IUF.bracket_ return (\_ -> hClose inh) FH.readChunks
116 in IUF.fold readEx (IFH.writeChunks devNull) inh
116 in IUF.fold readEx (IFH.writeChunks devNull) inh
117
117
118#ifdef INSPECTION
118#ifdef INSPECTION
119−inspect $ hasNoTypeClasses 'readChunksBracket
119+inspect $ hasNoTypeClasses 'readChunksBracket_
120-- inspect $ 'readChunksBracket `hasNoType` ''Step
120-- inspect $ 'readChunksBracket `hasNoType` ''Step
121#endif
121#endif
122
122
123−readChunksBracketIO :: Handle -> Handle -> IO ()
123+readChunksBracket :: Handle -> Handle -> IO ()
124−readChunksBracketIO inh devNull =
124+readChunksBracket inh devNull =
125− let readEx = IUF.bracketIO return (\_ -> hClose inh) FH.readChunks
125+ let readEx = IUF.bracket return (\_ -> hClose inh) FH.readChunks
126 in IUF.fold readEx (IFH.writeChunks devNull) inh
126 in IUF.fold readEx (IFH.writeChunks devNull) inh
127
127
128o_1_space_copy_exceptions_readChunks :: BenchEnv -> [Benchmark]
128o_1_space_copy_exceptions_readChunks :: BenchEnv -> [Benchmark]
@@ -130,10 +130,10 @@ o_1_space_copy_exceptions_readChunks env =
130 [ bgroup "copy/readChunks/exceptions"
130 [ bgroup "copy/readChunks/exceptions"
131 [ mkBench "UF.onException" env $ \inH _ ->
131 [ mkBench "UF.onException" env $ \inH _ ->
132 readChunksOnException inH (nullH env)
132 readChunksOnException inH (nullH env)
133+ , mkBench "UF.bracket_" env $ \inH _ ->
134+ readChunksBracket_ inH (nullH env)
133 , mkBench "UF.bracket" env $ \inH _ ->
135 , mkBench "UF.bracket" env $ \inH _ ->
134 readChunksBracket inH (nullH env)
136 readChunksBracket inH (nullH env)
135− , mkBench "UF.bracketIO" env $ \inH _ ->
136− readChunksBracketIO inH (nullH env)
137 ]
137 ]
138 ]
138 ]
139
139
@@ -142,22 +142,22 @@ o_1_space_copy_exceptions_readChunks env =
142-------------------------------------------------------------------------------
142-------------------------------------------------------------------------------
143
143
144-- | Send the file contents to /dev/null with exception handling
144-- | Send the file contents to /dev/null with exception handling
145−toChunksBracket :: Handle -> Handle -> IO ()
145+toChunksBracket_ :: Handle -> Handle -> IO ()
146−toChunksBracket inh devNull =
146+toChunksBracket_ inh devNull =
147− let readEx = S.bracket
147+ let readEx = IP.bracket_
148 (return ())
148 (return ())
149 (\_ -> hClose inh)
149 (\_ -> hClose inh)
150 (\_ -> IFH.toChunks inh)
150 (\_ -> IFH.toChunks inh)
151 in S.fold (IFH.writeChunks devNull) $ readEx
151 in S.fold (IFH.writeChunks devNull) $ readEx
152
152
153#ifdef INSPECTION
153#ifdef INSPECTION
154−inspect $ hasNoTypeClasses 'toChunksBracket
154+inspect $ hasNoTypeClasses 'toChunksBracket_
155-- inspect $ 'toChunksBracket `hasNoType` ''Step
155-- inspect $ 'toChunksBracket `hasNoType` ''Step
156#endif
156#endif
157
157
158−toChunksBracketIO :: Handle -> Handle -> IO ()
158+toChunksBracket :: Handle -> Handle -> IO ()
159−toChunksBracketIO inh devNull =
159+toChunksBracket inh devNull =
160− let readEx = IP.bracketIO
160+ let readEx = S.bracket
161 (return ())
161 (return ())
162 (\_ -> hClose inh)
162 (\_ -> hClose inh)
163 (\_ -> IFH.toChunks inh)
163 (\_ -> IFH.toChunks inh)
@@ -166,10 +166,10 @@ toChunksBracketIO inh devNull =
166o_1_space_copy_exceptions_toChunks :: BenchEnv -> [Benchmark]
166o_1_space_copy_exceptions_toChunks :: BenchEnv -> [Benchmark]
167o_1_space_copy_exceptions_toChunks env =
167o_1_space_copy_exceptions_toChunks env =
168 [ bgroup "copy/toChunks/exceptions"
168 [ bgroup "copy/toChunks/exceptions"
169+ [ mkBench "S.bracket_" env $ \inH _ ->
170+ toChunksBracket_ inH (nullH env)
169− [ mkBench "S.bracket" env $ \inH _ ->
171+ , mkBench "S.bracket" env $ \inH _ ->
170 toChunksBracket inH (nullH env)
172 toChunksBracket inH (nullH env)
171− , mkBench "S.bracketIO" env $ \inH _ ->
172− toChunksBracketIO inH (nullH env)
173 ]
173 ]
174 ]
174 ]
175
175
@@ -308,35 +308,35 @@ inspect $ hasNoTypeClasses 'readWriteHandleExceptionUnfold
308#endif
308#endif
309
309
310-- | Send the file contents to /dev/null with exception handling
310-- | Send the file contents to /dev/null with exception handling
311−readWriteFinallyUnfold :: Handle -> Handle -> IO ()
311+readWriteFinally_Unfold :: Handle -> Handle -> IO ()
312−readWriteFinallyUnfold inh devNull =
312+readWriteFinally_Unfold inh devNull =
313− let readEx = IUF.finally (\_ -> hClose inh) FH.read
313+ let readEx = IUF.finally_ (\_ -> hClose inh) FH.read
314 in S.fold (FH.write devNull) $ S.unfold readEx inh
314 in S.fold (FH.write devNull) $ S.unfold readEx inh
315
315
316#ifdef INSPECTION
316#ifdef INSPECTION
317−inspect $ hasNoTypeClasses 'readWriteFinallyUnfold
317+inspect $ hasNoTypeClasses 'readWriteFinally_Unfold
318-- inspect $ 'readWriteFinallyUnfold `hasNoType` ''Step
318-- inspect $ 'readWriteFinallyUnfold `hasNoType` ''Step
319#endif
319#endif
320
320
321−readWriteFinallyIOUnfold :: Handle -> Handle -> IO ()
321+readWriteFinallyUnfold :: Handle -> Handle -> IO ()
322−readWriteFinallyIOUnfold inh devNull =
322+readWriteFinallyUnfold inh devNull =
323− let readEx = IUF.finallyIO (\_ -> hClose inh) FH.read
323+ let readEx = IUF.finally (\_ -> hClose inh) FH.read
324 in S.fold (FH.write devNull) $ S.unfold readEx inh
324 in S.fold (FH.write devNull) $ S.unfold readEx inh
325
325
326-- | Send the file contents to /dev/null with exception handling
326-- | Send the file contents to /dev/null with exception handling
327−readWriteBracketUnfold :: Handle -> Handle -> IO ()
327+readWriteBracket_Unfold :: Handle -> Handle -> IO ()
328−readWriteBracketUnfold inh devNull =
328+readWriteBracket_Unfold inh devNull =
329− let readEx = IUF.bracket return (\_ -> hClose inh) FH.read
329+ let readEx = IUF.bracket_ return (\_ -> hClose inh) FH.read
330 in S.fold (FH.write devNull) $ S.unfold readEx inh
330 in S.fold (FH.write devNull) $ S.unfold readEx inh
331
331
332#ifdef INSPECTION
332#ifdef INSPECTION
333−inspect $ hasNoTypeClasses 'readWriteBracketUnfold
333+inspect $ hasNoTypeClasses 'readWriteBracket_Unfold
334-- inspect $ 'readWriteBracketUnfold `hasNoType` ''Step
334-- inspect $ 'readWriteBracketUnfold `hasNoType` ''Step
335#endif
335#endif
336
336
337−readWriteBracketIOUnfold :: Handle -> Handle -> IO ()
337+readWriteBracketUnfold :: Handle -> Handle -> IO ()
338−readWriteBracketIOUnfold inh devNull =
338+readWriteBracketUnfold inh devNull =
339− let readEx = IUF.bracketIO return (\_ -> hClose inh) FH.read
339+ let readEx = IUF.bracket return (\_ -> hClose inh) FH.read
340 in S.fold (FH.write devNull) $ S.unfold readEx inh
340 in S.fold (FH.write devNull) $ S.unfold readEx inh
341
341
342o_1_space_copy_read_exceptions :: BenchEnv -> [Benchmark]
342o_1_space_copy_read_exceptions :: BenchEnv -> [Benchmark]
@@ -346,14 +346,14 @@ o_1_space_copy_read_exceptions env =
346 readWriteOnExceptionUnfold inh (nullH env)
346 readWriteOnExceptionUnfold inh (nullH env)
347 , mkBenchSmall "UF.handle" env $ \inh _ ->
347 , mkBenchSmall "UF.handle" env $ \inh _ ->
348 readWriteHandleExceptionUnfold inh (nullH env)
348 readWriteHandleExceptionUnfold inh (nullH env)
349+ , mkBenchSmall "UF.finally_" env $ \inh _ ->
350+ readWriteFinally_Unfold inh (nullH env)
349 , mkBenchSmall "UF.finally" env $ \inh _ ->
351 , mkBenchSmall "UF.finally" env $ \inh _ ->
350 readWriteFinallyUnfold inh (nullH env)
352 readWriteFinallyUnfold inh (nullH env)
351− , mkBenchSmall "UF.finallyIO" env $ \inh _ ->
353+ , mkBenchSmall "UF.bracket_" env $ \inh _ ->
352− readWriteFinallyIOUnfold inh (nullH env)
354+ readWriteBracket_Unfold inh (nullH env)
353 , mkBenchSmall "UF.bracket" env $ \inh _ ->
355 , mkBenchSmall "UF.bracket" env $ \inh _ ->
354 readWriteBracketUnfold inh (nullH env)
356 readWriteBracketUnfold inh (nullH env)
355− , mkBenchSmall "UF.bracketIO" env $ \inh _ ->
356− readWriteBracketIOUnfold inh (nullH env)
357 ]
357 ]
358 ]
358 ]
359
359
@@ -375,31 +375,31 @@ readWriteHandleExceptionStream inh devNull =
375 in S.fold (FH.write devNull) $ readEx
375 in S.fold (FH.write devNull) $ readEx
376
376
377-- | Send the file contents to /dev/null with exception handling
377-- | Send the file contents to /dev/null with exception handling
378+readWriteFinally_Stream :: Handle -> Handle -> IO ()
379+readWriteFinally_Stream inh devNull =
380+ let readEx = IP.finally_ (hClose inh) (S.unfold FH.read inh)
381+ in S.fold (FH.write devNull) readEx
382+
378readWriteFinallyStream :: Handle -> Handle -> IO ()
383readWriteFinallyStream :: Handle -> Handle -> IO ()
379readWriteFinallyStream inh devNull =
384readWriteFinallyStream inh devNull =
380 let readEx = S.finally (hClose inh) (S.unfold FH.read inh)
385 let readEx = S.finally (hClose inh) (S.unfold FH.read inh)
381 in S.fold (FH.write devNull) readEx
386 in S.fold (FH.write devNull) readEx
382
387
383−readWriteFinallyIOStream :: Handle -> Handle -> IO ()
384−readWriteFinallyIOStream inh devNull =
385− let readEx = IP.finallyIO (hClose inh) (S.unfold FH.read inh)
386− in S.fold (FH.write devNull) readEx
387−
388-- | Send the file contents to /dev/null with exception handling
388-- | Send the file contents to /dev/null with exception handling
389−fromToBytesBracketStream :: Handle -> Handle -> IO ()
389+fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
390−fromToBytesBracketStream inh devNull =
390+fromToBytesBracket_Stream inh devNull =
391− let readEx = S.bracket (return ()) (\_ -> hClose inh)
391+ let readEx = IP.bracket_ (return ()) (\_ -> hClose inh)
392 (\_ -> IFH.toBytes inh)
392 (\_ -> IFH.toBytes inh)
393 in IFH.fromBytes devNull $ readEx
393 in IFH.fromBytes devNull $ readEx
394
394
395#ifdef INSPECTION
395#ifdef INSPECTION
396−inspect $ hasNoTypeClasses 'fromToBytesBracketStream
396+inspect $ hasNoTypeClasses 'fromToBytesBracket_Stream
397-- inspect $ 'fromToBytesBracketStream `hasNoType` ''Step
397-- inspect $ 'fromToBytesBracketStream `hasNoType` ''Step
398#endif
398#endif
399
399
400−fromToBytesBracketIOStream :: Handle -> Handle -> IO ()
400+fromToBytesBracketStream :: Handle -> Handle -> IO ()
401−fromToBytesBracketIOStream inh devNull =
401+fromToBytesBracketStream inh devNull =
402− let readEx = IP.bracketIO (return ()) (\_ -> hClose inh)
402+ let readEx = S.bracket (return ()) (\_ -> hClose inh)
403 (\_ -> IFH.toBytes inh)
403 (\_ -> IFH.toBytes inh)
404 in IFH.fromBytes devNull $ readEx
404 in IFH.fromBytes devNull $ readEx
405
405
@@ -410,16 +410,16 @@ o_1_space_copy_stream_exceptions env =
410 readWriteOnExceptionStream inh (nullH env)
410 readWriteOnExceptionStream inh (nullH env)
411 , mkBenchSmall "S.handle" env $ \inh _ ->
411 , mkBenchSmall "S.handle" env $ \inh _ ->
412 readWriteHandleExceptionStream inh (nullH env)
412 readWriteHandleExceptionStream inh (nullH env)
413+ , mkBenchSmall "S.finally_" env $ \inh _ ->
414+ readWriteFinally_Stream inh (nullH env)
413 , mkBenchSmall "S.finally" env $ \inh _ ->
415 , mkBenchSmall "S.finally" env $ \inh _ ->
414 readWriteFinallyStream inh (nullH env)
416 readWriteFinallyStream inh (nullH env)
415− , mkBenchSmall "S.finallyIO" env $ \inh _ ->
416− readWriteFinallyIOStream inh (nullH env)
417 ]
417 ]
418 , bgroup "copy/fromToBytes/exceptions"
418 , bgroup "copy/fromToBytes/exceptions"
419+ [ mkBenchSmall "S.bracket_" env $ \inh _ ->
420+ fromToBytesBracket_Stream inh (nullH env)
419− [ mkBenchSmall "S.bracket" env $ \inh _ ->
421+ , mkBenchSmall "S.bracket" env $ \inh _ ->
420 fromToBytesBracketStream inh (nullH env)
422 fromToBytesBracketStream inh (nullH env)
421− , mkBenchSmall "S.bracketIO" env $ \inh _ ->
422− fromToBytesBracketIOStream inh (nullH env)
423 ]
423 ]
424 ]
424 ]
425
425
src/Streamly/Internal/Data/Stream/StreamD.hs+43−35
@@ -282,15 +282,16 @@ module Streamly.Internal.Data.Stream.StreamD
282 , newFinalizedIORef
282 , newFinalizedIORef
283 , runIORefFinalizer
283 , runIORefFinalizer
284 , clearIORefFinalizer
284 , clearIORefFinalizer
285+ , gbracket_
285 , gbracket
286 , gbracket
286 , before
287 , before
288+ , after_
287 , after
289 , after
288− , afterIO
290+ , bracket_
289 , bracket
291 , bracket
290− , bracketIO
291 , onException
292 , onException
293+ , finally_
292 , finally
294 , finally
293− , finallyIO
294 , handle
295 , handle
295
296
296 -- * Concurrent Application
297 -- * Concurrent Application
@@ -3131,8 +3132,8 @@ data GbracketState s1 s2 v
3131--
3132--
3132-- /Internal/
3133-- /Internal/
3133--
3134--
3134−{-# INLINE_NORMAL gbracket #-}
3135+{-# INLINE_NORMAL gbracket_ #-}
3135−gbracket
3136+gbracket_
3136 :: Monad m
3137 :: Monad m
3137 => m c -- ^ before
3138 => m c -- ^ before
3138 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
3139 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
@@ -3140,7 +3141,7 @@ gbracket
3140 -> (c -> e -> Stream m b) -- ^ on exception
3141 -> (c -> e -> Stream m b) -- ^ on exception
3141 -> (c -> Stream m b) -- ^ stream generator
3142 -> (c -> Stream m b) -- ^ stream generator
3142 -> Stream m b
3143 -> Stream m b
3143−gbracket bef exc aft fexc fnormal =
3144+gbracket_ bef exc aft fexc fnormal =
3144 Stream step GBracketInit
3145 Stream step GBracketInit
3145
3146
3146 where
3147 where
@@ -3204,18 +3205,17 @@ data GbracketIOState s1 s2 v wref
3204 | GBracketIONormal s1 v wref
3205 | GBracketIONormal s1 v wref
3205 | GBracketIOException s2
3206 | GBracketIOException s2
3206
3207
3207−-- | Like gbracket but also uses a finalizer to make sure when the stream is
3208−-- garbage collected we run the finalizing action. This requires a MonadIO and
3209−-- MonadBaseControl IO constraint.
3210−--
3211-- | The most general bracketing and exception combinator. All other
3208-- | The most general bracketing and exception combinator. All other
3212-- combinators can be expressed in terms of this combinator. This can also be
3209-- combinators can be expressed in terms of this combinator. This can also be
3213-- used for cases which are not covered by the standard combinators.
3210-- used for cases which are not covered by the standard combinators.
3214--
3211--
3212+-- | It uses a finalizer to make sure we run the finalizing action
3213+-- when the stream is garbage collected.
3214+--
3215-- /Internal/
3215-- /Internal/
3216--
3216--
3217−{-# INLINE_NORMAL gbracketIO #-}
3217+{-# INLINE_NORMAL gbracket #-}
3218−gbracketIO
3218+gbracket
3219 :: (MonadIO m, MonadBaseControl IO m)
3219 :: (MonadIO m, MonadBaseControl IO m)
3220 => m c -- ^ before
3220 => m c -- ^ before
3221 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
3221 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
@@ -3223,7 +3223,7 @@ gbracketIO
3223 -> (c -> e -> Stream m b) -- ^ on exception
3223 -> (c -> e -> Stream m b) -- ^ on exception
3224 -> (c -> Stream m b) -- ^ stream generator
3224 -> (c -> Stream m b) -- ^ stream generator
3225 -> Stream m b
3225 -> Stream m b
3226−gbracketIO bef exc aft fexc fnormal =
3226+gbracket bef exc aft fexc fnormal =
3227 Stream step GBracketIOInit
3227 Stream step GBracketIOInit
3228
3228
3229 where
3229 where
@@ -3283,9 +3283,9 @@ before action (Stream step state) = Stream step' Nothing
3283 Stop -> return Stop
3283 Stop -> return Stop
3284
3284
3285-- | Run a side effect whenever the stream stops normally.
3285-- | Run a side effect whenever the stream stops normally.
3286−{-# INLINE_NORMAL after #-}
3286+{-# INLINE_NORMAL after_ #-}
3287−after :: Monad m => m b -> Stream m a -> Stream m a
3287+after_ :: Monad m => m b -> Stream m a -> Stream m a
3288−after action (Stream step state) = Stream step' state
3288+after_ action (Stream step state) = Stream step' state
3289
3289
3290 where
3290 where
3291
3291
@@ -3297,10 +3297,10 @@ after action (Stream step state) = Stream step' state
3297 Skip s -> return $ Skip s
3297 Skip s -> return $ Skip s
3298 Stop -> action >> return Stop
3298 Stop -> action >> return Stop
3299
3299
3300−{-# INLINE_NORMAL afterIO #-}
3300+{-# INLINE_NORMAL after #-}
3301−afterIO :: (MonadIO m, MonadBaseControl IO m)
3301+after :: (MonadIO m, MonadBaseControl IO m)
3302 => m b -> Stream m a -> Stream m a
3302 => m b -> Stream m a -> Stream m a
3303−afterIO action (Stream step state) = Stream step' Nothing
3303+after action (Stream step state) = Stream step' Nothing
3304
3304
3305 where
3305 where
3306
3306
@@ -3331,7 +3331,7 @@ afterIO action (Stream step state) = Stream step' Nothing
3331{-# INLINE_NORMAL onException #-}
3331{-# INLINE_NORMAL onException #-}
3332onException :: MonadCatch m => m b -> Stream m a -> Stream m a
3332onException :: MonadCatch m => m b -> Stream m a -> Stream m a
3333onException action str =
3333onException action str =
3334− gbracket (return ()) MC.try return
3334+ gbracket_ (return ()) MC.try return
3335 (\_ (e :: MC.SomeException) -> nilM (action >> MC.throwM e))
3335 (\_ (e :: MC.SomeException) -> nilM (action >> MC.throwM e))
3336 (\_ -> str)
3336 (\_ -> str)
3337
3337
@@ -3357,17 +3357,22 @@ _onException action (Stream step state) = Stream step' state
3357-- generate a stream using the output, run the second action providing the
3357-- generate a stream using the output, run the second action providing the
3358-- remembered value as an argument whenever the stream ends normally or due to
3358-- remembered value as an argument whenever the stream ends normally or due to
3359-- an exception.
3359-- an exception.
3360−{-# INLINE_NORMAL bracket #-}
3360+{-# INLINE_NORMAL bracket_ #-}
3361−bracket :: MonadCatch m => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3361+bracket_ :: MonadCatch m => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3362−bracket bef aft bet =
3362+bracket_ bef aft bet =
3363− gbracket bef MC.try aft
3363+ gbracket_ bef MC.try aft
3364 (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
3364 (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
3365
3365
3366+-- | Run the first action before the stream starts and remember its output,
3367+-- generate a stream using the output, run the second action providing the
3368+-- remembered value as an argument whenever the stream ends normally or due to
3369+-- an exception or gets garbage collected.
3370+--
3366−{-# INLINE_NORMAL bracketIO #-}
3371+{-# INLINE_NORMAL bracket #-}
3367−bracketIO :: (MonadAsync m, MonadCatch m)
3372+bracket :: (MonadAsync m, MonadCatch m)
3368 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3373 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
3369−bracketIO bef aft bet =
3374+bracket bef aft bet =
3370− gbracketIO bef MC.try aft
3375+ gbracket bef MC.try aft
3371 (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
3376 (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
3372
3377
3373data BracketState s v = BracketInit | BracketRun s v
3378data BracketState s v = BracketInit | BracketRun s v
@@ -3395,14 +3400,17 @@ _bracket bef aft bet = Stream step' BracketInit
3395
3400
3396-- | Run a side effect whenever the stream stops normally or aborts due to an
3401-- | Run a side effect whenever the stream stops normally or aborts due to an
3397-- exception.
3402-- exception.
3398−{-# INLINE finally #-}
3403+{-# INLINE finally_ #-}
3399−finally :: MonadCatch m => m b -> Stream m a -> Stream m a
3404+finally_ :: MonadCatch m => m b -> Stream m a -> Stream m a
3400-- finally action xs = after action $ onException action xs
3405-- finally action xs = after action $ onException action xs
3401−finally action xs = bracket (return ()) (\_ -> action) (const xs)
3406+finally_ action xs = bracket_ (return ()) (\_ -> action) (const xs)
3402
3407
3408+-- | Run a side effect whenever the stream stops normally or aborts due to an
3409+-- exception or gets garbage collected.
3410+--
3403−{-# INLINE finallyIO #-}
3411+{-# INLINE finally #-}
3404−finallyIO :: (MonadAsync m, MonadCatch m) => m b -> Stream m a -> Stream m a
3412+finally :: (MonadAsync m, MonadCatch m) => m b -> Stream m a -> Stream m a
3405−finallyIO action xs = bracketIO (return ()) (\_ -> action) (const xs)
3413+finally action xs = bracket (return ()) (\_ -> action) (const xs)
3406
3414
3407-- | When evaluating a stream if an exception occurs, stream evaluation aborts
3415-- | When evaluating a stream if an exception occurs, stream evaluation aborts
3408-- and the specified exception handler is run with the exception as argument.
3416-- and the specified exception handler is run with the exception as argument.
@@ -3410,7 +3418,7 @@ finallyIO action xs = bracketIO (return ()) (\_ -> action) (const xs)
3410handle :: (MonadCatch m, Exception e)
3418handle :: (MonadCatch m, Exception e)
3411 => (e -> Stream m a) -> Stream m a -> Stream m a
3419 => (e -> Stream m a) -> Stream m a -> Stream m a
3412handle f str =
3420handle f str =
3413− gbracket (return ()) MC.try return (\_ e -> f e) (\_ -> str)
3421+ gbracket_ (return ()) MC.try return (\_ e -> f e) (\_ -> str)
3414
3422
3415{-# INLINE_NORMAL _handle #-}
3423{-# INLINE_NORMAL _handle #-}
3416_handle :: (MonadCatch m, Exception e)
3424_handle :: (MonadCatch m, Exception e)
src/Streamly/Internal/Data/Unfold.hs+38−38
@@ -111,16 +111,16 @@ module Streamly.Internal.Data.Unfold
111 , apDiscardSnd
111 , apDiscardSnd
112
112
113 -- * Exceptions
113 -- * Exceptions
114+ , gbracket_
114 , gbracket
115 , gbracket
115− , gbracketIO
116 , before
116 , before
117+ , after_
117 , after
118 , after
118− , afterIO
119 , onException
119 , onException
120+ , finally_
120 , finally
121 , finally
121− , finallyIO
122+ , bracket_
122 , bracket
123 , bracket
123− , bracketIO
124 , handle
124 , handle
125 )
125 )
126where
126where
@@ -754,8 +754,8 @@ concatMapM f (Unfold step1 inject1) = Unfold step inject
754--
754--
755-- /Internal/
755-- /Internal/
756--
756--
757−{-# INLINE_NORMAL gbracket #-}
757+{-# INLINE_NORMAL gbracket_ #-}
758−gbracket
758+gbracket_
759 :: Monad m
759 :: Monad m
760 => (a -> m c) -- ^ before
760 => (a -> m c) -- ^ before
761 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
761 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
@@ -763,7 +763,7 @@ gbracket
763 -> Unfold m (c, e) b -- ^ on exception
763 -> Unfold m (c, e) b -- ^ on exception
764 -> Unfold m c b -- ^ unfold to run
764 -> Unfold m c b -- ^ unfold to run
765 -> Unfold m a b
765 -> Unfold m a b
766−gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
766+gbracket_ bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
767 Unfold step inject
767 Unfold step inject
768
768
769 where
769 where
@@ -797,8 +797,8 @@ gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
797--
797--
798-- /Internal/
798-- /Internal/
799--
799--
800−{-# INLINE_NORMAL gbracketIO #-}
800+{-# INLINE_NORMAL gbracket #-}
801−gbracketIO
801+gbracket
802 :: (MonadIO m, MonadBaseControl IO m)
802 :: (MonadIO m, MonadBaseControl IO m)
803 => (a -> m c) -- ^ before
803 => (a -> m c) -- ^ before
804 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
804 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
@@ -806,7 +806,7 @@ gbracketIO
806 -> Unfold m (c, e) b -- ^ on exception
806 -> Unfold m (c, e) b -- ^ on exception
807 -> Unfold m c b -- ^ unfold to run
807 -> Unfold m c b -- ^ unfold to run
808 -> Unfold m a b
808 -> Unfold m a b
809−gbracketIO bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
809+gbracket bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
810 Unfold step inject
810 Unfold step inject
811
811
812 where
812 where
@@ -848,7 +848,7 @@ gbracketIO bef exc aft (Unfold estep einject) (Unfold step1 inject1) =
848--
848--
849{-# INLINE_NORMAL _before #-}
849{-# INLINE_NORMAL _before #-}
850_before :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
850_before :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
851−_before action = gbracket (\x -> action x >> return x) (fmap Right)
851+_before action = gbracket_ (\x -> action x >> return x) (fmap Right)
852 (\_ -> return ()) undefined
852 (\_ -> return ()) undefined
853
853
854-- | Run a side effect before the unfold yields its first element.
854-- | Run a side effect before the unfold yields its first element.
@@ -874,18 +874,18 @@ before action (Unfold step1 inject1) = Unfold step inject
874
874
875{-# INLINE_NORMAL _after #-}
875{-# INLINE_NORMAL _after #-}
876_after :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
876_after :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
877−_after aft = gbracket return (fmap Right) aft undefined
877+_after aft = gbracket_ return (fmap Right) aft undefined
878
878
879-- | Run a side effect whenever the unfold stops normally.
879-- | Run a side effect whenever the unfold stops normally.
880--
880--
881−-- Prefer afterIO over this as the @after@ action in this combinator is not
881+-- Prefer 'after' over this as the @after@ action in this combinator is not
882-- executed if the unfold is partially evaluated lazily and then garbage
882-- executed if the unfold is partially evaluated lazily and then garbage
883-- collected.
883-- collected.
884--
884--
885-- /Internal/
885-- /Internal/
886−{-# INLINE_NORMAL after #-}
886+{-# INLINE_NORMAL after_ #-}
887−after :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
887+after_ :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
888−after action (Unfold step1 inject1) = Unfold step inject
888+after_ action (Unfold step1 inject1) = Unfold step inject
889
889
890 where
890 where
891
891
@@ -905,10 +905,10 @@ after action (Unfold step1 inject1) = Unfold step inject
905-- or is garbage collected after a partial lazy evaluation.
905-- or is garbage collected after a partial lazy evaluation.
906--
906--
907-- /Internal/
907-- /Internal/
908−{-# INLINE_NORMAL afterIO #-}
908+{-# INLINE_NORMAL after #-}
909−afterIO :: (MonadIO m, MonadBaseControl IO m)
909+after :: (MonadIO m, MonadBaseControl IO m)
910 => (a -> m c) -> Unfold m a b -> Unfold m a b
910 => (a -> m c) -> Unfold m a b -> Unfold m a b
911−afterIO action (Unfold step1 inject1) = Unfold step inject
911+after action (Unfold step1 inject1) = Unfold step inject
912
912
913 where
913 where
914
914
@@ -930,7 +930,7 @@ afterIO action (Unfold step1 inject1) = Unfold step inject
930{-# INLINE_NORMAL _onException #-}
930{-# INLINE_NORMAL _onException #-}
931_onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
931_onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
932_onException action =
932_onException action =
933− gbracket return MC.try
933+ gbracket_ return MC.try
934 (\_ -> return ())
934 (\_ -> return ())
935 (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
935 (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
936
936
@@ -958,20 +958,20 @@ onException action (Unfold step1 inject1) = Unfold step inject
958{-# INLINE_NORMAL _finally #-}
958{-# INLINE_NORMAL _finally #-}
959_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
959_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
960_finally action =
960_finally action =
961− gbracket return MC.try action
961+ gbracket_ return MC.try action
962 (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
962 (nilM (\(a, e :: MC.SomeException) -> action a >> MC.throwM e))
963
963
964-- | Run a side effect whenever the unfold stops normally or aborts due to an
964-- | Run a side effect whenever the unfold stops normally or aborts due to an
965-- exception.
965-- exception.
966--
966--
967−-- Prefer finallyIO over this as the @after@ action in this combinator is not
967+-- Prefer 'finally' over this as the @after@ action in this combinator is not
968-- executed if the unfold is partially evaluated lazily and then garbage
968-- executed if the unfold is partially evaluated lazily and then garbage
969-- collected.
969-- collected.
970--
970--
971-- /Internal/
971-- /Internal/
972−{-# INLINE_NORMAL finally #-}
972+{-# INLINE_NORMAL finally_ #-}
973−finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
973+finally_ :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
974−finally action (Unfold step1 inject1) = Unfold step inject
974+finally_ action (Unfold step1 inject1) = Unfold step inject
975
975
976 where
976 where
977
977
@@ -991,10 +991,10 @@ finally action (Unfold step1 inject1) = Unfold step inject
991-- exception or if it is garbage collected after a partial lazy evaluation.
991-- exception or if it is garbage collected after a partial lazy evaluation.
992--
992--
993-- /Internal/
993-- /Internal/
994−{-# INLINE_NORMAL finallyIO #-}
994+{-# INLINE_NORMAL finally #-}
995−finallyIO :: (MonadAsync m, MonadCatch m)
995+finally :: (MonadAsync m, MonadCatch m)
996 => (a -> m c) -> Unfold m a b -> Unfold m a b
996 => (a -> m c) -> Unfold m a b -> Unfold m a b
997−finallyIO action (Unfold step1 inject1) = Unfold step inject
997+finally action (Unfold step1 inject1) = Unfold step inject
998
998
999 where
999 where
1000
1000
@@ -1017,23 +1017,23 @@ finallyIO action (Unfold step1 inject1) = Unfold step inject
1017_bracket :: MonadCatch m
1017_bracket :: MonadCatch m
1018 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1018 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1019_bracket bef aft =
1019_bracket bef aft =
1020− gbracket bef MC.try aft (nilM (\(a, e :: MC.SomeException) -> aft a >>
1020+ gbracket_ bef MC.try aft (nilM (\(a, e :: MC.SomeException) -> aft a >>
1021 MC.throwM e))
1021 MC.throwM e))
1022
1022
1023−-- | @bracket before after between@ runs the @before@ action and then unfolds
1023+-- | @bracket_ before after between@ runs the @before@ action and then unfolds
1024-- its output using the @between@ unfold. When the @between@ unfold is done or
1024-- its output using the @between@ unfold. When the @between@ unfold is done or
1025-- if an exception occurs then the @after@ action is run with the output of
1025-- if an exception occurs then the @after@ action is run with the output of
1026-- @before@ as argument.
1026-- @before@ as argument.
1027--
1027--
1028−-- Prefer bracketIO over this as the @after@ action in this combinator is not
1028+-- Prefer 'bracket' over this as the @after@ action in this combinator is not
1029-- executed if the unfold is partially evaluated lazily and then garbage
1029-- executed if the unfold is partially evaluated lazily and then garbage
1030-- collected.
1030-- collected.
1031--
1031--
1032-- /Internal/
1032-- /Internal/
1033−{-# INLINE_NORMAL bracket #-}
1033+{-# INLINE_NORMAL bracket_ #-}
1034−bracket :: MonadCatch m
1034+bracket_ :: MonadCatch m
1035 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1035 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1036−bracket bef aft (Unfold step1 inject1) = Unfold step inject
1036+bracket_ bef aft (Unfold step1 inject1) = Unfold step inject
1037
1037
1038 where
1038 where
1039
1039
@@ -1057,10 +1057,10 @@ bracket bef aft (Unfold step1 inject1) = Unfold step inject
1057-- paritally evaluated and then garbage collected.
1057-- paritally evaluated and then garbage collected.
1058--
1058--
1059-- /Internal/
1059-- /Internal/
1060−{-# INLINE_NORMAL bracketIO #-}
1060+{-# INLINE_NORMAL bracket #-}
1061−bracketIO :: (MonadAsync m, MonadCatch m)
1061+bracket :: (MonadAsync m, MonadCatch m)
1062 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1062 => (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
1063−bracketIO bef aft (Unfold step1 inject1) = Unfold step inject
1063+bracket bef aft (Unfold step1 inject1) = Unfold step inject
1064
1064
1065 where
1065 where
1066
1066
@@ -1092,4 +1092,4 @@ bracketIO bef aft (Unfold step1 inject1) = Unfold step inject
1092handle :: (MonadCatch m, Exception e)
1092handle :: (MonadCatch m, Exception e)
1093 => Unfold m e b -> Unfold m a b -> Unfold m a b
1093 => Unfold m e b -> Unfold m a b -> Unfold m a b
1094handle exc =
1094handle exc =
1095− gbracket return MC.try (\_ -> return ()) (discardFirst exc)
1095+ gbracket_ return MC.try (\_ -> return ()) (discardFirst exc)
src/Streamly/Internal/FileSystem/File.hs+10−10
@@ -138,7 +138,7 @@ import qualified Streamly.Prelude as S
138-- /Internal/
138-- /Internal/
139--
139--
140{-# INLINE withFile #-}
140{-# INLINE withFile #-}
141−withFile :: (IsStream t, MonadCatch m, MonadIO m)
141+withFile :: (IsStream t, MonadCatch m, MonadAsync m)
142 => FilePath -> IOMode -> (Handle -> t m a) -> t m a
142 => FilePath -> IOMode -> (Handle -> t m a) -> t m a
143withFile file mode = S.bracket (liftIO $ openFile file mode) (liftIO . hClose)
143withFile file mode = S.bracket (liftIO $ openFile file mode) (liftIO . hClose)
144
144
@@ -151,14 +151,14 @@ withFile file mode = S.bracket (liftIO $ openFile file mode) (liftIO . hClose)
151-- /Internal/
151-- /Internal/
152--
152--
153{-# INLINABLE usingFile #-}
153{-# INLINABLE usingFile #-}
154−usingFile :: (MonadCatch m, MonadIO m)
154+usingFile :: (MonadCatch m, MonadAsync m)
155 => Unfold m Handle a -> Unfold m FilePath a
155 => Unfold m Handle a -> Unfold m FilePath a
156usingFile =
156usingFile =
157 UF.bracket (\file -> liftIO $ openFile file ReadMode)
157 UF.bracket (\file -> liftIO $ openFile file ReadMode)
158 (liftIO . hClose)
158 (liftIO . hClose)
159
159
160{-# INLINABLE usingFile2 #-}
160{-# INLINABLE usingFile2 #-}
161−usingFile2 :: (MonadCatch m, MonadIO m)
161+usingFile2 :: (MonadCatch m, MonadAsync m)
162 => Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
162 => Unfold m (x, Handle) a -> Unfold m (x, FilePath) a
163usingFile2 = UF.bracket before after
163usingFile2 = UF.bracket before after
164
164
@@ -202,7 +202,7 @@ appendArray file arr = SIO.withFile file AppendMode (`FH.writeArray` arr)
202-- The maximum size of a single array is specified by @size@. The actual size
202-- The maximum size of a single array is specified by @size@. The actual size
203-- read may be less than or equal to @size@.
203-- read may be less than or equal to @size@.
204{-# INLINABLE toChunksWithBufferOf #-}
204{-# INLINABLE toChunksWithBufferOf #-}
205−toChunksWithBufferOf :: (IsStream t, MonadCatch m, MonadIO m)
205+toChunksWithBufferOf :: (IsStream t, MonadCatch m, MonadAsync m)
206 => Int -> FilePath -> t m (Array Word8)
206 => Int -> FilePath -> t m (Array Word8)
207toChunksWithBufferOf size file =
207toChunksWithBufferOf size file =
208 withFile file ReadMode (FH.toChunksWithBufferOf size)
208 withFile file ReadMode (FH.toChunksWithBufferOf size)
@@ -217,7 +217,7 @@ toChunksWithBufferOf size file =
217--
217--
218-- @since 0.7.0
218-- @since 0.7.0
219{-# INLINE toChunks #-}
219{-# INLINE toChunks #-}
220−toChunks :: (IsStream t, MonadCatch m, MonadIO m)
220+toChunks :: (IsStream t, MonadCatch m, MonadAsync m)
221 => FilePath -> t m (Array Word8)
221 => FilePath -> t m (Array Word8)
222toChunks = toChunksWithBufferOf defaultChunkSize
222toChunks = toChunksWithBufferOf defaultChunkSize
223
223
@@ -237,7 +237,7 @@ toChunks = toChunksWithBufferOf defaultChunkSize
237-- /Internal/
237-- /Internal/
238--
238--
239{-# INLINE readChunksWithBufferOf #-}
239{-# INLINE readChunksWithBufferOf #-}
240−readChunksWithBufferOf :: (MonadCatch m, MonadIO m)
240+readChunksWithBufferOf :: (MonadCatch m, MonadAsync m)
241 => Unfold m (Int, FilePath) (Array Word8)
241 => Unfold m (Int, FilePath) (Array Word8)
242readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
242readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
243
243
@@ -249,7 +249,7 @@ readChunksWithBufferOf = usingFile2 FH.readChunksWithBufferOf
249--
249--
250-- /Internal/
250-- /Internal/
251{-# INLINE readChunks #-}
251{-# INLINE readChunks #-}
252−readChunks :: (MonadCatch m, MonadIO m) => Unfold m FilePath (Array Word8)
252+readChunks :: (MonadCatch m, MonadAsync m) => Unfold m FilePath (Array Word8)
253readChunks = usingFile FH.readChunks
253readChunks = usingFile FH.readChunks
254
254
255-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
255-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
@@ -257,7 +257,7 @@ readChunks = usingFile FH.readChunks
257--
257--
258-- /Internal/
258-- /Internal/
259{-# INLINE readWithBufferOf #-}
259{-# INLINE readWithBufferOf #-}
260−readWithBufferOf :: (MonadCatch m, MonadIO m) => Unfold m (Int, FilePath) Word8
260+readWithBufferOf :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8
261readWithBufferOf = usingFile2 FH.readWithBufferOf
261readWithBufferOf = usingFile2 FH.readWithBufferOf
262
262
263-- | Unfolds a file path into a byte stream. IO requests to the device are
263-- | Unfolds a file path into a byte stream. IO requests to the device are
@@ -266,7 +266,7 @@ readWithBufferOf = usingFile2 FH.readWithBufferOf
266--
266--
267-- @since 0.7.0
267-- @since 0.7.0
268{-# INLINE read #-}
268{-# INLINE read #-}
269−read :: (MonadCatch m, MonadIO m) => Unfold m FilePath Word8
269+read :: (MonadCatch m, MonadAsync m) => Unfold m FilePath Word8
270read = UF.concat (usingFile FH.readChunks) A.read
270read = UF.concat (usingFile FH.readChunks) A.read
271
271
272-- | Generate a stream of bytes from a file specified by path. The stream ends
272-- | Generate a stream of bytes from a file specified by path. The stream ends
@@ -276,7 +276,7 @@ read = UF.concat (usingFile FH.readChunks) A.read
276-- /Internal/
276-- /Internal/
277--
277--
278{-# INLINE toBytes #-}
278{-# INLINE toBytes #-}
279−toBytes :: (IsStream t, MonadCatch m, MonadIO m) => FilePath -> t m Word8
279+toBytes :: (IsStream t, MonadCatch m, MonadAsync m) => FilePath -> t m Word8
280toBytes file = AS.concat $ withFile file ReadMode FH.toChunks
280toBytes file = AS.concat $ withFile file ReadMode FH.toChunks
281
281
282{-
282{-
src/Streamly/Internal/Network/Inet/TCP.hs+4−4
@@ -278,7 +278,7 @@ withConnectionM addr port =
278--
278--
279-- /Internal/
279-- /Internal/
280{-# INLINABLE usingConnection #-}
280{-# INLINABLE usingConnection #-}
281−usingConnection :: (MonadCatch m, MonadIO m)
281+usingConnection :: (MonadCatch m, MonadAsync m)
282 => Unfold m Socket a
282 => Unfold m Socket a
283 -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) a
283 -> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) a
284usingConnection =
284usingConnection =
@@ -298,7 +298,7 @@ usingConnection =
298--
298--
299-- /Internal/
299-- /Internal/
300{-# INLINABLE withConnection #-}
300{-# INLINABLE withConnection #-}
301−withConnection :: (IsStream t, MonadCatch m, MonadIO m)
301+withConnection :: (IsStream t, MonadCatch m, MonadAsync m)
302 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> t m a) -> t m a
302 => (Word8, Word8, Word8, Word8) -> PortNumber -> (Socket -> t m a) -> t m a
303withConnection addr port =
303withConnection addr port =
304 S.bracket (liftIO $ connect addr port) (liftIO . Net.close)
304 S.bracket (liftIO $ connect addr port) (liftIO . Net.close)
@@ -311,7 +311,7 @@ withConnection addr port =
311--
311--
312-- @since 0.7.0
312-- @since 0.7.0
313{-# INLINE read #-}
313{-# INLINE read #-}
314−read :: (MonadCatch m, MonadIO m)
314+read :: (MonadCatch m, MonadAsync m)
315 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8
315 => Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8
316read = UF.concat (usingConnection ISK.readChunks) A.read
316read = UF.concat (usingConnection ISK.readChunks) A.read
317
317
@@ -319,7 +319,7 @@ read = UF.concat (usingConnection ISK.readChunks) A.read
319--
319--
320-- @since 0.7.0
320-- @since 0.7.0
321{-# INLINE toBytes #-}
321{-# INLINE toBytes #-}
322−toBytes :: (IsStream t, MonadCatch m, MonadIO m)
322+toBytes :: (IsStream t, MonadCatch m, MonadAsync m)
323 => (Word8, Word8, Word8, Word8) -> PortNumber -> t m Word8
323 => (Word8, Word8, Word8, Word8) -> PortNumber -> t m Word8
324toBytes addr port = AS.concat $ withConnection addr port ISK.toChunks
324toBytes addr port = AS.concat $ withConnection addr port ISK.toChunks
325
325
src/Streamly/Internal/Network/Socket.hs+1−1
@@ -122,7 +122,7 @@ handleWithM f sk = finally (f sk) (liftIO (Net.close sk))
122--
122--
123-- @since 0.7.0
123-- @since 0.7.0
124{-# INLINE handleWith #-}
124{-# INLINE handleWith #-}
125−handleWith :: (IsStream t, MonadCatch m, MonadIO m)
125+handleWith :: (IsStream t, MonadAsync m, MonadCatch m)
126 => Socket -> (Socket -> t m a) -> t m a
126 => Socket -> (Socket -> t m a) -> t m a
127handleWith sk f = S.finally (liftIO $ Net.close sk) (f sk)
127handleWith sk f = S.finally (liftIO $ Net.close sk) (f sk)
128
128
src/Streamly/Internal/Prelude.hs+27−27
@@ -481,13 +481,13 @@ module Streamly.Internal.Prelude
481
481
482 -- * Exceptions
482 -- * Exceptions
483 , before
483 , before
484+ , after_
484 , after
485 , after
485− , afterIO
486+ , bracket_
486 , bracket
487 , bracket
487− , bracketIO
488 , onException
488 , onException
489+ , finally_
489 , finally
490 , finally
490− , finallyIO
491 , handle
491 , handle
492
492
493 -- * Generalize Inner Monad
493 -- * Generalize Inner Monad
@@ -4831,23 +4831,23 @@ before action xs = D.fromStreamD $ D.before action $ D.toStreamD xs
4831
4831
4832-- | Run a side effect whenever the stream stops normally.
4832-- | Run a side effect whenever the stream stops normally.
4833--
4833--
4834−-- Prefer 'afterIO' over this as the @after@ action in this combinator is not
4834+-- Prefer 'after' over this as the @after@ action in this combinator is not
4835−-- executed if the unfold is partially evaluated lazily and then garbage
4835+-- executed if the stream is partially evaluated lazily and then garbage
4836-- collected.
4836-- collected.
4837--
4837--
4838-- @since 0.7.0
4838-- @since 0.7.0
4839−{-# INLINE after #-}
4839+{-# INLINE after_ #-}
4840−after :: (IsStream t, Monad m) => m b -> t m a -> t m a
4840+after_ :: (IsStream t, Monad m) => m b -> t m a -> t m a
4841−after action xs = D.fromStreamD $ D.after action $ D.toStreamD xs
4841+after_ action xs = D.fromStreamD $ D.after_ action $ D.toStreamD xs
4842
4842
4843-- | Run a side effect whenever the stream stops normally
4843-- | Run a side effect whenever the stream stops normally
4844-- or is garbage collected after a partial lazy evaluation.
4844-- or is garbage collected after a partial lazy evaluation.
4845--
4845--
4846-- /Internal/
4846-- /Internal/
4847--
4847--
4848−{-# INLINE afterIO #-}
4848+{-# INLINE after #-}
4849−afterIO :: (IsStream t, MonadIO m, MonadBaseControl IO m) => m b -> t m a -> t m a
4849+after :: (IsStream t, MonadIO m, MonadBaseControl IO m) => m b -> t m a -> t m a
4850−afterIO action xs = D.fromStreamD $ D.afterIO action $ D.toStreamD xs
4850+after action xs = D.fromStreamD $ D.after action $ D.toStreamD xs
4851
4851
4852-- | Run a side effect whenever the stream aborts due to an exception.
4852-- | Run a side effect whenever the stream aborts due to an exception.
4853--
4853--
@@ -4859,39 +4859,39 @@ onException action xs = D.fromStreamD $ D.onException action $ D.toStreamD xs
4859-- | Run a side effect whenever the stream stops normally or aborts due to an
4859-- | Run a side effect whenever the stream stops normally or aborts due to an
4860-- exception.
4860-- exception.
4861--
4861--
4862−-- Prefer 'finallyIO' over this as the @after@ action in this combinator is not
4862+-- Prefer 'finally' over this as the @after@ action in this combinator is not
4863-- executed if the unfold is partially evaluated lazily and then garbage
4863-- executed if the unfold is partially evaluated lazily and then garbage
4864-- collected.
4864-- collected.
4865--
4865--
4866-- @since 0.7.0
4866-- @since 0.7.0
4867−{-# INLINE finally #-}
4867+{-# INLINE finally_ #-}
4868−finally :: (IsStream t, MonadCatch m) => m b -> t m a -> t m a
4868+finally_ :: (IsStream t, MonadCatch m) => m b -> t m a -> t m a
4869−finally action xs = D.fromStreamD $ D.finally action $ D.toStreamD xs
4869+finally_ action xs = D.fromStreamD $ D.finally_ action $ D.toStreamD xs
4870
4870
4871-- | Run a side effect whenever the stream stops normally, aborts due to an
4871-- | Run a side effect whenever the stream stops normally, aborts due to an
4872-- exception or if it is garbage collected after a partial lazy evaluation.
4872-- exception or if it is garbage collected after a partial lazy evaluation.
4873--
4873--
4874-- /Internal/
4874-- /Internal/
4875--
4875--
4876−{-# INLINE finallyIO #-}
4876+{-# INLINE finally #-}
4877−finallyIO :: (IsStream t, MonadAsync m, MonadCatch m) => m b -> t m a -> t m a
4877+finally :: (IsStream t, MonadAsync m, MonadCatch m) => m b -> t m a -> t m a
4878−finallyIO action xs = D.fromStreamD $ D.finallyIO action $ D.toStreamD xs
4878+finally action xs = D.fromStreamD $ D.finally action $ D.toStreamD xs
4879
4879
4880-- | Run the first action before the stream starts and remember its output,
4880-- | Run the first action before the stream starts and remember its output,
4881-- generate a stream using the output, run the second action using the
4881-- generate a stream using the output, run the second action using the
4882-- remembered value as an argument whenever the stream ends normally or due to
4882-- remembered value as an argument whenever the stream ends normally or due to
4883-- an exception.
4883-- an exception.
4884--
4884--
4885−-- Prefer 'bracketIO' over this as the @after@ action in this combinator is not
4885+-- Prefer 'bracket' over this as the @after@ action in this combinator is not
4886-- executed if the unfold is partially evaluated lazily and then garbage
4886-- executed if the unfold is partially evaluated lazily and then garbage
4887-- collected.
4887-- collected.
4888--
4888--
4889-- @since 0.7.0
4889-- @since 0.7.0
4890−{-# INLINE bracket #-}
4890+{-# INLINE bracket_ #-}
4891−bracket :: (IsStream t, MonadCatch m)
4891+bracket_ :: (IsStream t, MonadCatch m)
4892 => m b -> (b -> m c) -> (b -> t m a) -> t m a
4892 => m b -> (b -> m c) -> (b -> t m a) -> t m a
4893−bracket bef aft bet = D.fromStreamD $
4893+bracket_ bef aft bet = D.fromStreamD $
4894− D.bracket bef aft (toStreamD . bet)
4894+ D.bracket_ bef aft (toStreamD . bet)
4895
4895
4896-- | Run the first action before the stream starts and remember its output,
4896-- | Run the first action before the stream starts and remember its output,
4897-- generate a stream using the output, run the second action using the
4897-- generate a stream using the output, run the second action using the
@@ -4900,11 +4900,11 @@ bracket bef aft bet = D.fromStreamD $
4900--
4900--
4901-- /Internal/
4901-- /Internal/
4902--
4902--
4903−{-# INLINE bracketIO #-}
4903+{-# INLINE bracket #-}
4904−bracketIO :: (IsStream t, MonadAsync m, MonadCatch m)
4904+bracket :: (IsStream t, MonadAsync m, MonadCatch m)
4905 => m b -> (b -> m c) -> (b -> t m a) -> t m a
4905 => m b -> (b -> m c) -> (b -> t m a) -> t m a
4906−bracketIO bef aft bet = D.fromStreamD $
4906+bracket bef aft bet = D.fromStreamD $
4907− D.bracketIO bef aft (toStreamD . bet)
4907+ D.bracket bef aft (toStreamD . bet)
4908
4908
4909-- | When evaluating a stream if an exception occurs, stream evaluation aborts
4909-- | When evaluating a stream if an exception occurs, stream evaluation aborts
4910-- and the specified exception handler is run with the exception as argument.
4910-- and the specified exception handler is run with the exception as argument.