on master help sign in
Rename bracket_ etc to bracketUnsafe etc
commit0902a1587f7f2a4f5f02fd7fa58a0a210e5b4636 authorHarendra Kumar <harendra@composewell.com> dateTue 25 Oct 2022, 23:19 (4 years ago) parent957b422
4 files changed+53−37
benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+5−4 core/src/Streamly/Internal/Data/Stream/Exception.hs+27−22 src/Streamly/Data/Stream/Exception.hs+4−1 src/Streamly/Internal/Data/Stream/Exception/Lifted.hs+17−10
benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+5−4
@@ -176,7 +176,8 @@ inspect $ hasNoTypeClasses 'readWriteHandleExceptionStream
176176-- | Send the file contents to /dev/null with exception handling
177177readWriteFinally_Stream :: Handle -> Handle -> IO ()
178178readWriteFinally_Stream inh devNull =
179− let readEx = Stream.finally_ (hClose inh) (Stream.unfold FH.reader inh)
179+ let readEx =
180+ Stream.finallyUnsafe (hClose inh) (Stream.unfold FH.reader inh)
180181 in Stream.fold (FH.write devNull) readEx
181182
182183#ifdef INSPECTION
@@ -191,7 +192,7 @@ readWriteFinallyStream inh devNull =
191192-- | Send the file contents to /dev/null with exception handling
192193fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
193194fromToBytesBracket_Stream inh devNull =
194− let readEx = Stream.bracket_ (return ()) (\_ -> hClose inh)
195+ let readEx = Stream.bracketUnsafe (return ()) (\_ -> hClose inh)
195196 (\_ -> IFH.read inh)
196197 in IFH.putBytes devNull readEx
197198
@@ -227,7 +228,7 @@ inspect $ 'readWriteAfterStream `hasNoType` ''D.Step
227228
228229readWriteAfter_Stream :: Handle -> Handle -> IO ()
229230readWriteAfter_Stream inh devNull =
230− let readEx = Stream.after_ (hClose inh) (Stream.unfold FH.reader inh)
231+ let readEx = Stream.afterUnsafe (hClose inh) (Stream.unfold FH.reader inh)
231232 in Stream.fold (FH.write devNull) readEx
232233
233234#ifdef INSPECTION
@@ -309,7 +310,7 @@ o_1_space_copy_exceptions_readChunks env =
309310-- | Send the file contents to /dev/null with exception handling
310311toChunksBracket_ :: Handle -> Handle -> IO ()
311312toChunksBracket_ inh devNull =
312− let readEx = Stream.bracket_
313+ let readEx = Stream.bracketUnsafe
313314 (return ())
314315 (\_ -> hClose inh)
315316 (\_ -> IFH.readChunks inh)
core/src/Streamly/Internal/Data/Stream/Exception.hs+27−22
@@ -9,13 +9,13 @@
99module Streamly.Internal.Data.Stream.Exception
1010 (
1111 before
12− , after_
12+ , afterUnsafe
1313 , afterIO
14− , bracket_
14+ , bracketUnsafe
1515 , bracketIO
1616 , bracket3IO
1717 , onException
18− , finally_
18+ , finallyUnsafe
1919 , finallyIO
2020 , ghandle
2121 , handle
@@ -59,13 +59,13 @@ before action xs = fromStreamD $ D.before action $ toStreamD xs
5959--
6060-- Same as the following, but with stream fusion:
6161--
62−-- >>> after_ action xs = xs <> Stream.nilM action
62+-- >>> afterUnsafe action xs = xs <> Stream.nilM action
6363--
6464-- /Pre-release/
6565--
66−{-# INLINE after_ #-}
67−after_ :: Monad m => m b -> Stream m a -> Stream m a
68−after_ action xs = fromStreamD $ D.after_ action $ toStreamD xs
66+{-# INLINE afterUnsafe #-}
67+afterUnsafe :: Monad m => m b -> Stream m a -> Stream m a
68+afterUnsafe action xs = fromStreamD $ D.after_ action $ toStreamD xs
6969
7070-- | Run the action @IO b@ whenever the stream stream stops normally, or
7171-- if it is garbage collected after a partial lazy evaluation.
@@ -73,7 +73,7 @@ after_ action xs = fromStreamD $ D.after_ action $ toStreamD xs
7373-- The semantics of the action @IO b@ are similar to the semantics of cleanup
7474-- action in 'bracketIO'.
7575--
76−-- /See also 'after_'/
76+-- /See also 'afterUnsafe'/
7777--
7878{-# INLINE afterIO #-}
7979afterIO :: MonadIO m => IO b -> Stream m a -> Stream m a
@@ -98,9 +98,9 @@ onException action xs = fromStreamD $ D.onException action $ toStreamD xs
9898--
9999-- /Pre-release/
100100--
101−{-# INLINE finally_ #-}
102−finally_ :: MonadCatch m => m b -> Stream m a -> Stream m a
103−finally_ action xs = fromStreamD $ D.finally_ action $ toStreamD xs
101+{-# INLINE finallyUnsafe #-}
102+finallyUnsafe :: MonadCatch m => m b -> Stream m a -> Stream m a
103+finallyUnsafe action xs = fromStreamD $ D.finally_ action $ toStreamD xs
104104
105105-- | Run the action @IO b@ whenever the stream stream stops normally, aborts
106106-- due to an exception or if it is garbage collected after a partial lazy
@@ -111,7 +111,7 @@ finally_ action xs = fromStreamD $ D.finally_ action $ toStreamD xs
111111--
112112-- >>> finallyIO release = Stream.bracketIO (return ()) (const release)
113113--
114−-- /See also 'finally_'/
114+-- /See also 'finallyUnsafe'/
115115--
116116-- /Inhibits stream fusion/
117117--
@@ -131,11 +131,10 @@ finallyIO action xs = fromStreamD $ D.finally action $ toStreamD xs
131131--
132132-- /Pre-release/
133133--
134−{-# INLINE bracket_ #-}
135−bracket_ :: MonadCatch m
134+{-# INLINE bracketUnsafe #-}
135+bracketUnsafe :: MonadCatch m
136136 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
137−bracket_ bef aft bet = fromStreamD $
138− D.bracket_ bef aft (toStreamD . bet)
137+bracketUnsafe bef aft bet = fromStreamD $ D.bracket_ bef aft (toStreamD . bet)
139138
140139-- | Run the alloc action @IO b@ with async exceptions disabled but keeping
141140-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
@@ -154,7 +153,7 @@ bracket_ bef aft bet = fromStreamD $
154153-- immediately in the current thread context, whereas in other cases it runs in
155154-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
156155--
157−-- /See also: 'bracket_'/
156+-- /See also: 'bracketUnsafe'/
158157--
159158-- /Inhibits stream fusion/
160159--
@@ -167,11 +166,17 @@ bracketIO bef aft = bracket3IO bef aft aft aft
167166-- the process in case of exception or garbage collection, but waits for the
168167-- process to terminate in normal cases.
169168
170−-- | Like 'bracketIO' but can use separate cleanup actions depending on the mode
171−-- of termination. @bracket3IO before onStop onGC onException action@ runs
172−-- @action@ using the result of @before@. If the stream stops, @onStop@ action
173−-- is executed, if the stream is abandoned @onGC@ is executed, if the stream
174−-- encounters an exception @onException@ is executed.
169+-- | Like 'bracketIO' but can use 3 separate cleanup actions depending on the
170+-- mode of termination:
171+--
172+-- 1. When the stream stops normally
173+-- 2. When the stream is garbage collected
174+-- 3. When the stream encounters an exception
175+--
176+-- @bracket3IO before onStop onGC onException action@ runs @action@ using the
177+-- result of @before@. If the stream stops, @onStop@ action is executed, if the
178+-- stream is abandoned @onGC@ is executed, if the stream encounters an
179+-- exception @onException@ is executed.
175180--
176181-- /Pre-release/
177182{-# INLINE bracket3IO #-}
src/Streamly/Data/Stream/Exception.hs+4−1
@@ -6,13 +6,16 @@
66-- Stability : released
77-- Portability : GHC
88--
9+-- This module is designed such that it does not conflict with
10+-- "Streamly.Data.Stream" module.
11+--
912-- >>> import qualified Streamly.Data.Stream.Exception as Stream
1013--
1114module Streamly.Data.Stream.Exception
1215 (
1316 after
1417 , bracket
15− , bracket'
18+ , bracket3
1619 , finally
1720 )
1821where
src/Streamly/Internal/Data/Stream/Exception/Lifted.hs+17−10
@@ -10,7 +10,7 @@ module Streamly.Internal.Data.Stream.Exception.Lifted
1010 (
1111 after
1212 , bracket
13− , bracket'
13+ , bracket3
1414 , finally
1515
1616 -- For IsStream module
@@ -123,21 +123,28 @@ bracket3D bef aft onExc onGC =
123123-- the process in case of exception or garbage collection, but waits for the
124124-- process to terminate in normal cases.
125125
126−-- | Like 'bracket' but can use separate cleanup actions depending on the mode
127−-- of termination. @bracket' before onStop onGC onException action@ runs
128−-- @action@ using the result of @before@. If the stream stops, @onStop@ action
129−-- is executed, if the stream is abandoned @onGC@ is executed, if the stream
130−-- encounters an exception @onException@ is executed.
126+-- | Like 'bracket' but can use 3 separate cleanup actions depending on the
127+-- mode of termination:
131128--
132−{-# INLINE bracket' #-}
133−bracket' :: (MonadRunInIO m, MonadCatch m)
129+-- 1. When the stream stops normally
130+-- 2. When the stream is garbage collected
131+-- 3. When the stream encounters an exception
132+--
133+-- @bracket3 before onStop onGC onException action@ runs @action@ using the
134+-- result of @before@. If the stream stops, @onStop@ action is executed, if the
135+-- stream is abandoned @onGC@ is executed, if the stream encounters an
136+-- exception @onException@ is executed.
137+--
138+-- /Pre-release/
139+{-# INLINE bracket3 #-}
140+bracket3 :: (MonadRunInIO m, MonadCatch m)
134141 => m b
135142 -> (b -> m c)
136143 -> (b -> m d)
137144 -> (b -> m e)
138145 -> (b -> Stream m a)
139146 -> Stream m a
140−bracket' bef aft gc exc bet = fromStreamD $
147+bracket3 bef aft gc exc bet = fromStreamD $
141148 bracket3D bef aft exc gc (toStreamD . bet)
142149
143150-- | Run the alloc action @m b@ with async exceptions disabled but keeping
@@ -164,7 +171,7 @@ bracket' bef aft gc exc bet = fromStreamD $
164171{-# INLINE bracket #-}
165172bracket :: (MonadRunInIO m, MonadCatch m)
166173 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
167−bracket bef aft = bracket' bef aft aft aft
174+bracket bef aft = bracket3 bef aft aft aft
168175
169176-- | Run the action @m b@ whenever the stream @Stream m a@ stops normally,
170177-- aborts due to an exception or if it is garbage collected after a partial