on master help sign in
Rename bracket_ etc to bracketUnsafe etc
0902a15Harendra Kumar
benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+5−4
@@ -176,7 +176,8 @@ inspect $ hasNoTypeClasses 'readWriteHandleExceptionStream
176-- | Send the file contents to /dev/null with exception handling
176-- | Send the file contents to /dev/null with exception handling
177readWriteFinally_Stream :: Handle -> Handle -> IO ()
177readWriteFinally_Stream :: Handle -> Handle -> IO ()
178readWriteFinally_Stream inh devNull =
178readWriteFinally_Stream inh devNull =
179+ let readEx =
179− let readEx = Stream.finally_ (hClose inh) (Stream.unfold FH.reader inh)
180+ Stream.finallyUnsafe (hClose inh) (Stream.unfold FH.reader inh)
180 in Stream.fold (FH.write devNull) readEx
181 in Stream.fold (FH.write devNull) readEx
181
182
182#ifdef INSPECTION
183#ifdef INSPECTION
@@ -191,7 +192,7 @@ readWriteFinallyStream inh devNull =
191-- | Send the file contents to /dev/null with exception handling
192-- | Send the file contents to /dev/null with exception handling
192fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
193fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
193fromToBytesBracket_Stream inh devNull =
194fromToBytesBracket_Stream inh devNull =
194− let readEx = Stream.bracket_ (return ()) (\_ -> hClose inh)
195+ let readEx = Stream.bracketUnsafe (return ()) (\_ -> hClose inh)
195 (\_ -> IFH.read inh)
196 (\_ -> IFH.read inh)
196 in IFH.putBytes devNull readEx
197 in IFH.putBytes devNull readEx
197
198
@@ -227,7 +228,7 @@ inspect $ 'readWriteAfterStream `hasNoType` ''D.Step
227
228
228readWriteAfter_Stream :: Handle -> Handle -> IO ()
229readWriteAfter_Stream :: Handle -> Handle -> IO ()
229readWriteAfter_Stream inh devNull =
230readWriteAfter_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)
231 in Stream.fold (FH.write devNull) readEx
232 in Stream.fold (FH.write devNull) readEx
232
233
233#ifdef INSPECTION
234#ifdef INSPECTION
@@ -309,7 +310,7 @@ o_1_space_copy_exceptions_readChunks env =
309-- | Send the file contents to /dev/null with exception handling
310-- | Send the file contents to /dev/null with exception handling
310toChunksBracket_ :: Handle -> Handle -> IO ()
311toChunksBracket_ :: Handle -> Handle -> IO ()
311toChunksBracket_ inh devNull =
312toChunksBracket_ inh devNull =
312− let readEx = Stream.bracket_
313+ let readEx = Stream.bracketUnsafe
313 (return ())
314 (return ())
314 (\_ -> hClose inh)
315 (\_ -> hClose inh)
315 (\_ -> IFH.readChunks inh)
316 (\_ -> IFH.readChunks inh)
core/src/Streamly/Internal/Data/Stream/Exception.hs+27−22
@@ -9,13 +9,13 @@
9module Streamly.Internal.Data.Stream.Exception
9module Streamly.Internal.Data.Stream.Exception
10 (
10 (
11 before
11 before
12− , after_
12+ , afterUnsafe
13 , afterIO
13 , afterIO
14− , bracket_
14+ , bracketUnsafe
15 , bracketIO
15 , bracketIO
16 , bracket3IO
16 , bracket3IO
17 , onException
17 , onException
18− , finally_
18+ , finallyUnsafe
19 , finallyIO
19 , finallyIO
20 , ghandle
20 , ghandle
21 , handle
21 , handle
@@ -59,13 +59,13 @@ before action xs = fromStreamD $ D.before action $ toStreamD xs
59--
59--
60-- Same as the following, but with stream fusion:
60-- Same as the following, but with stream fusion:
61--
61--
62−-- >>> after_ action xs = xs <> Stream.nilM action
62+-- >>> afterUnsafe action xs = xs <> Stream.nilM action
63--
63--
64-- /Pre-release/
64-- /Pre-release/
65--
65--
66−{-# INLINE after_ #-}
66+{-# INLINE afterUnsafe #-}
67−after_ :: Monad m => m b -> Stream m a -> Stream m a
67+afterUnsafe :: Monad m => m b -> Stream m a -> Stream m a
68−after_ action xs = fromStreamD $ D.after_ action $ toStreamD xs
68+afterUnsafe action xs = fromStreamD $ D.after_ action $ toStreamD xs
69
69
70-- | Run the action @IO b@ whenever the stream stream stops normally, or
70-- | Run the action @IO b@ whenever the stream stream stops normally, or
71-- if it is garbage collected after a partial lazy evaluation.
71-- if it is garbage collected after a partial lazy evaluation.
@@ -73,7 +73,7 @@ after_ action xs = fromStreamD $ D.after_ action $ toStreamD xs
73-- The semantics of the action @IO b@ are similar to the semantics of cleanup
73-- The semantics of the action @IO b@ are similar to the semantics of cleanup
74-- action in 'bracketIO'.
74-- action in 'bracketIO'.
75--
75--
76−-- /See also 'after_'/
76+-- /See also 'afterUnsafe'/
77--
77--
78{-# INLINE afterIO #-}
78{-# INLINE afterIO #-}
79afterIO :: MonadIO m => IO b -> Stream m a -> Stream m a
79afterIO :: MonadIO m => IO b -> Stream m a -> Stream m a
@@ -98,9 +98,9 @@ onException action xs = fromStreamD $ D.onException action $ toStreamD xs
98--
98--
99-- /Pre-release/
99-- /Pre-release/
100--
100--
101−{-# INLINE finally_ #-}
101+{-# INLINE finallyUnsafe #-}
102−finally_ :: MonadCatch m => m b -> Stream m a -> Stream m a
102+finallyUnsafe :: MonadCatch m => m b -> Stream m a -> Stream m a
103−finally_ action xs = fromStreamD $ D.finally_ action $ toStreamD xs
103+finallyUnsafe action xs = fromStreamD $ D.finally_ action $ toStreamD xs
104
104
105-- | Run the action @IO b@ whenever the stream stream stops normally, aborts
105-- | Run the action @IO b@ whenever the stream stream stops normally, aborts
106-- due to an exception or if it is garbage collected after a partial lazy
106-- 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
111--
111--
112-- >>> finallyIO release = Stream.bracketIO (return ()) (const release)
112-- >>> finallyIO release = Stream.bracketIO (return ()) (const release)
113--
113--
114−-- /See also 'finally_'/
114+-- /See also 'finallyUnsafe'/
115--
115--
116-- /Inhibits stream fusion/
116-- /Inhibits stream fusion/
117--
117--
@@ -131,11 +131,10 @@ finallyIO action xs = fromStreamD $ D.finally action $ toStreamD xs
131--
131--
132-- /Pre-release/
132-- /Pre-release/
133--
133--
134−{-# INLINE bracket_ #-}
134+{-# INLINE bracketUnsafe #-}
135−bracket_ :: MonadCatch m
135+bracketUnsafe :: MonadCatch m
136 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
136 => 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)
139
138
140-- | Run the alloc action @IO b@ with async exceptions disabled but keeping
139-- | Run the alloc action @IO b@ with async exceptions disabled but keeping
141-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
140-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
@@ -154,7 +153,7 @@ bracket_ bef aft bet = fromStreamD $
154-- immediately in the current thread context, whereas in other cases it runs in
153-- immediately in the current thread context, whereas in other cases it runs in
155-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
154-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
156--
155--
157−-- /See also: 'bracket_'/
156+-- /See also: 'bracketUnsafe'/
158--
157--
159-- /Inhibits stream fusion/
158-- /Inhibits stream fusion/
160--
159--
@@ -167,11 +166,17 @@ bracketIO bef aft = bracket3IO bef aft aft aft
167-- the process in case of exception or garbage collection, but waits for the
166-- the process in case of exception or garbage collection, but waits for the
168-- process to terminate in normal cases.
167-- process to terminate in normal cases.
169
168
170−-- | Like 'bracketIO' but can use separate cleanup actions depending on the mode
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+--
171−-- of termination. @bracket3IO before onStop onGC onException action@ runs
176+-- @bracket3IO before onStop onGC onException action@ runs @action@ using the
172−-- @action@ using the result of @before@. If the stream stops, @onStop@ action
177+-- result of @before@. If the stream stops, @onStop@ action is executed, if the
173−-- is executed, if the stream is abandoned @onGC@ is executed, if the stream
178+-- stream is abandoned @onGC@ is executed, if the stream encounters an
174−-- encounters an exception @onException@ is executed.
179+-- exception @onException@ is executed.
175--
180--
176-- /Pre-release/
181-- /Pre-release/
177{-# INLINE bracket3IO #-}
182{-# INLINE bracket3IO #-}
src/Streamly/Data/Stream/Exception.hs+4−1
@@ -6,13 +6,16 @@
6-- Stability : released
6-- Stability : released
7-- Portability : GHC
7-- Portability : GHC
8--
8--
9+-- This module is designed such that it does not conflict with
10+-- "Streamly.Data.Stream" module.
11+--
9-- >>> import qualified Streamly.Data.Stream.Exception as Stream
12-- >>> import qualified Streamly.Data.Stream.Exception as Stream
10--
13--
11module Streamly.Data.Stream.Exception
14module Streamly.Data.Stream.Exception
12 (
15 (
13 after
16 after
14 , bracket
17 , bracket
15− , bracket'
18+ , bracket3
16 , finally
19 , finally
17 )
20 )
18where
21where
src/Streamly/Internal/Data/Stream/Exception/Lifted.hs+17−10
@@ -10,7 +10,7 @@ module Streamly.Internal.Data.Stream.Exception.Lifted
10 (
10 (
11 after
11 after
12 , bracket
12 , bracket
13− , bracket'
13+ , bracket3
14 , finally
14 , finally
15
15
16 -- For IsStream module
16 -- For IsStream module
@@ -123,21 +123,28 @@ bracket3D bef aft onExc onGC =
123-- the process in case of exception or garbage collection, but waits for the
123-- the process in case of exception or garbage collection, but waits for the
124-- process to terminate in normal cases.
124-- process to terminate in normal cases.
125
125
126−-- | Like 'bracket' but can use separate cleanup actions depending on the mode
126+-- | Like 'bracket' but can use 3 separate cleanup actions depending on the
127−-- of termination. @bracket' before onStop onGC onException action@ runs
127+-- mode of termination:
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.
131--
128--
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/
132−{-# INLINE bracket' #-}
139+{-# INLINE bracket3 #-}
133−bracket' :: (MonadRunInIO m, MonadCatch m)
140+bracket3 :: (MonadRunInIO m, MonadCatch m)
134 => m b
141 => m b
135 -> (b -> m c)
142 -> (b -> m c)
136 -> (b -> m d)
143 -> (b -> m d)
137 -> (b -> m e)
144 -> (b -> m e)
138 -> (b -> Stream m a)
145 -> (b -> Stream m a)
139 -> Stream m a
146 -> Stream m a
140−bracket' bef aft gc exc bet = fromStreamD $
147+bracket3 bef aft gc exc bet = fromStreamD $
141 bracket3D bef aft exc gc (toStreamD . bet)
148 bracket3D bef aft exc gc (toStreamD . bet)
142
149
143-- | Run the alloc action @m b@ with async exceptions disabled but keeping
150-- | Run the alloc action @m b@ with async exceptions disabled but keeping
@@ -164,7 +171,7 @@ bracket' bef aft gc exc bet = fromStreamD $
164{-# INLINE bracket #-}
171{-# INLINE bracket #-}
165bracket :: (MonadRunInIO m, MonadCatch m)
172bracket :: (MonadRunInIO m, MonadCatch m)
166 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
173 => 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
168
175
169-- | Run the action @m b@ whenever the stream @Stream m a@ stops normally,
176-- | Run the action @m b@ whenever the stream @Stream m a@ stops normally,
170-- aborts due to an exception or if it is garbage collected after a partial
177-- aborts due to an exception or if it is garbage collected after a partial