on master help sign in
Change Handle, Stdio, File unfold APIs (#1998)
e03ccebHarendra Kumar
benchmark/Streamly/Benchmark/Data/Array/Stream/Unboxed.hs+8−8
@@ -83,7 +83,7 @@ benchIO name src sink =
83-- | Get the last byte from a file bytestream.
83-- | Get the last byte from a file bytestream.
84toChunksLast :: Handle -> IO (Maybe Word8)
84toChunksLast :: Handle -> IO (Maybe Word8)
85toChunksLast inh = do
85toChunksLast inh = do
86− let s = Handle.getChunks inh
86+ let s = Handle.readChunks inh
87 larr <- Stream.last s
87 larr <- Stream.last s
88 return $ case larr of
88 return $ case larr of
89 Nothing -> Nothing
89 Nothing -> Nothing
@@ -97,7 +97,7 @@ inspect $ 'toChunksLast `hasNoType` ''Step
97-- | Count the number of bytes in a file.
97-- | Count the number of bytes in a file.
98toChunksSumLengths :: Handle -> IO Int
98toChunksSumLengths :: Handle -> IO Int
99toChunksSumLengths inh =
99toChunksSumLengths inh =
100− let s = Handle.getChunks inh
100+ let s = Handle.readChunks inh
101 in Stream.sum (Stream.map Array.length s)
101 in Stream.sum (Stream.map Array.length s)
102
102
103#ifdef INSPECTION
103#ifdef INSPECTION
@@ -109,7 +109,7 @@ inspect $ 'toChunksSumLengths `hasNoType` ''Step
109toChunksCountBytes :: Handle -> IO Word8
109toChunksCountBytes :: Handle -> IO Word8
110toChunksCountBytes inh = do
110toChunksCountBytes inh = do
111 let foldlArr' f z = runIdentity . Stream.foldl' f z . Array.toStream
111 let foldlArr' f z = runIdentity . Stream.foldl' f z . Array.toStream
112− let s = Handle.getChunks inh
112+ let s = Handle.readChunks inh
113 Stream.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
113 Stream.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
114
114
115#ifdef INSPECTION
115#ifdef INSPECTION
@@ -119,7 +119,7 @@ inspect $ 'toChunksCountBytes `hasNoType` ''Step
119
119
120toChunksDecodeUtf8Arrays :: Handle -> IO ()
120toChunksDecodeUtf8Arrays :: Handle -> IO ()
121toChunksDecodeUtf8Arrays =
121toChunksDecodeUtf8Arrays =
122− Stream.drain . Unicode.decodeUtf8Arrays . Handle.getChunks
122+ Stream.drain . Unicode.decodeUtf8Arrays . Handle.readChunks
123
123
124#ifdef INSPECTION
124#ifdef INSPECTION
125inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
125inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
@@ -133,7 +133,7 @@ inspect $ hasNoTypeClasses 'toChunksDecodeUtf8Arrays
133-- | Count the number of lines in a file.
133-- | Count the number of lines in a file.
134toChunksSplitOnSuffix :: Handle -> IO Int
134toChunksSplitOnSuffix :: Handle -> IO Int
135toChunksSplitOnSuffix =
135toChunksSplitOnSuffix =
136− Stream.length . ArrayStream.splitOnSuffix 10 . Handle.getChunks
136+ Stream.length . ArrayStream.splitOnSuffix 10 . Handle.readChunks
137
137
138#ifdef INSPECTION
138#ifdef INSPECTION
139inspect $ hasNoTypeClasses 'toChunksSplitOnSuffix
139inspect $ hasNoTypeClasses 'toChunksSplitOnSuffix
@@ -143,7 +143,7 @@ inspect $ 'toChunksSplitOnSuffix `hasNoType` ''Step
143-- XXX use a word splitting combinator instead of splitOn and test it.
143-- XXX use a word splitting combinator instead of splitOn and test it.
144-- | Count the number of words in a file.
144-- | Count the number of words in a file.
145toChunksSplitOn :: Handle -> IO Int
145toChunksSplitOn :: Handle -> IO Int
146−toChunksSplitOn = Stream.length . ArrayStream.splitOn 32 . Handle.getChunks
146+toChunksSplitOn = Stream.length . ArrayStream.splitOn 32 . Handle.readChunks
147
147
148#ifdef INSPECTION
148#ifdef INSPECTION
149inspect $ hasNoTypeClasses 'toChunksSplitOn
149inspect $ hasNoTypeClasses 'toChunksSplitOn
@@ -183,7 +183,7 @@ copyChunksSplitInterposeSuffix inh outh =
183 Stream.fold (Handle.write outh)
183 Stream.fold (Handle.write outh)
184 $ ArrayStream.interposeSuffix 10
184 $ ArrayStream.interposeSuffix 10
185 $ ArrayStream.splitOnSuffix 10
185 $ ArrayStream.splitOnSuffix 10
186− $ Handle.getChunks inh
186+ $ Handle.readChunks inh
187
187
188#ifdef INSPECTION
188#ifdef INSPECTION
189inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterposeSuffix [''Unboxed]
189inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterposeSuffix [''Unboxed]
@@ -198,7 +198,7 @@ copyChunksSplitInterpose inh outh =
198 $ ArrayStream.interpose 32
198 $ ArrayStream.interpose 32
199 -- XXX this is not correct word splitting combinator
199 -- XXX this is not correct word splitting combinator
200 $ ArrayStream.splitOn 32
200 $ ArrayStream.splitOn 32
201− $ Handle.getChunks inh
201+ $ Handle.readChunks inh
202
202
203#ifdef INSPECTION
203#ifdef INSPECTION
204inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterpose [''Unboxed]
204inspect $ hasNoTypeClassesExcept 'copyChunksSplitInterpose [''Unboxed]
benchmark/Streamly/Benchmark/Data/Parser.hs+1−1
@@ -330,7 +330,7 @@ parseManyChunksOfSum n inh =
330 Stream.fold Fold.length
330 Stream.fold Fold.length
331 $ Stream.parseMany
331 $ Stream.parseMany
332 (PR.fromFold $ Fold.take n Fold.sum)
332 (PR.fromFold $ Fold.take n Fold.sum)
333− (Stream.unfold Handle.read inh)
333+ (Stream.unfold Handle.reader inh)
334
334
335-------------------------------------------------------------------------------
335-------------------------------------------------------------------------------
336-- Parsing with unfolds
336-- Parsing with unfolds
benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs+14−14
@@ -149,7 +149,7 @@ o_1_space_serial_exceptions length =
149-- | Send the file contents to /dev/null with exception handling
149-- | Send the file contents to /dev/null with exception handling
150readWriteOnExceptionStream :: Handle -> Handle -> IO ()
150readWriteOnExceptionStream :: Handle -> Handle -> IO ()
151readWriteOnExceptionStream inh devNull =
151readWriteOnExceptionStream inh devNull =
152− let readEx = Stream.onException (hClose inh) (Stream.unfold FH.read inh)
152+ let readEx = Stream.onException (hClose inh) (Stream.unfold FH.reader inh)
153 in Stream.fold (FH.write devNull) readEx
153 in Stream.fold (FH.write devNull) readEx
154
154
155#ifdef INSPECTION
155#ifdef INSPECTION
@@ -160,7 +160,7 @@ inspect $ hasNoTypeClasses 'readWriteOnExceptionStream
160readWriteHandleExceptionStream :: Handle -> Handle -> IO ()
160readWriteHandleExceptionStream :: Handle -> Handle -> IO ()
161readWriteHandleExceptionStream inh devNull =
161readWriteHandleExceptionStream inh devNull =
162 let handler (_e :: SomeException) = Stream.fromEffect (hClose inh >> return 10)
162 let handler (_e :: SomeException) = Stream.fromEffect (hClose inh >> return 10)
163− readEx = Stream.handle handler (Stream.unfold FH.read inh)
163+ readEx = Stream.handle handler (Stream.unfold FH.reader inh)
164 in Stream.fold (FH.write devNull) readEx
164 in Stream.fold (FH.write devNull) readEx
165
165
166#ifdef INSPECTION
166#ifdef INSPECTION
@@ -170,7 +170,7 @@ inspect $ hasNoTypeClasses 'readWriteHandleExceptionStream
170-- | Send the file contents to /dev/null with exception handling
170-- | Send the file contents to /dev/null with exception handling
171readWriteFinally_Stream :: Handle -> Handle -> IO ()
171readWriteFinally_Stream :: Handle -> Handle -> IO ()
172readWriteFinally_Stream inh devNull =
172readWriteFinally_Stream inh devNull =
173− let readEx = Stream.finally_ (hClose inh) (Stream.unfold FH.read inh)
173+ let readEx = Stream.finally_ (hClose inh) (Stream.unfold FH.reader inh)
174 in Stream.fold (FH.write devNull) readEx
174 in Stream.fold (FH.write devNull) readEx
175
175
176#ifdef INSPECTION
176#ifdef INSPECTION
@@ -179,14 +179,14 @@ inspect $ hasNoTypeClasses 'readWriteFinally_Stream
179
179
180readWriteFinallyStream :: Handle -> Handle -> IO ()
180readWriteFinallyStream :: Handle -> Handle -> IO ()
181readWriteFinallyStream inh devNull =
181readWriteFinallyStream inh devNull =
182− let readEx = Stream.finally (hClose inh) (Stream.unfold FH.read inh)
182+ let readEx = Stream.finally (hClose inh) (Stream.unfold FH.reader inh)
183 in Stream.fold (FH.write devNull) readEx
183 in Stream.fold (FH.write devNull) readEx
184
184
185-- | Send the file contents to /dev/null with exception handling
185-- | Send the file contents to /dev/null with exception handling
186fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
186fromToBytesBracket_Stream :: Handle -> Handle -> IO ()
187fromToBytesBracket_Stream inh devNull =
187fromToBytesBracket_Stream inh devNull =
188 let readEx = Stream.bracket_ (return ()) (\_ -> hClose inh)
188 let readEx = Stream.bracket_ (return ()) (\_ -> hClose inh)
189− (\_ -> IFH.getBytes inh)
189+ (\_ -> IFH.read inh)
190 in IFH.putBytes devNull readEx
190 in IFH.putBytes devNull readEx
191
191
192#ifdef INSPECTION
192#ifdef INSPECTION
@@ -196,14 +196,14 @@ inspect $ hasNoTypeClasses 'fromToBytesBracket_Stream
196fromToBytesBracketStream :: Handle -> Handle -> IO ()
196fromToBytesBracketStream :: Handle -> Handle -> IO ()
197fromToBytesBracketStream inh devNull =
197fromToBytesBracketStream inh devNull =
198 let readEx = Stream.bracket (return ()) (\_ -> hClose inh)
198 let readEx = Stream.bracket (return ()) (\_ -> hClose inh)
199− (\_ -> IFH.getBytes inh)
199+ (\_ -> IFH.read inh)
200 in IFH.putBytes devNull readEx
200 in IFH.putBytes devNull readEx
201
201
202readWriteBeforeAfterStream :: Handle -> Handle -> IO ()
202readWriteBeforeAfterStream :: Handle -> Handle -> IO ()
203readWriteBeforeAfterStream inh devNull =
203readWriteBeforeAfterStream inh devNull =
204 let readEx =
204 let readEx =
205 Stream.after (hClose inh)
205 Stream.after (hClose inh)
206− $ Stream.before (hPutChar devNull 'A') (Stream.unfold FH.read inh)
206+ $ Stream.before (hPutChar devNull 'A') (Stream.unfold FH.reader inh)
207 in Stream.fold (FH.write devNull) readEx
207 in Stream.fold (FH.write devNull) readEx
208
208
209#ifdef INSPECTION
209#ifdef INSPECTION
@@ -212,7 +212,7 @@ inspect $ 'readWriteBeforeAfterStream `hasNoType` ''D.Step
212
212
213readWriteAfterStream :: Handle -> Handle -> IO ()
213readWriteAfterStream :: Handle -> Handle -> IO ()
214readWriteAfterStream inh devNull =
214readWriteAfterStream inh devNull =
215− let readEx = Stream.after (hClose inh) (Stream.unfold FH.read inh)
215+ let readEx = Stream.after (hClose inh) (Stream.unfold FH.reader inh)
216 in Stream.fold (FH.write devNull) readEx
216 in Stream.fold (FH.write devNull) readEx
217
217
218#ifdef INSPECTION
218#ifdef INSPECTION
@@ -221,7 +221,7 @@ inspect $ 'readWriteAfterStream `hasNoType` ''D.Step
221
221
222readWriteAfter_Stream :: Handle -> Handle -> IO ()
222readWriteAfter_Stream :: Handle -> Handle -> IO ()
223readWriteAfter_Stream inh devNull =
223readWriteAfter_Stream inh devNull =
224− let readEx = Stream.after_ (hClose inh) (Stream.unfold FH.read inh)
224+ let readEx = Stream.after_ (hClose inh) (Stream.unfold FH.reader inh)
225 in Stream.fold (FH.write devNull) readEx
225 in Stream.fold (FH.write devNull) readEx
226
226
227#ifdef INSPECTION
227#ifdef INSPECTION
@@ -262,7 +262,7 @@ o_1_space_copy_stream_exceptions env =
262-- | Send the file contents to /dev/null with exception handling
262-- | Send the file contents to /dev/null with exception handling
263readChunksOnException :: Handle -> Handle -> IO ()
263readChunksOnException :: Handle -> Handle -> IO ()
264readChunksOnException inh devNull =
264readChunksOnException inh devNull =
265− let readEx = IUF.onException (\_ -> hClose inh) FH.readChunks
265+ let readEx = IUF.onException (\_ -> hClose inh) FH.chunkReader
266 in IUF.fold (IFH.writeChunks devNull) readEx inh
266 in IUF.fold (IFH.writeChunks devNull) readEx inh
267
267
268#ifdef INSPECTION
268#ifdef INSPECTION
@@ -272,7 +272,7 @@ inspect $ hasNoTypeClasses 'readChunksOnException
272-- | Send the file contents to /dev/null with exception handling
272-- | Send the file contents to /dev/null with exception handling
273readChunksBracket_ :: Handle -> Handle -> IO ()
273readChunksBracket_ :: Handle -> Handle -> IO ()
274readChunksBracket_ inh devNull =
274readChunksBracket_ inh devNull =
275− let readEx = IUF.bracket_ return (\_ -> hClose inh) FH.readChunks
275+ let readEx = IUF.bracket_ return (\_ -> hClose inh) FH.chunkReader
276 in IUF.fold (IFH.writeChunks devNull) readEx inh
276 in IUF.fold (IFH.writeChunks devNull) readEx inh
277
277
278#ifdef INSPECTION
278#ifdef INSPECTION
@@ -281,7 +281,7 @@ inspect $ hasNoTypeClasses 'readChunksBracket_
281
281
282readChunksBracket :: Handle -> Handle -> IO ()
282readChunksBracket :: Handle -> Handle -> IO ()
283readChunksBracket inh devNull =
283readChunksBracket inh devNull =
284− let readEx = IUF.bracket return (\_ -> hClose inh) FH.readChunks
284+ let readEx = IUF.bracket return (\_ -> hClose inh) FH.chunkReader
285 in IUF.fold (IFH.writeChunks devNull) readEx inh
285 in IUF.fold (IFH.writeChunks devNull) readEx inh
286
286
287o_1_space_copy_exceptions_readChunks :: BenchEnv -> [Benchmark]
287o_1_space_copy_exceptions_readChunks :: BenchEnv -> [Benchmark]
@@ -306,7 +306,7 @@ toChunksBracket_ inh devNull =
306 let readEx = Stream.bracket_
306 let readEx = Stream.bracket_
307 (return ())
307 (return ())
308 (\_ -> hClose inh)
308 (\_ -> hClose inh)
309− (\_ -> IFH.getChunks inh)
309+ (\_ -> IFH.readChunks inh)
310 in Stream.fold (IFH.writeChunks devNull) readEx
310 in Stream.fold (IFH.writeChunks devNull) readEx
311
311
312#ifdef INSPECTION
312#ifdef INSPECTION
@@ -318,7 +318,7 @@ toChunksBracket inh devNull =
318 let readEx = Stream.bracket
318 let readEx = Stream.bracket
319 (return ())
319 (return ())
320 (\_ -> hClose inh)
320 (\_ -> hClose inh)
321− (\_ -> IFH.getChunks inh)
321+ (\_ -> IFH.readChunks inh)
322 in Stream.fold (IFH.writeChunks devNull) readEx
322 in Stream.fold (IFH.writeChunks devNull) readEx
323
323
324o_1_space_copy_exceptions_toChunks :: BenchEnv -> [Benchmark]
324o_1_space_copy_exceptions_toChunks :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/Data/Unfold.hs+7−7
@@ -707,7 +707,7 @@ lf = fromIntegral (ord '\n')
707-- | Split on line feed.
707-- | Split on line feed.
708foldManySepBy :: Handle -> IO Int
708foldManySepBy :: Handle -> IO Int
709foldManySepBy =
709foldManySepBy =
710− let u = UF.foldMany (FL.takeEndBy_ (== lf) FL.drain) FH.read
710+ let u = UF.foldMany (FL.takeEndBy_ (== lf) FL.drain) FH.reader
711 in UF.fold FL.length u
711 in UF.fold FL.length u
712
712
713o_1_space_nested :: BenchEnv -> Int -> [Benchmark]
713o_1_space_nested :: BenchEnv -> Int -> [Benchmark]
@@ -748,7 +748,7 @@ o_n_space_nested size =
748-- | Send the file contents to /dev/null with exception handling
748-- | Send the file contents to /dev/null with exception handling
749readWriteOnExceptionUnfold :: Handle -> Handle -> IO ()
749readWriteOnExceptionUnfold :: Handle -> Handle -> IO ()
750readWriteOnExceptionUnfold inh devNull =
750readWriteOnExceptionUnfold inh devNull =
751− let readEx = UF.onException (\_ -> hClose inh) FH.read
751+ let readEx = UF.onException (\_ -> hClose inh) FH.reader
752 in S.fold (FH.write devNull) $ S.unfold readEx inh
752 in S.fold (FH.write devNull) $ S.unfold readEx inh
753
753
754#ifdef INSPECTION
754#ifdef INSPECTION
@@ -760,7 +760,7 @@ inspect $ hasNoTypeClasses 'readWriteOnExceptionUnfold
760readWriteHandleExceptionUnfold :: Handle -> Handle -> IO ()
760readWriteHandleExceptionUnfold :: Handle -> Handle -> IO ()
761readWriteHandleExceptionUnfold inh devNull =
761readWriteHandleExceptionUnfold inh devNull =
762 let handler (_e :: SomeException) = hClose inh >> return 10
762 let handler (_e :: SomeException) = hClose inh >> return 10
763− readEx = UF.handle (UF.functionM handler) FH.read
763+ readEx = UF.handle (UF.functionM handler) FH.reader
764 in S.fold (FH.write devNull) $ S.unfold readEx inh
764 in S.fold (FH.write devNull) $ S.unfold readEx inh
765
765
766#ifdef INSPECTION
766#ifdef INSPECTION
@@ -771,7 +771,7 @@ inspect $ hasNoTypeClasses 'readWriteHandleExceptionUnfold
771-- | Send the file contents to /dev/null with exception handling
771-- | Send the file contents to /dev/null with exception handling
772readWriteFinally_Unfold :: Handle -> Handle -> IO ()
772readWriteFinally_Unfold :: Handle -> Handle -> IO ()
773readWriteFinally_Unfold inh devNull =
773readWriteFinally_Unfold inh devNull =
774− let readEx = UF.finally_ (\_ -> hClose inh) FH.read
774+ let readEx = UF.finally_ (\_ -> hClose inh) FH.reader
775 in S.fold (FH.write devNull) $ S.unfold readEx inh
775 in S.fold (FH.write devNull) $ S.unfold readEx inh
776
776
777#ifdef INSPECTION
777#ifdef INSPECTION
@@ -781,13 +781,13 @@ inspect $ hasNoTypeClasses 'readWriteFinally_Unfold
781
781
782readWriteFinallyUnfold :: Handle -> Handle -> IO ()
782readWriteFinallyUnfold :: Handle -> Handle -> IO ()
783readWriteFinallyUnfold inh devNull =
783readWriteFinallyUnfold inh devNull =
784− let readEx = UF.finally (\_ -> hClose inh) FH.read
784+ let readEx = UF.finally (\_ -> hClose inh) FH.reader
785 in S.fold (FH.write devNull) $ S.unfold readEx inh
785 in S.fold (FH.write devNull) $ S.unfold readEx inh
786
786
787-- | Send the file contents to /dev/null with exception handling
787-- | Send the file contents to /dev/null with exception handling
788readWriteBracket_Unfold :: Handle -> Handle -> IO ()
788readWriteBracket_Unfold :: Handle -> Handle -> IO ()
789readWriteBracket_Unfold inh devNull =
789readWriteBracket_Unfold inh devNull =
790− let readEx = UF.bracket_ return (\_ -> hClose inh) FH.read
790+ let readEx = UF.bracket_ return (\_ -> hClose inh) FH.reader
791 in S.fold (FH.write devNull) $ S.unfold readEx inh
791 in S.fold (FH.write devNull) $ S.unfold readEx inh
792
792
793#ifdef INSPECTION
793#ifdef INSPECTION
@@ -797,7 +797,7 @@ inspect $ hasNoTypeClasses 'readWriteBracket_Unfold
797
797
798readWriteBracketUnfold :: Handle -> Handle -> IO ()
798readWriteBracketUnfold :: Handle -> Handle -> IO ()
799readWriteBracketUnfold inh devNull =
799readWriteBracketUnfold inh devNull =
800− let readEx = UF.bracket return (\_ -> hClose inh) FH.read
800+ let readEx = UF.bracket return (\_ -> hClose inh) FH.reader
801 in S.fold (FH.write devNull) $ S.unfold readEx inh
801 in S.fold (FH.write devNull) $ S.unfold readEx inh
802
802
803o_1_space_copy_read_exceptions :: BenchEnv -> [Benchmark]
803o_1_space_copy_read_exceptions :: BenchEnv -> [Benchmark]
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+14−14
@@ -59,7 +59,7 @@ import Test.Inspection
59
59
60-- | Get the last byte from a file bytestream.
60-- | Get the last byte from a file bytestream.
61readLast :: Handle -> IO (Maybe Word8)
61readLast :: Handle -> IO (Maybe Word8)
62−readLast = S.last . S.unfold FH.read
62+readLast = S.last . S.unfold FH.reader
63
63
64#ifdef INSPECTION
64#ifdef INSPECTION
65inspect $ hasNoTypeClasses 'readLast
65inspect $ hasNoTypeClasses 'readLast
@@ -71,7 +71,7 @@ inspect $ 'readLast `hasNoType` ''MA.ArrayUnsafe -- FH.read/A.read
71-- assert that flattenArrays constructors are not present
71-- assert that flattenArrays constructors are not present
72-- | Count the number of bytes in a file.
72-- | Count the number of bytes in a file.
73readCountBytes :: Handle -> IO Int
73readCountBytes :: Handle -> IO Int
74−readCountBytes = S.length . S.unfold FH.read
74+readCountBytes = S.length . S.unfold FH.reader
75
75
76#ifdef INSPECTION
76#ifdef INSPECTION
77inspect $ hasNoTypeClasses 'readCountBytes
77inspect $ hasNoTypeClasses 'readCountBytes
@@ -86,7 +86,7 @@ readCountLines =
86 S.length
86 S.length
87 . IUS.lines FL.drain
87 . IUS.lines FL.drain
88 . SS.decodeLatin1
88 . SS.decodeLatin1
89− . S.unfold FH.read
89+ . S.unfold FH.reader
90
90
91#ifdef INSPECTION
91#ifdef INSPECTION
92inspect $ hasNoTypeClasses 'readCountLines
92inspect $ hasNoTypeClasses 'readCountLines
@@ -101,7 +101,7 @@ readCountWords =
101 S.length
101 S.length
102 . IUS.words FL.drain
102 . IUS.words FL.drain
103 . SS.decodeLatin1
103 . SS.decodeLatin1
104− . S.unfold FH.read
104+ . S.unfold FH.reader
105
105
106#ifdef INSPECTION
106#ifdef INSPECTION
107inspect $ hasNoTypeClasses 'readCountWords
107inspect $ hasNoTypeClasses 'readCountWords
@@ -110,7 +110,7 @@ inspect $ hasNoTypeClasses 'readCountWords
110
110
111-- | Sum the bytes in a file.
111-- | Sum the bytes in a file.
112readSumBytes :: Handle -> IO Word8
112readSumBytes :: Handle -> IO Word8
113−readSumBytes = S.sum . S.unfold FH.read
113+readSumBytes = S.sum . S.unfold FH.reader
114
114
115#ifdef INSPECTION
115#ifdef INSPECTION
116inspect $ hasNoTypeClasses 'readSumBytes
116inspect $ hasNoTypeClasses 'readSumBytes
@@ -130,7 +130,7 @@ inspect $ 'readSumBytes `hasNoType` ''MA.ArrayUnsafe -- FH.read/A.read
130-- fusion-plugin to propagate INLINE phase information such that this problem
130-- fusion-plugin to propagate INLINE phase information such that this problem
131-- does not occur.
131-- does not occur.
132readDrain :: Handle -> IO ()
132readDrain :: Handle -> IO ()
133−readDrain inh = S.drain $ S.unfold FH.read inh
133+readDrain inh = S.drain $ S.unfold FH.reader inh
134
134
135-- XXX investigate why we need an INLINE in this case (GHC)
135-- XXX investigate why we need an INLINE in this case (GHC)
136{-# INLINE readDecodeLatin1 #-}
136{-# INLINE readDecodeLatin1 #-}
@@ -138,13 +138,13 @@ readDecodeLatin1 :: Handle -> IO ()
138readDecodeLatin1 inh =
138readDecodeLatin1 inh =
139 S.drain
139 S.drain
140 $ SS.decodeLatin1
140 $ SS.decodeLatin1
141− $ S.unfold FH.read inh
141+ $ S.unfold FH.reader inh
142
142
143readDecodeUtf8 :: Handle -> IO ()
143readDecodeUtf8 :: Handle -> IO ()
144readDecodeUtf8 inh =
144readDecodeUtf8 inh =
145 S.drain
145 S.drain
146 $ SS.decodeUtf8
146 $ SS.decodeUtf8
147− $ S.unfold FH.read inh
147+ $ S.unfold FH.reader inh
148
148
149#ifdef INSPECTION
149#ifdef INSPECTION
150inspect $ hasNoTypeClasses 'readDecodeUtf8
150inspect $ hasNoTypeClasses 'readDecodeUtf8
@@ -189,7 +189,7 @@ getChunksConcatUnfoldCountLines inh =
189 $ IUS.lines FL.drain
189 $ IUS.lines FL.drain
190 $ SS.decodeLatin1
190 $ SS.decodeLatin1
191 -- XXX replace with toBytes
191 -- XXX replace with toBytes
192− $ S.unfoldMany A.read (IFH.getChunks inh)
192+ $ S.unfoldMany A.read (IFH.readChunks inh)
193
193
194#ifdef INSPECTION
194#ifdef INSPECTION
195inspect $ hasNoTypeClasses 'getChunksConcatUnfoldCountLines
195inspect $ hasNoTypeClasses 'getChunksConcatUnfoldCountLines
@@ -210,15 +210,15 @@ o_1_space_reduce_toBytes env =
210-------------------------------------------------------------------------------
210-------------------------------------------------------------------------------
211
211
212chunksOfSum :: Int -> Handle -> IO Int
212chunksOfSum :: Int -> Handle -> IO Int
213−chunksOfSum n inh = S.length $ S.chunksOf n FL.sum (S.unfold FH.read inh)
213+chunksOfSum n inh = S.length $ S.chunksOf n FL.sum (S.unfold FH.reader inh)
214
214
215foldManyPostChunksOfSum :: Int -> Handle -> IO Int
215foldManyPostChunksOfSum :: Int -> Handle -> IO Int
216foldManyPostChunksOfSum n inh =
216foldManyPostChunksOfSum n inh =
217− S.length $ IP.foldManyPost (FL.take n FL.sum) (S.unfold FH.read inh)
217+ S.length $ IP.foldManyPost (FL.take n FL.sum) (S.unfold FH.reader inh)
218
218
219foldManyChunksOfSum :: Int -> Handle -> IO Int
219foldManyChunksOfSum :: Int -> Handle -> IO Int
220foldManyChunksOfSum n inh =
220foldManyChunksOfSum n inh =
221− S.length $ IP.foldMany (FL.take n FL.sum) (S.unfold FH.read inh)
221+ S.length $ IP.foldMany (FL.take n FL.sum) (S.unfold FH.reader inh)
222
222
223-- XXX investigate why we need an INLINE in this case (GHC)
223-- XXX investigate why we need an INLINE in this case (GHC)
224-- Even though allocations remain the same in both cases inlining improves time
224-- Even though allocations remain the same in both cases inlining improves time
@@ -228,7 +228,7 @@ foldManyChunksOfSum n inh =
228chunksOf :: Int -> Handle -> IO Int
228chunksOf :: Int -> Handle -> IO Int
229chunksOf n inh =
229chunksOf n inh =
230 -- writeNUnsafe gives 2.5x boost here over writeN.
230 -- writeNUnsafe gives 2.5x boost here over writeN.
231− S.length $ S.chunksOf n (AT.writeNUnsafe n) (S.unfold FH.read inh)
231+ S.length $ S.chunksOf n (AT.writeNUnsafe n) (S.unfold FH.reader inh)
232
232
233#ifdef INSPECTION
233#ifdef INSPECTION
234inspect $ hasNoTypeClasses 'chunksOf
234inspect $ hasNoTypeClasses 'chunksOf
@@ -241,7 +241,7 @@ inspect $ 'chunksOf `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
241
241
242{-# INLINE arraysOf #-}
242{-# INLINE arraysOf #-}
243arraysOf :: Int -> Handle -> IO Int
243arraysOf :: Int -> Handle -> IO Int
244−arraysOf n inh = S.length $ IP.arraysOf n (S.unfold FH.read inh)
244+arraysOf n inh = S.length $ IP.arraysOf n (S.unfold FH.reader inh)
245
245
246o_1_space_reduce_read_grouped :: BenchEnv -> [Benchmark]
246o_1_space_reduce_read_grouped :: BenchEnv -> [Benchmark]
247o_1_space_reduce_read_grouped env =
247o_1_space_reduce_read_grouped env =
benchmark/Streamly/Benchmark/FileSystem/Handle/ReadWrite.hs+8−8
@@ -52,7 +52,7 @@ import Test.Inspection
52
52
53-- | Copy file
53-- | Copy file
54copyChunks :: Handle -> Handle -> IO ()
54copyChunks :: Handle -> Handle -> IO ()
55−copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.getChunks inh
55+copyChunks inh outh = S.fold (IFH.writeChunks outh) $ IFH.readChunks inh
56
56
57#ifdef INSPECTION
57#ifdef INSPECTION
58inspect $ hasNoTypeClasses 'copyChunks
58inspect $ hasNoTypeClasses 'copyChunks
@@ -75,7 +75,7 @@ o_1_space_copy_chunked env =
75
75
76-- | Copy file
76-- | Copy file
77copyStream :: Handle -> Handle -> IO ()
77copyStream :: Handle -> Handle -> IO ()
78−copyStream inh outh = S.fold (FH.write outh) (S.unfold FH.read inh)
78+copyStream inh outh = S.fold (FH.write outh) (S.unfold FH.reader inh)
79
79
80#ifdef INSPECTION
80#ifdef INSPECTION
81inspect $ hasNoTypeClasses 'copyStream
81inspect $ hasNoTypeClasses 'copyStream
@@ -102,7 +102,7 @@ o_1_space_copy_read env =
102
102
103-- | Send the file contents to /dev/null
103-- | Send the file contents to /dev/null
104readFromBytesNull :: Handle -> Handle -> IO ()
104readFromBytesNull :: Handle -> Handle -> IO ()
105−readFromBytesNull inh devNull = IFH.putBytes devNull $ S.unfold FH.read inh
105+readFromBytesNull inh devNull = IFH.putBytes devNull $ S.unfold FH.reader inh
106
106
107#ifdef INSPECTION
107#ifdef INSPECTION
108inspect $ hasNoTypeClasses 'readFromBytesNull
108inspect $ hasNoTypeClasses 'readFromBytesNull
@@ -116,7 +116,7 @@ inspect $ 'readFromBytesNull `hasNoType` ''D.FoldMany
116readWithFromBytesNull :: Handle -> Handle -> IO ()
116readWithFromBytesNull :: Handle -> Handle -> IO ()
117readWithFromBytesNull inh devNull =
117readWithFromBytesNull inh devNull =
118 IFH.putBytes devNull
118 IFH.putBytes devNull
119− $ S.unfold FH.readWith (defaultChunkSize, inh)
119+ $ S.unfold FH.readerWith (defaultChunkSize, inh)
120
120
121#ifdef INSPECTION
121#ifdef INSPECTION
122inspect $ hasNoTypeClasses 'readWithFromBytesNull
122inspect $ hasNoTypeClasses 'readWithFromBytesNull
@@ -134,7 +134,7 @@ _readChunks inh devNull = IUF.fold fld unf inh
134 where
134 where
135
135
136 fld = FH.write devNull
136 fld = FH.write devNull
137− unf = IUF.many A.read FH.readChunks
137+ unf = IUF.many A.read FH.chunkReader
138
138
139-- | Send the chunk content to /dev/null
139-- | Send the chunk content to /dev/null
140-- Implicitly benchmarked via 'readWithFromBytesNull'
140-- Implicitly benchmarked via 'readWithFromBytesNull'
@@ -144,7 +144,7 @@ _readChunksWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
144 where
144 where
145
145
146 fld = FH.write devNull
146 fld = FH.write devNull
147− unf = IUF.many A.read FH.readChunksWith
147+ unf = IUF.many A.read FH.chunkReaderWith
148
148
149o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
149o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
150o_1_space_copy_fromBytes env =
150o_1_space_copy_fromBytes env =
@@ -163,7 +163,7 @@ writeReadWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
163 where
163 where
164
164
165 fld = FH.writeWith defaultChunkSize devNull
165 fld = FH.writeWith defaultChunkSize devNull
166− unf = FH.readWith
166+ unf = FH.readerWith
167
167
168#ifdef INSPECTION
168#ifdef INSPECTION
169inspect $ hasNoTypeClasses 'writeReadWith
169inspect $ hasNoTypeClasses 'writeReadWith
@@ -180,7 +180,7 @@ writeRead inh devNull = IUF.fold fld unf inh
180 where
180 where
181
181
182 fld = FH.write devNull
182 fld = FH.write devNull
183− unf = FH.read
183+ unf = FH.reader
184
184
185#ifdef INSPECTION
185#ifdef INSPECTION
186inspect $ hasNoTypeClasses 'writeRead
186inspect $ hasNoTypeClasses 'writeRead
benchmark/Streamly/Benchmark/Unicode/Stream.hs+13−14
@@ -23,7 +23,6 @@ import System.IO (Handle)
23
23
24import qualified Streamly.Data.Array.Unboxed as Array
24import qualified Streamly.Data.Array.Unboxed as Array
25import qualified Streamly.Data.Fold as Fold
25import qualified Streamly.Data.Fold as Fold
26−import qualified Streamly.FileSystem.Handle as Handle
27import qualified Streamly.Internal.Data.Stream.IsStream as Stream
26import qualified Streamly.Internal.Data.Stream.IsStream as Stream
28import qualified Streamly.Internal.Data.Unfold as Unfold
27import qualified Streamly.Internal.Data.Unfold as Unfold
29import qualified Streamly.Internal.FileSystem.Handle as Handle
28import qualified Streamly.Internal.FileSystem.Handle as Handle
@@ -54,7 +53,7 @@ copyCodecUtf8ArraysLenient inh outh =
54 Stream.fold (Handle.write outh)
53 Stream.fold (Handle.write outh)
55 $ Unicode.encodeUtf8'
54 $ Unicode.encodeUtf8'
56 $ Unicode.decodeUtf8Arrays
55 $ Unicode.decodeUtf8Arrays
57− $ Handle.getChunks inh
56+ $ Handle.readChunks inh
58
57
59#ifdef INSPECTION
58#ifdef INSPECTION
60inspect $ hasNoTypeClasses 'copyCodecUtf8ArraysLenient
59inspect $ hasNoTypeClasses 'copyCodecUtf8ArraysLenient
@@ -82,7 +81,7 @@ linesUnlinesCopy inh outh =
82 $ Unicode.unlines Unfold.fromList
81 $ Unicode.unlines Unfold.fromList
83 $ Stream.splitOnSuffix (== '\n') Fold.toList
82 $ Stream.splitOnSuffix (== '\n') Fold.toList
84 $ Unicode.decodeLatin1
83 $ Unicode.decodeLatin1
85− $ Stream.unfold Handle.read inh
84+ $ Stream.unfold Handle.reader inh
86
85
87{-# NOINLINE linesUnlinesArrayWord8Copy #-}
86{-# NOINLINE linesUnlinesArrayWord8Copy #-}
88linesUnlinesArrayWord8Copy :: Handle -> Handle -> IO ()
87linesUnlinesArrayWord8Copy :: Handle -> Handle -> IO ()
@@ -90,7 +89,7 @@ linesUnlinesArrayWord8Copy inh outh =
90 Stream.fold (Handle.write outh)
89 Stream.fold (Handle.write outh)
91 $ Stream.interposeSuffix 10 Array.read
90 $ Stream.interposeSuffix 10 Array.read
92 $ Stream.splitOnSuffix (== 10) Array.write
91 $ Stream.splitOnSuffix (== 10) Array.write
93− $ Stream.unfold Handle.read inh
92+ $ Stream.unfold Handle.reader inh
94
93
95-- XXX splitSuffixOn requires -funfolding-use-threshold=150 for better fusion
94-- XXX splitSuffixOn requires -funfolding-use-threshold=150 for better fusion
96-- | Lines and unlines
95-- | Lines and unlines
@@ -102,7 +101,7 @@ linesUnlinesArrayCharCopy inh outh =
102 $ UnicodeArr.unlines
101 $ UnicodeArr.unlines
103 $ UnicodeArr.lines
102 $ UnicodeArr.lines
104 $ Unicode.decodeLatin1
103 $ Unicode.decodeLatin1
105− $ Stream.unfold Handle.read inh
104+ $ Stream.unfold Handle.reader inh
106
105
107#ifdef INSPECTION
106#ifdef INSPECTION
108inspect $ hasNoTypeClassesExcept 'linesUnlinesArrayCharCopy [''Unboxed]
107inspect $ hasNoTypeClassesExcept 'linesUnlinesArrayCharCopy [''Unboxed]
@@ -130,7 +129,7 @@ wordsUnwordsCopyWord8 inh outh =
130 Stream.fold (Handle.write outh)
129 Stream.fold (Handle.write outh)
131 $ Stream.interposeSuffix 32 Unfold.fromList
130 $ Stream.interposeSuffix 32 Unfold.fromList
132 $ Stream.wordsBy isSp Fold.toList
131 $ Stream.wordsBy isSp Fold.toList
133− $ Stream.unfold Handle.read inh
132+ $ Stream.unfold Handle.reader inh
134
133
135#ifdef INSPECTION
134#ifdef INSPECTION
136inspect $ hasNoTypeClasses 'wordsUnwordsCopyWord8
135inspect $ hasNoTypeClasses 'wordsUnwordsCopyWord8
@@ -154,7 +153,7 @@ wordsUnwordsCopy inh outh =
154 $ Stream.wordsBy isSpace Fold.toList
153 $ Stream.wordsBy isSpace Fold.toList
155 -- -- $ Stream.splitOn isSpace Fold.toList
154 -- -- $ Stream.splitOn isSpace Fold.toList
156 $ Unicode.decodeLatin1
155 $ Unicode.decodeLatin1
157− $ Stream.unfold Handle.read inh
156+ $ Stream.unfold Handle.reader inh
158
157
159#ifdef INSPECTION
158#ifdef INSPECTION
160-- inspect $ hasNoTypeClasses 'wordsUnwordsCopy
159-- inspect $ hasNoTypeClasses 'wordsUnwordsCopy
@@ -169,7 +168,7 @@ wordsUnwordsCharArrayCopy inh outh =
169 $ UnicodeArr.unwords
168 $ UnicodeArr.unwords
170 $ UnicodeArr.words
169 $ UnicodeArr.words
171 $ Unicode.decodeLatin1
170 $ Unicode.decodeLatin1
172− $ Stream.unfold Handle.read inh
171+ $ Stream.unfold Handle.reader inh
173
172
174o_1_space_copy_read_group_ungroup :: BenchEnv -> [Benchmark]
173o_1_space_copy_read_group_ungroup :: BenchEnv -> [Benchmark]
175o_1_space_copy_read_group_ungroup env =
174o_1_space_copy_read_group_ungroup env =
@@ -201,7 +200,7 @@ copyStreamLatin1' inh outh =
201 Stream.fold (Handle.write outh)
200 Stream.fold (Handle.write outh)
202 $ Unicode.encodeLatin1'
201 $ Unicode.encodeLatin1'
203 $ Unicode.decodeLatin1
202 $ Unicode.decodeLatin1
204− $ Stream.unfold Handle.read inh
203+ $ Stream.unfold Handle.reader inh
205
204
206#ifdef INSPECTION
205#ifdef INSPECTION
207inspect $ hasNoTypeClasses 'copyStreamLatin1'
206inspect $ hasNoTypeClasses 'copyStreamLatin1'
@@ -221,7 +220,7 @@ copyStreamLatin1 inh outh =
221 Stream.fold (Handle.write outh)
220 Stream.fold (Handle.write outh)
222 $ Unicode.encodeLatin1
221 $ Unicode.encodeLatin1
223 $ Unicode.decodeLatin1
222 $ Unicode.decodeLatin1
224− $ Stream.unfold Handle.read inh
223+ $ Stream.unfold Handle.reader inh
225
224
226#ifdef INSPECTION
225#ifdef INSPECTION
227inspect $ hasNoTypeClasses 'copyStreamLatin1
226inspect $ hasNoTypeClasses 'copyStreamLatin1
@@ -241,7 +240,7 @@ _copyStreamUtf8' inh outh =
241 Stream.fold (Handle.write outh)
240 Stream.fold (Handle.write outh)
242 $ Unicode.encodeUtf8'
241 $ Unicode.encodeUtf8'
243 $ Unicode.decodeUtf8'
242 $ Unicode.decodeUtf8'
244− $ Stream.unfold Handle.read inh
243+ $ Stream.unfold Handle.reader inh
245
244
246#ifdef INSPECTION
245#ifdef INSPECTION
247inspect $ hasNoTypeClasses '_copyStreamUtf8'
246inspect $ hasNoTypeClasses '_copyStreamUtf8'
@@ -257,7 +256,7 @@ copyStreamUtf8 inh outh =
257 Stream.fold (Handle.write outh)
256 Stream.fold (Handle.write outh)
258 $ Unicode.encodeUtf8
257 $ Unicode.encodeUtf8
259 $ Unicode.decodeUtf8
258 $ Unicode.decodeUtf8
260− $ Stream.unfold Handle.read inh
259+ $ Stream.unfold Handle.reader inh
261
260
262#ifdef INSPECTION
261#ifdef INSPECTION
263inspect $ hasNoTypeClasses 'copyStreamUtf8
262inspect $ hasNoTypeClasses 'copyStreamUtf8
@@ -272,7 +271,7 @@ _copyStreamUtf8'Fold inh outh =
272 Stream.fold (Handle.write outh)
271 Stream.fold (Handle.write outh)
273 $ Unicode.encodeUtf8
272 $ Unicode.encodeUtf8
274 $ Stream.foldMany Unicode.writeCharUtf8'
273 $ Stream.foldMany Unicode.writeCharUtf8'
275− $ Stream.unfold Handle.read inh
274+ $ Stream.unfold Handle.reader inh
276
275
277{-# NOINLINE _copyStreamUtf8Parser #-}
276{-# NOINLINE _copyStreamUtf8Parser #-}
278_copyStreamUtf8Parser :: Handle -> Handle -> IO ()
277_copyStreamUtf8Parser :: Handle -> Handle -> IO ()
@@ -281,7 +280,7 @@ _copyStreamUtf8Parser inh outh =
281 $ Unicode.encodeUtf8
280 $ Unicode.encodeUtf8
282 $ Stream.parseMany
281 $ Stream.parseMany
283 (Unicode.parseCharUtf8With Unicode.TransliterateCodingFailure)
282 (Unicode.parseCharUtf8With Unicode.TransliterateCodingFailure)
284− $ Stream.unfold Handle.read inh
283+ $ Stream.unfold Handle.reader inh
285
284
286o_1_space_decode_encode_read :: BenchEnv -> [Benchmark]
285o_1_space_decode_encode_read :: BenchEnv -> [Benchmark]
287o_1_space_decode_encode_read env =
286o_1_space_decode_encode_read env =
core/src/Streamly/Console/Stdio.hs+29−4
@@ -13,9 +13,9 @@
13
13
14module Streamly.Console.Stdio
14module Streamly.Console.Stdio
15 (
15 (
16− -- * Read (stdin)
16+ -- * Unfolds (stdin)
17− read
17+ reader
18− , readChunks
18+ , chunkReader
19
19
20 -- * Write (stdout)
20 -- * Write (stdout)
21 , write
21 , write
@@ -24,8 +24,33 @@ module Streamly.Console.Stdio
24 -- * Write (stderr)
24 -- * Write (stderr)
25 , writeErr
25 , writeErr
26 , writeErrChunks
26 , writeErrChunks
27+
28+ -- * Deprecated
29+ , read
30+ , readChunks
27 )
31 )
28where
32where
29
33
34+import Control.Monad.IO.Class (MonadIO(..))
35+import Data.Word (Word8)
36+import Streamly.Internal.Data.Array.Unboxed.Type (Array)
37+import Streamly.Internal.Data.Unfold (Unfold)
38+
30−import Streamly.Internal.Console.Stdio
39+import Streamly.Internal.Console.Stdio hiding (read, readChunks)
31import Prelude hiding (read)
40import Prelude hiding (read)
41+
42+-- Same as 'reader'
43+--
44+-- @since 0.8.0
45+{-# DEPRECATED read "Please use 'reader' instead" #-}
46+{-# INLINE read #-}
47+read :: MonadIO m => Unfold m () Word8
48+read = reader
49+
50+-- Same as 'chunkReader'
51+--
52+-- @since 0.8.0
53+{-# DEPRECATED readChunks "Please use 'chunkReader' instead" #-}
54+{-# INLINE readChunks #-}
55+readChunks :: MonadIO m => Unfold m () (Array Word8)
56+readChunks = chunkReader
core/src/Streamly/FileSystem/Handle.hs+38−7
@@ -75,12 +75,19 @@ module Streamly.FileSystem.Handle
75 -- position of the file handle. The stream ends as soon as EOF is
75 -- position of the file handle. The stream ends as soon as EOF is
76 -- encountered.
76 -- encountered.
77
77
78+ -- -- *** Streams
78− , read
79+ -- , read
79− , readWith
80+ -- , readWith
80− , readChunks
81+ -- , readChunks
81− , readChunksWith
82+ -- , readChunksWith
83+
84+ -- -- *** Unfolds
85+ , reader
86+ , readerWith
87+ , chunkReader
88+ , chunkReaderWith
82
89
83− -- ** Writing
90+ -- ** Folds
84 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
91 -- | 'TextEncoding', 'NewLineMode', and 'Buffering' options of the
85 -- underlying handle are ignored. The write occurs from the current seek
92 -- underlying handle are ignored. The write occurs from the current seek
86 -- position of the file handle. The write behavior depends on the 'IOMode'
93 -- position of the file handle. The write behavior depends on the 'IOMode'
@@ -91,12 +98,36 @@ module Streamly.FileSystem.Handle
91 , writeChunks
98 , writeChunks
92
99
93 -- * Deprecated
100 -- * Deprecated
94− , readChunksWithBufferOf
101+ , read
95 , readWithBufferOf
102 , readWithBufferOf
103+ , readChunks
104+ , readChunksWithBufferOf
96 , writeChunksWithBufferOf
105 , writeChunksWithBufferOf
97 , writeWithBufferOf
106 , writeWithBufferOf
98 )
107 )
99where
108where
100
109
110+import Control.Monad.IO.Class (MonadIO(..))
111+import Data.Word (Word8)
112+import Streamly.Internal.Data.Array.Unboxed.Type (Array)
113+import Streamly.Internal.Data.Unfold.Type (Unfold)
114+import System.IO (Handle)
115+
101−import Streamly.Internal.FileSystem.Handle
116+import Streamly.Internal.FileSystem.Handle hiding (read, readChunks)
102import Prelude hiding (read)
117import Prelude hiding (read)
118+
119+-- | Same as 'reader'
120+--
121+-- @since 0.7.0
122+{-# DEPRECATED read "Please use 'reader' instead" #-}
123+{-# INLINE read #-}
124+read :: MonadIO m => Unfold m Handle Word8
125+read = reader
126+
127+-- | Same as 'chunkReader'
128+--
129+-- @since 0.7.0
130+{-# DEPRECATED readChunks "Please use 'chunkReader' instead" #-}
131+{-# INLINE readChunks #-}
132+readChunks :: MonadIO m => Unfold m Handle (Array Word8)
133+readChunks = chunkReader
core/src/Streamly/Internal/Console/Stdio.hs+33−29
@@ -9,25 +9,29 @@
9
9
10module Streamly.Internal.Console.Stdio
10module Streamly.Internal.Console.Stdio
11 (
11 (
12− -- * Read
12+ -- * Streams
13 read
13 read
14− , getBytes
14+ , readChars
15− , getChars
16 , readChunks
15 , readChunks
17− , getChunks
18 -- , getChunksLn
16 -- , getChunksLn
19 -- , getStringsWith -- get strings using the supplied decoding
17 -- , getStringsWith -- get strings using the supplied decoding
20 -- , getStrings -- get strings of complete chars,
18 -- , getStrings -- get strings of complete chars,
21 -- leave any partial chars for next string
19 -- leave any partial chars for next string
22 -- , getStringsLn -- get lines decoded as char strings
20 -- , getStringsLn -- get lines decoded as char strings
23
21
24− -- * Write
22+ -- * Unfolds
23+ , reader
24+ , chunkReader
25+
26+ -- * Folds
25 , write
27 , write
28+ , writeChunks
26 , writeErr
29 , writeErr
30+ , writeErrChunks
31+
32+ -- * Stream writes
27 , putBytes -- Buffered (32K)
33 , putBytes -- Buffered (32K)
28 , putChars
34 , putChars
29− , writeChunks
30− , writeErrChunks
31 , putChunks -- Unbuffered
35 , putChunks -- Unbuffered
32 , putStringsWith
36 , putStringsWith
33 , putStrings
37 , putStrings
@@ -59,51 +63,51 @@ import qualified Streamly.Internal.Unicode.Stream as Unicode
59
63
60-- | Unfold standard input into a stream of 'Word8'.
64-- | Unfold standard input into a stream of 'Word8'.
61--
65--
62−-- @since 0.8.0
66+-- @since 0.9.0
63−{-# INLINE read #-}
67+{-# INLINE reader #-}
64−read :: MonadIO m => Unfold m () Word8
68+reader :: MonadIO m => Unfold m () Word8
65−read = Unfold.lmap (\() -> stdin) Handle.read
69+reader = Unfold.lmap (\() -> stdin) Handle.reader
66
70
67-- | Read a byte stream from standard input.
71-- | Read a byte stream from standard input.
68--
72--
69−-- > getBytes = Handle.getBytes stdin
73+-- > read = Handle.read stdin
70−-- > getBytes = Stream.unfold Stdio.read ()
74+-- > read = Stream.unfold Stdio.reader ()
71--
75--
72-- /Pre-release/
76-- /Pre-release/
73--
77--
74−{-# INLINE getBytes #-}
78+{-# INLINE read #-}
75−getBytes :: MonadIO m => Stream m Word8
79+read :: MonadIO m => Stream m Word8
76−getBytes = Handle.getBytes stdin
80+read = Handle.read stdin
77
81
78-- | Read a character stream from Utf8 encoded standard input.
82-- | Read a character stream from Utf8 encoded standard input.
79--
83--
80−-- > getChars = Unicode.decodeUtf8 Stdio.getBytes
84+-- > readChars = Unicode.decodeUtf8 Stdio.read
81--
85--
82-- /Pre-release/
86-- /Pre-release/
83--
87--
84−{-# INLINE getChars #-}
88+{-# INLINE readChars #-}
85−getChars :: MonadIO m => Stream m Char
89+readChars :: MonadIO m => Stream m Char
86−getChars = Unicode.decodeUtf8 getBytes
90+readChars = Unicode.decodeUtf8 read
87
91
88-- | Unfolds standard input into a stream of 'Word8' arrays.
92-- | Unfolds standard input into a stream of 'Word8' arrays.
89--
93--
90−-- @since 0.8.0
94+-- @since 0.9.0
91−{-# INLINE readChunks #-}
95+{-# INLINE chunkReader #-}
92−readChunks :: MonadIO m => Unfold m () (Array Word8)
96+chunkReader :: MonadIO m => Unfold m () (Array Word8)
93−readChunks = Unfold.lmap (\() -> stdin) Handle.readChunks
97+chunkReader = Unfold.lmap (\() -> stdin) Handle.chunkReader
94
98
95-- | Read a stream of chunks from standard input. The maximum size of a single
99-- | Read a stream of chunks from standard input. The maximum size of a single
96-- chunk is limited to @defaultChunkSize@. The actual size read may be less
100-- chunk is limited to @defaultChunkSize@. The actual size read may be less
97-- than @defaultChunkSize@.
101-- than @defaultChunkSize@.
98--
102--
99−-- > getChunks = Handle.getChunks stdin
103+-- > readChunks = Handle.readChunks stdin
100−-- > getChunks = Stream.unfold Stdio.readChunks ()
104+-- > readChunks = Stream.unfold Stdio.chunkReader ()
101--
105--
102-- /Pre-release/
106-- /Pre-release/
103--
107--
104−{-# INLINE getChunks #-}
108+{-# INLINE readChunks #-}
105−getChunks :: MonadIO m => Stream m (Array Word8)
109+readChunks :: MonadIO m => Stream m (Array Word8)
106−getChunks = Handle.getChunks stdin
110+readChunks = Handle.readChunks stdin
107
111
108{-
112{-
109-- | Read UTF8 encoded lines from standard input.
113-- | Read UTF8 encoded lines from standard input.
core/src/Streamly/Internal/FileSystem/Handle.hs+82−96
@@ -30,41 +30,41 @@ module Streamly.Internal.FileSystem.Handle
30 , getChunkOf
30 , getChunkOf
31 , putChunk
31 , putChunk
32
32
33− -- * Byte Stream Read
33+ -- * Streams
34 , read
34 , read
35− -- , readUtf8
36− -- , readLines
37− -- , readFrames
38 , readWith
35 , readWith
39−
40− , getBytes
41− , getBytesWith
42−
43− -- * Chunked Stream Read
44− , readChunks
45 , readChunksWith
36 , readChunksWith
37+ , readChunks
46
38
47− , getChunksWith
39+ -- * Unfolds
48− , getChunks
40+ , reader
41+ -- , readUtf8
42+ -- , readLines
43+ -- , readFrames
44+ , readerWith
45+ , chunkReader
46+ , chunkReaderWith
49
47
50− -- * Byte Stream Write
51− -- Byte stream write (Folds)
48+ -- * Folds
52 , write
49 , write
53− , consumer
54 -- , writeUtf8
50 -- , writeUtf8
55 -- , writeUtf8ByLines
51 -- , writeUtf8ByLines
56 -- , writeByFrames
52 -- , writeByFrames
57 -- , writeLines
53 -- , writeLines
58 , writeWith
54 , writeWith
55+ , writeChunks
56+ , writeChunksWith
59 , writeMaybesWith
57 , writeMaybesWith
60
58
59+ -- * Refolds
60+ , writer
61+ , writerWith
62+ , chunkWriter
63+ -- , chunkWriterWith
64+
65+ -- * Stream writes
61 , putBytes
66 , putBytes
62 , putBytesWith
67 , putBytesWith
63−
64− -- * Chunked Stream Write
65− , writeChunks
66− , writeChunksWith
67−
68 , putChunksWith
68 , putChunksWith
69 , putChunks
69 , putChunks
70
70
@@ -86,7 +86,7 @@ module Streamly.Internal.FileSystem.Handle
86
86
87 -- , readChunksFrom
87 -- , readChunksFrom
88 -- , readChunksFromTo
88 -- , readChunksFromTo
89− , readChunksFromToWith
89+ , chunkReaderFromToWith
90 -- , readChunksFromThenToWith
90 -- , readChunksFromThenToWith
91
91
92 -- , writeIndex
92 -- , writeIndex
@@ -101,6 +101,7 @@ module Streamly.Internal.FileSystem.Handle
101 -- , writeChunksFromTo
101 -- , writeChunksFromTo
102 -- , writeChunksFromToWith
102 -- , writeChunksFromToWith
103 -- , writeChunksFromThenToWith
103 -- , writeChunksFromThenToWith
104+
104 -- * Deprecated
105 -- * Deprecated
105 , readChunksWithBufferOf
106 , readChunksWithBufferOf
106 , readWithBufferOf
107 , readWithBufferOf
@@ -145,9 +146,9 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as K (mkStream)
145-- >>> import qualified Streamly.Data.Array.Unboxed as Array
146-- >>> import qualified Streamly.Data.Array.Unboxed as Array
146-- >>> import qualified Streamly.Data.Fold as Fold
147-- >>> import qualified Streamly.Data.Fold as Fold
147-- >>> import qualified Streamly.Data.Unfold as Unfold
148-- >>> import qualified Streamly.Data.Unfold as Unfold
148−-- >>> import qualified Streamly.FileSystem.Handle as Handle
149-- >>> import qualified Streamly.Prelude as Stream
149-- >>> import qualified Streamly.Prelude as Stream
150--
150--
151+-- >>> import qualified Streamly.Internal.Data.Array.Unboxed.Type as Array (writeNUnsafe)
151-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
152-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
152-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
153-- >>> import qualified Streamly.Internal.Data.Unfold as Unfold (first)
153-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
154-- >>> import qualified Streamly.Internal.FileSystem.Handle as Handle
@@ -220,16 +221,16 @@ _getChunksWith size h = S.fromStreamK go
220 then stp
221 then stp
221 else yld arr go
222 else yld arr go
222
223
223−-- | @getChunksWith size handle@ reads a stream of arrays from the file
224+-- | @readChunksWith size handle@ reads a stream of arrays from the file
224-- handle @handle@. The maximum size of a single array is limited to @size@.
225-- handle @handle@. The maximum size of a single array is limited to @size@.
225-- The actual size read may be less than or equal to @size@.
226-- The actual size read may be less than or equal to @size@.
226--
227--
227−-- >>> getChunksWith size h = Stream.unfold Handle.readChunksWith (size, h)
228+-- >>> readChunksWith size h = Stream.unfold Handle.chunkReaderWith (size, h)
228--
229--
229-- @since 0.9.0
230-- @since 0.9.0
230−{-# INLINE_NORMAL getChunksWith #-}
231+{-# INLINE_NORMAL readChunksWith #-}
231−getChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
232+readChunksWith :: MonadIO m => Int -> Handle -> Stream m (Array Word8)
232−getChunksWith size h = S.fromStreamD (D.Stream step ())
233+readChunksWith size h = S.fromStreamD (D.Stream step ())
233 where
234 where
234 {-# INLINE_LATE step #-}
235 {-# INLINE_LATE step #-}
235 step _ _ = do
236 step _ _ = do
@@ -245,20 +246,19 @@ getChunksWith size h = S.fromStreamD (D.Stream step ())
245-- or equal to @bufsize@.
246-- or equal to @bufsize@.
246--
247--
247-- @since 0.9.0
248-- @since 0.9.0
248−{-# INLINE_NORMAL readChunksWith #-}
249+{-# INLINE_NORMAL chunkReaderWith #-}
249−readChunksWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
250+chunkReaderWith :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
250−readChunksWith =
251+chunkReaderWith =
251 UF.lmap (uncurry getChunk) UF.repeatM
252 UF.lmap (uncurry getChunk) UF.repeatM
252 & UF.takeWhile ((/= 0) . byteLength)
253 & UF.takeWhile ((/= 0) . byteLength)
253
254
254−
255+-- | Same as 'chunkReaderWith'
255−-- | Same as 'readChunksWith'
256--
256--
257-- @since 0.7.0
257-- @since 0.7.0
258−{-# DEPRECATED readChunksWithBufferOf "Please use readChunksWith instead." #-}
258+{-# DEPRECATED readChunksWithBufferOf "Please use chunkReaderWith instead." #-}
259{-# INLINE_NORMAL readChunksWithBufferOf #-}
259{-# INLINE_NORMAL readChunksWithBufferOf #-}
260readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
260readChunksWithBufferOf :: MonadIO m => Unfold m (Int, Handle) (Array Word8)
261−readChunksWithBufferOf = readChunksWith
261+readChunksWithBufferOf = chunkReaderWith
262
262
263-- There are two ways to implement this.
263-- There are two ways to implement this.
264--
264--
@@ -267,15 +267,14 @@ readChunksWithBufferOf = readChunksWith
267-- 2. Simply implement it from scratch like readChunksWith.
267-- 2. Simply implement it from scratch like readChunksWith.
268--
268--
269-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
269-- XXX Change this to readChunksWithFromTo (bufferSize, from, to, h)?
270−--
270+
271-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
271-- | The input to the unfold is @(from, to, bufferSize, handle)@. It starts
272-- reading from the offset `from` in the file and reads up to the offset `to`.
272-- reading from the offset `from` in the file and reads up to the offset `to`.
273--
273--
274−--
274+{-# INLINE_NORMAL chunkReaderFromToWith #-}
275−{-# INLINE_NORMAL readChunksFromToWith #-}
275+chunkReaderFromToWith :: MonadIO m =>
276−readChunksFromToWith :: MonadIO m =>
277 Unfold m (Int, Int, Int, Handle) (Array Word8)
276 Unfold m (Int, Int, Int, Handle) (Array Word8)
278−readChunksFromToWith = Unfold step inject
277+chunkReaderFromToWith = Unfold step inject
279
278
280 where
279 where
281
280
@@ -297,20 +296,17 @@ readChunksFromToWith = Unfold step inject
297 assert (len <= remaining)
296 assert (len <= remaining)
298 $ D.Yield arr (remaining - len, bufSize, h)
297 $ D.Yield arr (remaining - len, bufSize, h)
299
298
300−
301−-- XXX read 'Array a' instead of Word8
302−--
303-- | @getChunks handle@ reads a stream of arrays from the specified file
299-- | @getChunks handle@ reads a stream of arrays from the specified file
304-- handle. The maximum size of a single array is limited to
300-- handle. The maximum size of a single array is limited to
305-- @defaultChunkSize@. The actual size read may be less than or equal to
301-- @defaultChunkSize@. The actual size read may be less than or equal to
306-- @defaultChunkSize@.
302-- @defaultChunkSize@.
307--
303--
308−-- >>> getChunks = Handle.getChunksWith IO.defaultChunkSize
304+-- >>> readChunks = Handle.readChunksWith IO.defaultChunkSize
309--
305--
310−-- @since 0.9.0
306+-- /Pre-release/
311−{-# INLINE getChunks #-}
307+{-# INLINE readChunks #-}
312−getChunks :: MonadIO m => Handle -> Stream m (Array Word8)
308+readChunks :: MonadIO m => Handle -> Stream m (Array Word8)
313−getChunks = getChunksWith defaultChunkSize
309+readChunks = readChunksWith defaultChunkSize
314
310
315-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
311-- | Unfolds a handle into a stream of 'Word8' arrays. Requests to the IO
316-- device are performed using a buffer of size
312-- device are performed using a buffer of size
@@ -318,12 +314,12 @@ getChunks = getChunksWith defaultChunkSize
318-- size of arrays in the resulting stream are therefore less than or equal to
314-- size of arrays in the resulting stream are therefore less than or equal to
319-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
315-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
320--
316--
321−-- >>> readChunks = Unfold.first IO.defaultChunkSize Handle.readChunksWith
317+-- >>> chunkReader = Unfold.first IO.defaultChunkSize Handle.chunkReaderWith
322--
318--
323−-- @since 0.7.0
319+-- @since 0.9.0
324−{-# INLINE readChunks #-}
320+{-# INLINE chunkReader #-}
325−readChunks :: MonadIO m => Unfold m Handle (Array Word8)
321+chunkReader :: MonadIO m => Unfold m Handle (Array Word8)
326−readChunks = UF.first defaultChunkSize readChunksWith
322+chunkReader = UF.first defaultChunkSize chunkReaderWith
327
323
328-------------------------------------------------------------------------------
324-------------------------------------------------------------------------------
329-- Read File to Stream
325-- Read File to Stream
@@ -336,54 +332,50 @@ readChunks = UF.first defaultChunkSize readChunksWith
336-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
332-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
337-- to the IO device are performed using buffers of @bufsize@.
333-- to the IO device are performed using buffers of @bufsize@.
338--
334--
339−-- >>> readWith = Unfold.many Array.read Handle.readChunksWith
335+-- >>> readerWith = Unfold.many Array.read Handle.chunkReaderWith
340--
336--
341-- @since 0.9.0
337-- @since 0.9.0
342−{-# INLINE readWith #-}
338+{-# INLINE readerWith #-}
343−readWith :: MonadIO m => Unfold m (Int, Handle) Word8
339+readerWith :: MonadIO m => Unfold m (Int, Handle) Word8
344−readWith = UF.many A.read readChunksWith
340+readerWith = UF.many A.read chunkReaderWith
345
341
346−-- | Same as 'readWith'
342+-- | Same as 'readerWith'
347--
343--
348-- @since 0.7.0
344-- @since 0.7.0
349−{-# DEPRECATED readWithBufferOf "Please use readWith instead." #-}
345+{-# DEPRECATED readWithBufferOf "Please use 'readerWith' instead." #-}
350{-# INLINE readWithBufferOf #-}
346{-# INLINE readWithBufferOf #-}
351readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
347readWithBufferOf :: MonadIO m => Unfold m (Int, Handle) Word8
352−readWithBufferOf = readWith
348+readWithBufferOf = readerWith
353
349
354−-- | @getBytesWith bufsize handle@ reads a byte stream from a file
350+-- | @readWith bufsize handle@ reads a byte stream from a file
355-- handle, reads are performed in chunks of up to @bufsize@.
351-- handle, reads are performed in chunks of up to @bufsize@.
356--
352--
357−-- >>> getBytesWith size h = Stream.unfoldMany Array.read $ Handle.getChunksWith size h
353+-- >>> readWith size h = Stream.unfoldMany Array.read $ Handle.readChunksWith size h
358--
354--
359-- /Pre-release/
355-- /Pre-release/
360−{-# INLINE getBytesWith #-}
356+{-# INLINE readWith #-}
361−getBytesWith :: MonadIO m => Int -> Handle -> Stream m Word8
357+readWith :: MonadIO m => Int -> Handle -> Stream m Word8
362−getBytesWith size h = AS.concat $ getChunksWith size h
358+readWith size h = AS.concat $ readChunksWith size h
363
359
364−-- TODO
365−-- Generate a stream of elements of the given type from a file 'Handle'.
366−-- read :: (MonadIO m, Unboxed a) => Handle -> Stream m a
367−--
368-- | Unfolds a file handle into a byte stream. IO requests to the device are
360-- | Unfolds a file handle into a byte stream. IO requests to the device are
369-- performed in sizes of
361-- performed in sizes of
370-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
362-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
371--
363--
372−-- >>> read = Unfold.many Handle.readChunks Array.read
364+-- >>> reader = Unfold.many Array.read chunkReader
373--
365--
374−-- @since 0.7.0
366+-- @since 0.9.0
375−{-# INLINE read #-}
367+{-# INLINE reader #-}
376−read :: MonadIO m => Unfold m Handle Word8
368+reader :: MonadIO m => Unfold m Handle Word8
377−read = UF.many A.read readChunks
369+reader = UF.many A.read chunkReader
378
370
379-- | Generate a byte stream from a file 'Handle'.
371-- | Generate a byte stream from a file 'Handle'.
380--
372--
381−-- >>> getBytes h = Stream.unfoldMany Array.read $ Handle.getChunks h
373+-- >>> read h = Stream.unfoldMany Array.read $ Handle.readChunks h
382--
374--
383-- /Pre-release/
375-- /Pre-release/
384−{-# INLINE getBytes #-}
376+{-# INLINE read #-}
385−getBytes :: MonadIO m => Handle -> Stream m Word8
377+read :: MonadIO m => Handle -> Stream m Word8
386−getBytes = AS.concat . getChunks
378+read = AS.concat . readChunks
387
379
388-------------------------------------------------------------------------------
380-------------------------------------------------------------------------------
389-- Writing
381-- Writing
@@ -479,12 +471,12 @@ writeChunks h = FL.drainBy (putChunk h)
479-- | Like writeChunks but uses the experimental 'Refold' API.
471-- | Like writeChunks but uses the experimental 'Refold' API.
480--
472--
481-- /Internal/
473-- /Internal/
482−{-# INLINE consumeChunks #-}
474+{-# INLINE chunkWriter #-}
483−consumeChunks :: MonadIO m => Refold m Handle (Array a) ()
475+chunkWriter :: MonadIO m => Refold m Handle (Array a) ()
484−consumeChunks = Refold.drainBy putChunk
476+chunkWriter = Refold.drainBy putChunk
485
477
486-- XXX lpackArraysChunksOf should be written idiomatically
478-- XXX lpackArraysChunksOf should be written idiomatically
487−--
479+
488-- | @writeChunksWith bufsize handle@ writes a stream of arrays
480-- | @writeChunksWith bufsize handle@ writes a stream of arrays
489-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
481-- to @handle@ after coalescing the adjacent arrays in chunks of @bufsize@.
490-- We never split an array, if a single array is bigger than the specified size
482-- We never split an array, if a single array is bigger than the specified size
@@ -515,7 +507,7 @@ writeChunksWithBufferOf = writeChunksWith
515-- writes as well.
507-- writes as well.
516
508
517-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
509-- XXX Maybe we should have a Fold.arraysOf like we have Stream.arraysOf
518−--
510+
519-- | @writeWith reqSize handle@ writes the input stream to @handle@.
511-- | @writeWith reqSize handle@ writes the input stream to @handle@.
520-- Bytes in the input stream are collected into a buffer until we have a chunk
512-- Bytes in the input stream are collected into a buffer until we have a chunk
521-- of @reqSize@ and then written to the IO device.
513-- of @reqSize@ and then written to the IO device.
@@ -548,13 +540,13 @@ writeMaybesWith n h =
548 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
540 writeOnNothing = FL.takeEndBy_ isNothing writeNJusts
549 in FL.many writeOnNothing (writeChunks h)
541 in FL.many writeOnNothing (writeChunks h)
550
542
551−-- | Like 'writeWith' but uses the experimental 'Refold' API.
543+-- | Like 'writeWith' but uses the experimental 'Refold' API.
552--
544--
553-- /Internal/
545-- /Internal/
554−{-# INLINE consumerWith #-}
546+{-# INLINE writerWith #-}
555−consumerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
547+writerWith :: MonadIO m => Int -> Refold m Handle Word8 ()
556−consumerWith n =
548+writerWith n =
557− FL.refoldMany (FL.take n $ writeNUnsafe n) consumeChunks
549+ FL.refoldMany (FL.take n $ writeNUnsafe n) chunkWriter
558
550
559-- | Write a byte stream to a file handle. Accumulates the input in chunks of
551-- | Write a byte stream to a file handle. Accumulates the input in chunks of
560-- up to 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before writing
552-- up to 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize' before writing
@@ -570,15 +562,9 @@ write = writeWith defaultChunkSize
570-- | Like 'write' but uses the experimental 'Refold' API.
562-- | Like 'write' but uses the experimental 'Refold' API.
571--
563--
572-- /Internal/
564-- /Internal/
573−{-# INLINE consumer #-}
565+{-# INLINE writer #-}
574−consumer :: MonadIO m => Refold m Handle Word8 ()
566+writer :: MonadIO m => Refold m Handle Word8 ()
575−consumer = consumerWith defaultChunkSize
576−
577−{-
578−{-# INLINE write #-}
579−write :: (MonadIO m, Unboxed a) => Handle -> Stream m a -> m ()
580−write = toHandleWith A.defaultChunkSize
567+writer = writerWith defaultChunkSize
581−-}
582
568
583-- XXX mmap a file into an array. This could be useful for in-place operations
569-- XXX mmap a file into an array. This could be useful for in-place operations
584-- on a file. For example, we can quicksort the contents of a file by mmapping
570-- on a file. For example, we can quicksort the contents of a file by mmapping
hie.yaml+3−1
@@ -34,13 +34,15 @@ cradle:
34 component: "bench:Data.Stream"
34 component: "bench:Data.Stream"
35 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Expand.hs"
35 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Expand.hs"
36 component: "bench:Data.Stream"
36 component: "bench:Data.Stream"
37+ - path: "./benchmark/Streamly/Benchmark/Data/Stream/Exceptions.hs"
38+ component: "bench:Data.Stream"
37 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Generate.hs"
39 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Generate.hs"
38 component: "bench:Data.Stream"
40 component: "bench:Data.Stream"
39 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Reduce.hs"
41 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Reduce.hs"
40 component: "bench:Data.Stream"
42 component: "bench:Data.Stream"
41 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Transform.hs"
43 - path: "./benchmark/Streamly/Benchmark/Data/Stream/Transform.hs"
42 component: "bench:Data.Stream"
44 component: "bench:Data.Stream"
43− - path: "./benchmark/Streamly/Benchmark/Data/Stream/Async.hs"
45+ - path: "./benchmark/Streamly/Benchmark/Data/Stream/Concurrent.hs"
44 component: "bench:Data.Stream.Concurrent"
46 component: "bench:Data.Stream.Concurrent"
45 - path: "./benchmark/Streamly/Benchmark/Data/Stream/ConcurrentCommon.hs"
47 - path: "./benchmark/Streamly/Benchmark/Data/Stream/ConcurrentCommon.hs"
46 component: "bench:Data.Stream.Concurrent"
48 component: "bench:Data.Stream.Concurrent"
src/Streamly/Internal/FileSystem/Event/Darwin.hs+1−1
@@ -512,7 +512,7 @@ readOneEvent = do
512
512
513watchToStream :: Watch -> Stream IO Event
513watchToStream :: Watch -> Stream IO Event
514watchToStream (Watch handle _ _) =
514watchToStream (Watch handle _ _) =
515− S.parseMany readOneEvent $ S.unfold FH.read handle
515+ S.parseMany readOneEvent $ S.unfold FH.reader handle
516
516
517-- XXX Write tests for all the points in macOS specific behavior.
517-- XXX Write tests for all the points in macOS specific behavior.
518--
518--
src/Streamly/Internal/FileSystem/Event/Linux.hs+1−1
@@ -879,7 +879,7 @@ watchToStream cfg wt@(Watch handle _) = do
879 -- sizeof(struct inotify_event) + NAME_MAX + 1
879 -- sizeof(struct inotify_event) + NAME_MAX + 1
880 --
880 --
881 -- will be sufficient to read at least one event.
881 -- will be sufficient to read at least one event.
882− S.parseMany (readOneEvent cfg wt) $ S.unfold FH.read handle
882+ S.parseMany (readOneEvent cfg wt) $ S.unfold FH.reader handle
883
883
884-- XXX We should not go across the mount points of network file systems or file
884-- XXX We should not go across the mount points of network file systems or file
885-- systems that are known to not generate any events.
885-- systems that are known to not generate any events.
src/Streamly/Internal/FileSystem/File.hs+45−46
@@ -43,37 +43,36 @@ module Streamly.Internal.FileSystem.File
43 -- ** File IO Using Handle
43 -- ** File IO Using Handle
44 withFile
44 withFile
45
45
46− -- ** Read From File
46+ -- ** Streams
47− , readWith
48 , read
47 , read
48+ , readChunksWith
49+ , readChunks
50+
51+ -- ** Unfolds
52+ , readerWith
53+ , reader
49 -- , readShared
54 -- , readShared
50 -- , readUtf8
55 -- , readUtf8
51 -- , readLines
56 -- , readLines
52 -- , readFrames
57 -- , readFrames
53−
58+ , chunkReaderWith
54− , toBytes
59+ , chunkReaderFromToWith
55−
60+ , chunkReader
56− -- -- * Array Read
57− , readChunksWith
58− , readChunksFromToWith
59− , readChunks
60−
61− , toChunksWith
62− , toChunks
63
61
64 -- ** Write To File
62 -- ** Write To File
63+ , putChunk -- writeChunk?
64+
65+ -- ** Folds
65 , write
66 , write
66 -- , writeUtf8
67 -- , writeUtf8
67 -- , writeUtf8ByLines
68 -- , writeUtf8ByLines
68 -- , writeByFrames
69 -- , writeByFrames
69 , writeWith
70 , writeWith
71+ , writeChunks
70
72
73+ -- ** Writing Streams
71− , fromBytes
74+ , fromBytes -- putBytes?
72 , fromBytesWith
75 , fromBytesWith
73−
74− -- -- * Array Write
75− , putChunk
76− , writeChunks
77 , fromChunks
76 , fromChunks
78
77
79 -- ** Append To File
78 -- ** Append To File
@@ -212,30 +211,30 @@ appendArray file arr = SIO.withFile file AppendMode (`FH.putChunk` arr)
212-- Stream of Arrays IO
211-- Stream of Arrays IO
213-------------------------------------------------------------------------------
212-------------------------------------------------------------------------------
214
213
215−-- | @toChunksWith size file@ reads a stream of arrays from file @file@.
214+-- | @readChunksWith size file@ reads a stream of arrays from file @file@.
216-- The maximum size of a single array is specified by @size@. The actual size
215-- The maximum size of a single array is specified by @size@. The actual size
217-- read may be less than or equal to @size@.
216-- read may be less than or equal to @size@.
218--
217--
219-- @since 0.9.0
218-- @since 0.9.0
220−{-# INLINE toChunksWith #-}
219+{-# INLINE readChunksWith #-}
221−toChunksWith :: (MonadCatch m, MonadAsync m)
220+readChunksWith :: (MonadCatch m, MonadAsync m)
222 => Int -> FilePath -> Stream m (Array Word8)
221 => Int -> FilePath -> Stream m (Array Word8)
223−toChunksWith size file =
222+readChunksWith size file =
224− withFile file ReadMode (FH.getChunksWith size)
223+ withFile file ReadMode (FH.readChunksWith size)
225
224
226-- XXX read 'Array a' instead of Word8
225-- XXX read 'Array a' instead of Word8
227--
226--
228−-- | @toChunks file@ reads a stream of arrays from file @file@.
227+-- | @readChunks file@ reads a stream of arrays from file @file@.
229-- The maximum size of a single array is limited to @defaultChunkSize@. The
228-- The maximum size of a single array is limited to @defaultChunkSize@. The
230-- actual size read may be less than @defaultChunkSize@.
229-- actual size read may be less than @defaultChunkSize@.
231--
230--
232−-- > toChunks = toChunksWith defaultChunkSize
231+-- > readChunks = readChunksWith defaultChunkSize
233--
232--
234-- @since 0.7.0
233-- @since 0.7.0
235−{-# INLINE toChunks #-}
234+{-# INLINE readChunks #-}
236−toChunks :: (MonadCatch m, MonadAsync m)
235+readChunks :: (MonadCatch m, MonadAsync m)
237 => FilePath -> Stream m (Array Word8)
236 => FilePath -> Stream m (Array Word8)
238−toChunks = toChunksWith defaultChunkSize
237+readChunks = readChunksWith defaultChunkSize
239
238
240-------------------------------------------------------------------------------
239-------------------------------------------------------------------------------
241-- Read File to Stream
240-- Read File to Stream
@@ -252,10 +251,10 @@ toChunks = toChunksWith defaultChunkSize
252--
251--
253-- /Pre-release/
252-- /Pre-release/
254--
253--
255−{-# INLINE readChunksWith #-}
254+{-# INLINE chunkReaderWith #-}
256−readChunksWith :: (MonadCatch m, MonadAsync m)
255+chunkReaderWith :: (MonadCatch m, MonadAsync m)
257 => Unfold m (Int, FilePath) (Array Word8)
256 => Unfold m (Int, FilePath) (Array Word8)
258−readChunksWith = usingFile2 FH.readChunksWith
257+chunkReaderWith = usingFile2 FH.chunkReaderWith
259
258
260-- | Unfold the tuple @(from, to, bufsize, filepath)@ into a stream
259-- | Unfold the tuple @(from, to, bufsize, filepath)@ into a stream
261-- of 'Word8' arrays.
260-- of 'Word8' arrays.
@@ -265,10 +264,10 @@ readChunksWith = usingFile2 FH.readChunksWith
265-- less than or equal to @bufsize@.
264-- less than or equal to @bufsize@.
266--
265--
267-- /Pre-release/
266-- /Pre-release/
268−{-# INLINE readChunksFromToWith #-}
267+{-# INLINE chunkReaderFromToWith #-}
269−readChunksFromToWith :: (MonadCatch m, MonadAsync m) =>
268+chunkReaderFromToWith :: (MonadCatch m, MonadAsync m) =>
270 Unfold m (Int, Int, Int, FilePath) (Array Word8)
269 Unfold m (Int, Int, Int, FilePath) (Array Word8)
271−readChunksFromToWith = usingFile3 FH.readChunksFromToWith
270+chunkReaderFromToWith = usingFile3 FH.chunkReaderFromToWith
272
271
273-- | Unfolds a 'FilePath' into a stream of 'Word8' arrays. Requests to the IO
272-- | Unfolds a 'FilePath' into a stream of 'Word8' arrays. Requests to the IO
274-- device are performed using a buffer of size
273-- device are performed using a buffer of size
@@ -277,26 +276,26 @@ readChunksFromToWith = usingFile3 FH.readChunksFromToWith
277-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
276-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
278--
277--
279-- /Pre-release/
278-- /Pre-release/
280−{-# INLINE readChunks #-}
279+{-# INLINE chunkReader #-}
281−readChunks :: (MonadCatch m, MonadAsync m) => Unfold m FilePath (Array Word8)
280+chunkReader :: (MonadCatch m, MonadAsync m) => Unfold m FilePath (Array Word8)
282−readChunks = usingFile FH.readChunks
281+chunkReader = usingFile FH.chunkReader
283
282
284-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
283-- | Unfolds the tuple @(bufsize, filepath)@ into a byte stream, read requests
285-- to the IO device are performed using buffers of @bufsize@.
284-- to the IO device are performed using buffers of @bufsize@.
286--
285--
287-- /Pre-release/
286-- /Pre-release/
288−{-# INLINE readWith #-}
287+{-# INLINE readerWith #-}
289−readWith :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8
288+readerWith :: (MonadCatch m, MonadAsync m) => Unfold m (Int, FilePath) Word8
290−readWith = usingFile2 FH.readWith
289+readerWith = usingFile2 FH.readerWith
291
290
292-- | Unfolds a file path into a byte stream. IO requests to the device are
291-- | Unfolds a file path into a byte stream. IO requests to the device are
293-- performed in sizes of
292-- performed in sizes of
294-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
293-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
295--
294--
296−-- @since 0.7.0
295+-- /Pre-release/
297−{-# INLINE read #-}
296+{-# INLINE reader #-}
298−read :: (MonadCatch m, MonadAsync m) => Unfold m FilePath Word8
297+reader :: (MonadCatch m, MonadAsync m) => Unfold m FilePath Word8
299−read = UF.many A.read (usingFile FH.readChunks)
298+reader = UF.many A.read (usingFile FH.chunkReader)
300
299
301-- | Generate a stream of bytes from a file specified by path. The stream ends
300-- | Generate a stream of bytes from a file specified by path. The stream ends
302-- when EOF is encountered. File is locked using multiple reader and single
301-- when EOF is encountered. File is locked using multiple reader and single
@@ -304,9 +303,9 @@ read = UF.many A.read (usingFile FH.readChunks)
304--
303--
305-- /Pre-release/
304-- /Pre-release/
306--
305--
307−{-# INLINE toBytes #-}
306+{-# INLINE read #-}
308−toBytes :: (MonadCatch m, MonadAsync m) => FilePath -> Stream m Word8
307+read :: (MonadCatch m, MonadAsync m) => FilePath -> Stream m Word8
309−toBytes file = AS.concat $ withFile file ReadMode FH.getChunks
308+read file = AS.concat $ withFile file ReadMode FH.readChunks
310
309
311{-
310{-
312-- | Generate a stream of elements of the given type from a file 'Handle'. The
311-- | Generate a stream of elements of the given type from a file 'Handle'. The
test/Streamly/Test/FileSystem/Handle.hs+8−8
@@ -77,20 +77,20 @@ executor f =
77
77
78readFromHandle :: IO (Stream IO Char)
78readFromHandle :: IO (Stream IO Char)
79readFromHandle =
79readFromHandle =
80− let f = Unicode.decodeUtf8 . Stream.unfold Handle.read
80+ let f = Unicode.decodeUtf8 . Stream.unfold Handle.reader
81 in executor f
81 in executor f
82
82
83readWithBufferFromHandle :: IO (Stream IO Char)
83readWithBufferFromHandle :: IO (Stream IO Char)
84readWithBufferFromHandle =
84readWithBufferFromHandle =
85 let f1 = (\h -> (1024, h))
85 let f1 = (\h -> (1024, h))
86− f2 = Unicode.decodeUtf8 . Stream.unfold Handle.readWith . f1
86+ f2 = Unicode.decodeUtf8 . Stream.unfold Handle.readerWith . f1
87 in executor f2
87 in executor f2
88
88
89readChunksFromHandle :: IO (Stream IO Char)
89readChunksFromHandle :: IO (Stream IO Char)
90readChunksFromHandle =
90readChunksFromHandle =
91 let f = Unicode.decodeUtf8
91 let f = Unicode.decodeUtf8
92 . Stream.concatMap Array.toStream
92 . Stream.concatMap Array.toStream
93− . Stream.unfold Handle.readChunks
93+ . Stream.unfold Handle.chunkReader
94 in executor f
94 in executor f
95
95
96readChunksWithBuffer :: IO (Stream IO Char)
96readChunksWithBuffer :: IO (Stream IO Char)
@@ -99,7 +99,7 @@ readChunksWithBuffer =
99 f2 =
99 f2 =
100 Unicode.decodeUtf8
100 Unicode.decodeUtf8
101 . Stream.concatMap Array.toStream
101 . Stream.concatMap Array.toStream
102− . Stream.unfold Handle.readChunksWith
102+ . Stream.unfold Handle.chunkReaderWith
103 . f1
103 . f1
104 in executor f2
104 in executor f2
105
105
@@ -129,7 +129,7 @@ testWrite hfold =
129 _ <- Stream.fold (hfold h) $ Stream.fromList list
129 _ <- Stream.fold (hfold h) $ Stream.fromList list
130 hFlush h
130 hFlush h
131 hSeek h AbsoluteSeek 0
131 hSeek h AbsoluteSeek 0
132− ls <- toList $ Stream.unfold Handle.read h
132+ ls <- toList $ Stream.unfold Handle.reader h
133 hClose h
133 hClose h
134 return (ls == list)
134 return (ls == list)
135
135
@@ -151,10 +151,10 @@ testWriteWithChunk =
151 hw <- openFile fpathWrite ReadWriteMode
151 hw <- openFile fpathWrite ReadWriteMode
152 hSeek hw AbsoluteSeek 0
152 hSeek hw AbsoluteSeek 0
153 _ <- Stream.fold (Handle.writeChunks hw)
153 _ <- Stream.fold (Handle.writeChunks hw)
154− $ Stream.unfold Handle.readChunksWith (1024, hr)
154+ $ Stream.unfold Handle.chunkReaderWith (1024, hr)
155 hFlush hw
155 hFlush hw
156 hSeek hw AbsoluteSeek 0
156 hSeek hw AbsoluteSeek 0
157− ls <- toList $ Stream.unfold Handle.read hw
157+ ls <- toList $ Stream.unfold Handle.reader hw
158 let arr = Array.fromList ls
158 let arr = Array.fromList ls
159 return (testDataLarge == utf8ToString arr)
159 return (testDataLarge == utf8ToString arr)
160
160
@@ -171,7 +171,7 @@ testReadChunksFromToWith from to buffSize res = monadicIO $ run go
171 ls <-
171 ls <-
172 toList
172 toList
173 $ Stream.unfold
173 $ Stream.unfold
174− Handle.readChunksFromToWith (from, to, buffSize, h)
174+ Handle.chunkReaderFromToWith (from, to, buffSize, h)
175 return (res `shouldBe` fmap Array.toList ls)
175 return (res `shouldBe` fmap Array.toList ls)
176
176
177-- Test for first byte
177-- Test for first byte