@@ -8,6 +8,7 @@
88-- Portability : GHC
99
1010{-# LANGUAGE CPP #-}
11+{-# LANGUAGE ScopedTypeVariables #-}
1112
1213#ifdef __HADDOCK_VERSION__
1314#undef INSPECTION
@@ -29,6 +30,11 @@ module Streamly.Benchmark.FileIO.Stream
2930 , sumBytes
3031 , cat
3132 , catStreamWrite
33+ , catBracket
34+ , catBracketStream
35+ , catOnException
36+ , catHandle
37+ , catFinally
3238 , copy
3339 , linesUnlinesCopy
3440 , wordsUnwordsCopyWord8
@@ -50,9 +56,10 @@ module Streamly.Benchmark.FileIO.Stream
5056 )
5157where
5258
59+import Control.Exception (SomeException)
5360import Data.Char (ord, chr)
5461import Data.Word (Word8)
55−import System.IO (Handle)
62+import System.IO (Handle, hClose)
5663import Prelude hiding (last, length)
5764
5865import qualified Streamly.FileSystem.Handle as FH
@@ -179,6 +186,76 @@ inspect $ 'catStreamWrite `hasNoType` ''AT.FlattenState
179186inspect $ 'catStreamWrite `hasNoType` ''D.ConcatMapUState
180187#endif
181188
189+-- | Send the file contents to /dev/null with exception handling
190+{-# INLINE catBracket #-}
191+catBracket :: Handle -> Handle -> IO ()
192+catBracket devNull inh =
193+ let readEx = IUF.bracket (return ()) (\_ -> hClose inh) (\_ -> FH.read)
194+ in S.fold (FH.write devNull) $ S.unfold readEx inh
195+
196+#ifdef INSPECTION
197+inspect $ hasNoTypeClasses 'catBracket
198+-- inspect $ 'catBracket `hasNoType` ''Step
199+-- inspect $ 'catBracket `hasNoType` ''AT.FlattenState
200+-- inspect $ 'catBracket `hasNoType` ''D.ConcatMapUState
201+#endif
202+
203+-- | Send the file contents to /dev/null with exception handling
204+{-# INLINE catBracketStream #-}
205+catBracketStream :: Handle -> Handle -> IO ()
206+catBracketStream devNull inh =
207+ let readEx = S.bracket (return ()) (\_ -> hClose inh)
208+ (\_ -> IFH.toStream inh)
209+ in IFH.fromStream devNull $ readEx
210+
211+#ifdef INSPECTION
212+inspect $ hasNoTypeClasses 'catBracketStream
213+-- inspect $ 'catBracketStream `hasNoType` ''Step
214+#endif
215+
216+-- | Send the file contents to /dev/null with exception handling
217+{-# INLINE catOnException #-}
218+catOnException :: Handle -> Handle -> IO ()
219+catOnException devNull inh =
220+ let readEx = IUF.onException (hClose inh) FH.read
221+ in S.fold (FH.write devNull) $ S.unfold readEx inh
222+
223+#ifdef INSPECTION
224+inspect $ hasNoTypeClasses 'catOnException
225+-- inspect $ 'catOnException `hasNoType` ''Step
226+-- inspect $ 'catOnException `hasNoType` ''AT.FlattenState
227+-- inspect $ 'catOnException `hasNoType` ''D.ConcatMapUState
228+#endif
229+
230+-- | Send the file contents to /dev/null with exception handling
231+{-# INLINE catFinally #-}
232+catFinally :: Handle -> Handle -> IO ()
233+catFinally devNull inh =
234+ let readEx = IUF.finally (hClose inh) FH.read
235+ in S.fold (FH.write devNull) $ S.unfold readEx inh
236+
237+#ifdef INSPECTION
238+inspect $ hasNoTypeClasses 'catFinally
239+-- inspect $ 'catFinally `hasNoType` ''Step
240+-- inspect $ 'catFinally `hasNoType` ''AT.FlattenState
241+-- inspect $ 'catFinally `hasNoType` ''D.ConcatMapUState
242+#endif
243+
244+-- | Send the file contents to /dev/null with exception handling
245+{-# INLINE catHandle #-}
246+catHandle :: Handle -> Handle -> IO ()
247+catHandle devNull inh =
248+ let readEx = IUF.handle (\(_e :: SomeException) -> hClose inh >> return 10)
249+ FH.read
250+ in S.fold (FH.write devNull) $ S.unfold readEx inh
251+
252+#ifdef INSPECTION
253+inspect $ hasNoTypeClasses 'catHandle
254+-- inspect $ 'catHandle `hasNoType` ''Step
255+-- inspect $ 'catHandle `hasNoType` ''AT.FlattenState
256+-- inspect $ 'catHandle `hasNoType` ''D.ConcatMapUState
257+#endif
258+
182259-- | Copy file
183260{-# INLINE copy #-}
184261copy :: Handle -> Handle -> IO ()